Changeset 5

Show
Ignore:
Timestamp:
06/25/06 00:21:34 (2 years ago)
Author:
steve
Message:

major rewrites

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • bend.php

    r3 r5  
    3030         * - PHP 5 with psql support 
    3131         * - lsdvd >= v.0.16 
     32         * - transcode >= 1.0.2 
     33         * - eject 
    3234         */ 
    33  
     35          
     36        // Check for system requirements 
     37         
     38        exec("which mencoder", $foo, $return_var); 
     39         
     40        if($return_var == 1) { 
     41                die('You must have mencoder installed to use this program.'); 
     42        } 
     43        exec("which lsdvd", $foo, $return_var); 
     44        if($return_var == 1) 
     45                die('You must have lsdvd v0.16 installed to use this program.'); 
     46         
    3447        require_once 'inc.pgsql.php'; 
    3548        require_once 'class.dvd.php'; 
    36  
    37         $stdout = fopen('php://stdout', 'w'); 
    38         $stdin = fopen('php://stdin', 'r'); 
    39         $stderr = fopen('php://stderr', 'w'); 
    4049 
    4150        /** 
     
    6776 
    6877                return $home; 
    69         } 
    70  
    71         /** 
    72          * Parse CLI arguments 
    73          * 
    74          * If a value is unset, it will be set to 1 
    75          * 
    76          * @param $argc argument count (system variable) 
    77          * @param $argv argument array (system variable) 
    78          * @return array 
    79          */ 
    80         function parseArguments($argc, $argv) { 
    81                 if($argc > 1) { 
    82                         array_shift($argv); 
    83  
    84                         for($x = 0; $x < count($argv); $x++) { 
    85                                 if(preg_match('/^(-\w$|--\w+)/', $argv[$x]) > 0) { 
    86                                         $argv[$x] = preg_replace('/^-{1,2}/', '', $argv[$x]); 
    87                                         $args[$argv[$x]] = 1; 
    88                                 } 
    89                                 else { 
    90                                         if(in_array($argv[($x-1)], array_keys($args))) { 
    91                                                 $args[$argv[($x-1)]] = $argv[$x]; 
    92                                         } 
    93                                 } 
    94                         } 
    95  
    96                         return $args; 
    97                 } 
    9878        } 
    9979 
     
    138118                        } 
    139119                        else { 
    140                                 trigger_error("Not a directory: $dir", E_WARNING); 
     120                                trigger_error("Not a directory: $dir", E_USER_WARNING); 
    141121                                return false; 
    142122                        } 
     
    146126        // TODO: write this for php4 users 
    147127        if(!function_exists('simplexml_load_string')) { 
    148                 trigger_error("Sorry, you need PHP5 with SimpleXML support to run bend / dvd2mkv", E_ERROR); 
     128                trigger_error("Sorry, you need PHP5 with SimpleXML support to run bend / dvd2mkv", E_USER_ERROR); 
    149129        } 
    150130 
    151131        // Read the config file 
    152132        $home = getHomeDirectory(); 
    153         $bendrc = "$home/.bendrc"; 
     133        $bendrc = "$home/.bend"; 
    154134 
    155135        // Default configuration 
     
    162142        ); 
    163143 
     144        /** Get the configuration options */ 
    164145        if(file_exists($bendrc)) { 
    165146                #$arr_config = parseConfigFile($bendrc); 
     
    167148        } 
    168149        else { 
    169                 trigger_error("No config file found, using defaults", E_WARNING); 
    170         } 
    171  
    172         #print_r($arr_config); 
    173         #die; 
    174  
    175         if(substr($argv[0], -7, 7) == 'dvd2mkv') 
    176                 $dvd2mkv = true; 
    177         else 
    178                 $dvd2mkv = false; 
    179                  
     150                trigger_error("No config file found, using defaults", E_USER_WARNING); 
     151        } 
     152         
    180153        // Create the DVD object 
    181154        $dvd =& new DVD($dvd2mkv); 
    182155 
    183         // Set the configuration flags 
    184         $dvd->config = $arr_config; 
    185          
    186         // Grab the commandline arguments 
    187         $dvd->args = parseArguments($argc, $argv); 
    188          
    189         #print_r($dvd->args); 
    190         #die; 
    191  
    192         // Set min, max length 
    193         if(isset($dvd->args['min'])) 
    194                 $dvd->min_len = $dvd->args['min']; 
    195         if(isset($dvd->args['max'])) 
    196                 $dvd->max_len = $dvd->args['max']; 
    197  
     156        // Set the configuration options 
     157        $dvd->setConfig($argc, $argv, $arr_config); 
     158         
    198159        $arr_cmd = array( 
    199160                'h' => array('help', 'Display this help'), 
     
    208169 
    209170        // Display help if no arguments are passed 
     171        /* 
    210172        if(($argc == 1 && $dvd2mkv == false) || $dvd->args['h'] == 1 || $dvd->args['help'] == 1) { 
    211173 
     
    244206                die; 
    245207        } 
     208        */ 
     209         
     210        // $arr = $dvd->getTrackStats($dvd->arr_tracks); 
    246211 
    247212        // Clear the queue 
    248         if($dvd->args['clear'] == 1) { 
     213        if($dvd->args['clear'] == 1 || $dvd->args['c'] == 1) { 
    249214                $dvd->emptyQueue(); 
    250215        } 
     
    259224        if($dvd->args['archive'] == 1 || $dvd->args['rip'] == 1) { 
    260225 
    261                 $dvd->lsdvd(); 
    262  
    263                 $matches = $dvd->getMatches(); 
     226                $dvd->disc_id = $dvd->getDiscID($dvd->config['dvd_device']); 
    264227                 
    265                 if(count($matches) == 1) { 
    266                         echo "Found an exact match!!\n"; 
    267                         $dvd->tv_show = $matches[0]; 
    268                 } 
    269                 elseif(count($matches) > 1 && !isset($dvd->args['tv_show'])) { 
    270                         die("There is more than one matching title.  Pass --id <id> to continue.\n"); 
    271                 } 
    272                 elseif(count($matches) > 1 && isset($dvd->args['id']) && is_numeric($dvd->args['id'])) { 
    273                         $dvd->tv_show = $dvd->args['id']; 
    274                 } 
    275                 elseif(count($matches) == 0 && !isset($dvd->args['title'])) { 
    276                         die("This is a new tv_show.  Pass --title <title> to create a new record, or --id <id> to use an existing.\n"); 
    277                 } 
    278  
    279                 if($dvd->args['archive'] == 1) { 
    280  
    281                         // Archive the title 
    282                         if(count($matches) == 0 && isset($dvd->args['title']) && !empty($dvd->args['title'])) { 
    283                                 decho("Archiving title."); 
    284                                 $dvd->archiveTitle(); 
    285                         } 
    286  
    287                         // Archive the disc 
    288                         $disc = $dvd->getDisc(); 
    289                         if($disc === false) { 
    290                                 $dvd->archiveDisc(); 
    291                                 echo "Archived disc: {$dvd->disc_title}\n"; 
    292                                 $disc = $dvd->getDisc(); 
    293                         } 
    294  
    295                         // Archive the episodes 
    296                         if($disc === true) { 
    297                                 $sql_episodes = "SELECT 1 FROM episodes WHERE disc = {$dvd->disc} AND ignore = FALSE;"; 
    298                                 $rs_episodes = pg_query($sql_episodes) or die(pg_last_error()); 
    299                                 $dvd->num_episodes = pg_num_rows($rs_episodes); 
    300  
    301                                 if($dvd->num_episodes == 0) { 
    302                                         $dvd->archiveEpisodes(); 
    303                                 } 
    304                         } 
    305                 } 
    306         } 
    307  
     228                $query_disc = $dvd->queryDisc($dvd->disc_id); 
     229                 
     230                // If disc is not in the database, it needs to be archived 
     231                if($query_disc === false) { 
     232                        $dvd->msg("Your DVD is not in the database."); 
     233                         
     234                        if(!isset($dvd->args['archive'])) { 
     235                                $archive = $dvd->ask("Would you like to archive it now? [Y/n]", 'y'); 
     236                                $archive = strtolower($archive); 
     237                        } 
     238                         
     239                        if($archive == 'y' || $archive == 'yes' || isset($dvd->args['archive'])) { 
     240                         
     241                                if(isset($dvd->args['show'])) { 
     242                                        $show = intval($dvd->args['show']); 
     243                                         
     244                                        if($show > 0) { 
     245                                                $sql = "SELECT id, title, min_len, max_len, fps, cartoon FROM tv_shows WHERE id = $show;"; 
     246                                                $rs = pg_query($sql) or die(pg_last_error()); 
     247                                                if(pg_num_rows($rs) == 1) 
     248                                                        $dvd->tv_show = pg_fetch_assoc($rs); 
     249                                        } 
     250                                } 
     251                                 
     252                                if(!isset($dvd->tv_show['id'])) { 
     253                                 
     254                                        // Get the current TV show titles 
     255                                        $sql = "SELECT id, title, min_len, max_len, fps, cartoon FROM tv_shows ORDER BY title;"; 
     256                                        $rs = pg_query($sql) or die(pg_last_error()); 
     257                                        $num_rows = pg_num_rows($rs); 
     258                                         
     259                                        // If no rows, then we are creating a new title 
     260                                        if($num_rows == 0) { 
     261                                                $new_title = true; 
     262                                                $dvd->msg("There aren't any TV shows in the database."); 
     263                                        } 
     264                                         
     265                                        // Otherwise, display menu, let them pick the show 
     266                                        else { 
     267                                                 
     268                                                // Build associative array 
     269                                                for($x = 0; $x < $num_rows; $x++) 
     270                                                        $arr[$x] = pg_fetch_assoc($rs); 
     271                                                         
     272                                                // Split the output into pages for the terminal (24 lines per display) 
     273                                                $arr_chunk = array_chunk($arr, 22, true); 
     274                                                 
     275                                                // Keep looping through the selection until they pick one 
     276                                                do { 
     277                                                        // Display only 24 lines per selection at a time: 
     278                                                        for($x = 0, $y = 1; $x < count($arr_chunk); $x++) { 
     279                                                         
     280                                                                $dvd->msg("Current TV shows:"); 
     281                                                                for($z = 0; $z < count($arr_chunk[$x]); $z++) { 
     282                                                                        $dvd->msg("\t$y. {$arr_chunk[$x][($y - 1)]['title']}"); 
     283                                                                        $y++; 
     284                                                                } 
     285                                                                 
     286                                                                $msg = ''; 
     287                                                                if(count($arr_chunk) > 1) 
     288                                                                        $msg = "[Page ".($x + 1)."/".count($arr_chunk)."]  Select TV show [NEXT PAGE/#/new]:"; 
     289                                                                else 
     290                                                                        $msg = "Select TV show [#/new]:"; 
     291                                                                         
     292                                                                $input = $dvd->ask($msg, ''); 
     293                                                                 
     294                                                                if(strtolower(trim($input)) != 'new') 
     295                                                                        $input = intval($input); 
     296                                                                else { 
     297                                                                        $new_title = true; 
     298                                                                        break 2; 
     299                                                                } 
     300                                                                 
     301                                                                // Break out once they have their selection 
     302                                                                if($input > 0) { 
     303                                                                        if($input > $num_rows) { 
     304                                                                                $dvd->msg("Please enter a valid selection.", true); 
     305                                                                                $input = 0; 
     306                                                                        } 
     307                                                                        else 
     308                                                                                break 1; 
     309                                                                } 
     310                                                        } 
     311                                                } while($input == 0); 
     312                                                 
     313                                                // Put the selected TV show array into the object 
     314                                                $dvd->tv_show = $arr[($input - 1)]; 
     315                                        } 
     316                                         
     317                                        // Create a new TV show record 
     318                                        if($new_title === true) { 
     319                                                $dvd->lsdvd(); 
     320                                                 
     321                                                $dvd->msg(''); 
     322                                                if($dvd->debug == false) 
     323                                                        $dvd->msg("Disc Title: ".$dvd->disc_title); 
     324                                                $title = $dvd->ask("What is the title of this TV show?"); 
     325                                                $min_len = $dvd->ask("What is the minimum TV show length (in minutes)? [20]", 20); 
     326                                                $max_len = $dvd->ask("What is the maximum TV show length (in minutes)? [60]", 60); 
     327                                                $cartoon = $dvd->ask("Is this series animated? [y/N]", 0); 
     328                                                // TODO: 
     329                                                // Ask for the framerate (PAL, NTSC, Autodetect); 
     330                                                $fps = 0; 
     331                                                // Ask "is this correct" 
     332                                                 
     333                                                $dvd->addTVShow($title, $min_len, $max_len, $fps, $cartoon); 
     334                                        } 
     335                                 
     336                                } 
     337                                 
     338                                 
     339                                $dvd->msg(''); 
     340                                $dvd->msg("New disc for '".$dvd->tv_show['title']."'"); 
     341                                 
     342                                /** Disc Season */ 
     343                                 
     344                                // If they didn't pass the CLI argument, ask for it 
     345                                if(!isset($dvd->args['season']) || intval($dvd->args['season'] == 0)) { 
     346                                        do { 
     347                                                $season = $dvd->ask("What season is this disc? [1]", 1); 
     348                                                $season = intval($season); 
     349                                        } while($season == 0); 
     350                                } 
     351                                // Use the CLI variable if provided 
     352                                else { 
     353                                        $dvd->disc['season'] = $season = intval($dvd->args['season']); 
     354                                        if($dvd->debug) 
     355                                                $dvd->msg("[Debug] Season: $season"); 
     356                                } 
     357                                 
     358                                /** Disc # */ 
     359                                 
     360                                // Find out which other discs they already have archived 
     361                                $sql = "SELECT disc FROM discs WHERE tv_show = {$dvd->tv_show['id']} AND season = $season ORDER BY disc;"; 
     362                                $rs = pg_query($sql) or die(pg_last_error()); 
     363                                 
     364                                $arr = array(); 
     365                                for($x = 0; $x < pg_num_rows($rs); $x++) 
     366                                        $arr[] = current(pg_fetch_row($rs)); 
     367                                 
     368                                $list = implode(', ', $arr); 
     369                                 
     370                                // Display currently archived discs, if any 
     371                                if(count($arr) > 0) 
     372                                        $dvd->msg("Discs archived for Season $season: $list"); 
     373                                 
     374                                // See if they passed it in the CLI 
     375                                if(!isset($dvd->args['disc']) || intval($dvd->args['disc'] == 0)) { 
     376                                        // First, see if there are any other discs in the database. 
     377                                        // If there are, assume this new one is the next in line 
     378                                        // and default the answer to the incremented value. 
     379                                         
     380                                        $sql = "SELECT MAX(disc) FROM discs WHERE tv_show = {$dvd->tv_show['id']} AND season = $season;"; 
     381                                        $rs = pg_query($sql) or die(pg_last_error()); 
     382                                        if(pg_num_rows($rs) == 1) { 
     383                                                $max = current(pg_fetch_row($rs)); 
     384                                        } 
     385                                        else 
     386                                                $max = 0; 
     387                                         
     388                                        // Increment by one -- our starting point 
     389                                        $max++; 
     390                                         
     391                                        do { 
     392                                                $disc = $dvd->ask("What number is this disc? [$max]", $max); 
     393                                                $dvd->disc['number'] = $disc = intval($disc); 
     394                                                 
     395                                                if(in_array($disc, $arr)) { 
     396                                                        $dvd->msg("Disc #$disc is already archived.  Choose another number."); 
     397                                                        $disc = 0; 
     398                                                } 
     399                                                 
     400                                        } while($disc == 0); 
     401                                         
     402                                } 
     403                                else { 
     404                                        $dvd->disc['number'] = $disc = intval($dvd->args['disc']); 
     405                                        if($dvd->debug) 
     406                                                $dvd->msg("[Debug] Disc: $disc"); 
     407                                } 
     408                                 
     409                                // Archive the disc 
     410                                $dvd->msg("Archiving your DVD ..."); 
     411                                $dvd->lsdvd(); 
     412                                 
     413                                $dvd->addDisc($dvd->tv_show['id'], $season, $disc, $dvd->disc_id, $dvd->disc_title, $start); 
     414                                 
     415                                 
     416                        } 
     417                        // Just exit gracefully if they don't want to archive it 
     418                        else 
     419                                die; 
     420                } 
     421        } 
     422         
     423        die; 
     424         
    308425        // Rip DVD tracks to the harddrive 
    309426        if($dvd->args['rip'] == 1) { 
    310427 
    311428                @exec("mount /mnt/dvd 2> /dev/null;"); 
    312                 $dvd->getDisc(); 
    313  
    314                 // Set the export directory (where to save ripped files) 
    315                 $dvd->export_dir = $dvd->getExportDir($dvd->title, $dvd->season); 
    316429 
    317430                // Create the export directory if it doesn't already exist 
     
    329442                // Pull out the tracks that haven't been flagged to ignore in the database frontend 
    330443                // This query has nothing to do with what has / hasn't been encoded 
    331                 $sql_rip = "SELECT track, len FROM episodes WHERE disc = {$dvd->disc} AND ignore = FALSE ORDER BY track;"; 
    332                 $rs_rip = pg_query($sql_rip) or die(pg_last_error()); 
    333                 $num_rips = pg_num_rows($rs_rip); 
    334  
    335                 if($num_rips > 0) { 
     444                $sql = "SELECT track, len FROM episodes WHERE disc = {$dvd->disc['id']} AND ignore = FALSE ORDER BY track;"; 
     445                $rs = pg_query($sql) or die(pg_last_error()); 
     446                $num_rows = pg_num_rows($rs); 
     447 
     448                if($num_rows > 0) { 
    336449 
    337450                        // By passing the --tracks flag, you can rip certain tracks only 
  • class.dvd.php

    r4 r5  
    1313                        $this->max_len = 59; 
    1414                } 
    15  
    16                 /* 
    17                  * Archives a disc in the database 
    18                  * 
    19                  */ 
    20                 function archiveDisc() { 
    21  
    22                         if(!isset($this->args['disc']) || intval($this->args['disc']) == 0) 
    23                                 die("You need to pass a valid disc # with the --disc argument.\n"); 
    24                         if(!isset($this->args['season']) || intval($this->args['season']) == 0) 
    25                                 die("You need to pass a valid season # with the --season argument.\n"); 
    26  
    27                         $arr_insert = array( 
    28                                 'tv_show' => $this->tv_show, 
    29                                 'season' => $this->args['season'], 
    30                                 'disc' => $this->args['disc'], 
    31                                 'disc_id' => $this->disc_id, 
    32                                 'disc_title' => $this->disc_title 
    33                         ); 
    34                          
    35                         #print_r($arr_insert); 
    36                         #die; 
    37  
    38                         if(isset($this->args['chapters'])) { 
    39                                 $arr_insert['chapters'] = 't'; 
    40                                 $arr_insert['chapters_track'] = intval($this->args['tracks']); 
    41                         } 
    42  
    43                         $sql_insert = pg_insert($this->db, 'discs', $arr_insert, PGSQL_DML_STRING); 
    44  
    45                         if($sql_insert === false) { 
    46                                 print_r($arr_insert); 
    47                                 trigger_error("Cannot build query", E_USER_ERROR); 
    48                         } 
    49                         elseif(is_string($sql_insert)) { 
    50                                 decho("Query: $sql_insert"); 
    51                                 pg_query($sql_insert) or die(pg_last_error()); 
    52                                 return true; 
    53                         } 
    54                 } 
    55  
    56                 function archiveEpisodes() { 
    57  
     15                 
     16                function addTVShow($title = 'TV Show', $min_len = 30, $max_len = 60, $fps = 0, $cartoon = false) { 
     17                        $title = pg_escape_string(trim($title)); 
     18                        $min_len = intval($min_len); 
     19                        $max_len = intval($max_len); 
     20                        $fps = intval($fps); 
     21                        if(substr(trim(strtolower($cartoon)), 0, 1) == 'y') { 
     22                                $pg_cartoon = 'TRUE'; 
     23                                $cartoon = true; 
     24                        } 
     25                        else { 
     26                                $pg_cartoon = 'FALSE'; 
     27                                $cartoon = false; 
     28                        } 
     29                         
     30                        $sql = "SELECT NEXTVAL('public.tv_shows_id_seq');"; 
     31                        $id = current(pg_fetch_row(pg_query($sql))) or die(pg_last_error()); 
     32                         
     33                        $sql = "INSERT INTO tv_shows (id, title, min_len, max_len, fps, cartoon) VALUES ($id, '$title', $min_len, $max_len, $fps, $pg_cartoon);"; 
     34                        pg_query($sql) or die(pg_last_error()); 
     35                         
     36                        $this->tv_show = compact('id', 'title', 'min_len', 'max_len', 'fps', 'cartoon'); 
     37                        return true; 
     38                } 
     39                 
     40                function addDisc($tv_show, $season, $disc, $disc_id, $disc_title) { 
     41                 
     42                        if(!isset($this->arr_tracks)) 
     43                                $this->lsdvd(); 
     44                         
     45                        $arr_tracks = $this->getValidTracks($this->arr_tracks, $this->tv_show['min_len'], $this->tv_show['max_len'], false); 
     46                         
     47                        /* 
     48                        if($disc > 1) { 
     49                                $this->disc['start'] = $this->getStartingEpisode(); 
     50                        } 
     51                        else 
     52                                $this->disc['start'] = 1; 
     53                         
     54                        $start = $this->disc['start']; 
     55                        */ 
     56                                 
     57                        // Insert disc into database 
     58                        $sql = "SELECT NEXTVAL('public.discs_id_seq');";