#!/usr/bin/php like )?(?P.*?) after ((s|season |series )(?P\d+) ?)?(e|episode )?(?P\d+)/i", $arg, $m ) ){ if( $arg == "" ){ fwrite( STDERR, "ERR: Didn't understand input: $arg\n" /*e.g. record_schedule Have I Got News for You after series 41 episode 3\n". "record_schedule Hollyoaks after episode 3245\n". "record_schedule The Simpsons after s40e12\n"*/ ); exit; } preg_match( "/(?Plike )?(?P.+)/i", $arg, $m ); $show = trim( $m["show"] ); $series = ""; $episode = ""; $fuzzy = $m["regexp"] != ""; }else{ $show = trim( $m["show"] ); $series = $m["series"]; $episode = $m["episode"]; $fuzzy = $m["regexp"] != ""; } if( !$show ) exit; $xml = simplexml_load_file( LISTINGS_PATH ); // List of shows which benefit from a date subtitle being added $aAddDateSubtitle = array( "Hollyoaks", "EastEnders" ); if( $debug ) if( $fuzzy ) echo "Fuzzy\n"; // Get list of shows $aShows = array(); foreach( $xml->programme as $programme ){ // Match or fuzzy match show names if( !$fuzzy && strcasecmp( $programme->title, $show ) != 0 ) continue; if( $fuzzy && !preg_match( "/$show/i", $programme->title ) ) continue; if( $debug ) echo $programme->title."\n"; $ep = parseEpisodeNum( $programme->{'episode-num'} ); if( $debug ) echo $programme->title." (".date("Y-m-d H:i:s", strtotime( $programme->attributes()->start )).") - ".$ep["episode"]."\n"; // No good finding programmes that have already aired if( strtotime( $programme->attributes()->start ) < time() ){ if( $debug ) echo "Skipping ".$programme->title." as its in the past (".date("Y-m-d H:i:s", strtotime( $programme->attributes()->start )).")\n"; continue; } if( $series != "" && $series > $ep["series"] ){ if( $debug ) echo "Skipping ".$programme->title." as it's a previous series to the one we're looking for\n"; continue; } if( $episode != "" && $episode >= $ep["episode"] && ($series == "" || $series == $ep["series"])){ if( $debug ) echo "Skipping ".$programme->title." as it's a previous episode number to ones we're looking for\n"; continue; } if( in_array( $programme->title, $aAddDateSubtitle ) ){ $programme->{'sub-title'} = date( "Y-m-d l", strtotime( $programme->attributes()->start ) ); } $aShows[] = $programme; } if( sizeof( $aShows ) == 0 ) exit; if( $debug ) print_r( $aShows ); echo "\n\n# Episodes of $show after "; if( $series != "" ) echo "series $series "; echo "episode $episode\n"; // Reorder usort( $aShows, "episodeCompare" ); // Remove duplicates for( $i=0; $i{'episode-num'} ) == trim( $last->{'episode-num'} ) ){ $aShows[$i] = null; } $last = $p; } // Create channel names lookup $aChannels = array(); foreach( $xml->channel as $channel ){ $ch = trim($channel->attributes()->id); $name = trim( $channel->{'display-name'} ); $name = preg_replace( "/BBC (One|Two) (Oxford|England)/", "BBC $1", $name ); $name = preg_replace( "/(ITV1)([a-zA-Z ]+)/", "$1", $name ); $aChannels[$ch] = $name; } // Output list of shows foreach( $aShows as $p ){ if( $p == null ) continue; $ch = trim( $p->attributes()->channel ); if( !isset( $aChannels[$ch] ) ) continue; $ep = parseEpisodeNum( $p->{'episode-num'} ); echo date( "i H d m * ", strtotime( $p->attributes()->start ) ); echo USER." "; echo RECORD_PATH." "; $cmd = $p->title." "; if( $ep["series"] != "" ) $cmd .= "s".$ep["series"]; if( $ep["episode"] != "" ) $cmd .= "e".$ep["episode"]; if( isset( $p->{'sub-title'} ) ) $cmd .= " ".$p->{'sub-title'}; $cmd .= " on ".$aChannels[$ch]; // Length $len = ceil( (strtotime( $p->attributes()->stop ) - strtotime( $p->attributes()->start )) / 60 ); $cmd .= " for ".$len; echo escapeshellarg( $cmd ); // echo " # ".$p->desc; echo "\n"; } function episodeCompare( $a, $b ){ $epa = parseEpisodeNum( $a->{'episode-num'} ); $epb = parseEpisodeNum( $b->{'episode-num'} ); // Series sort if( $epa["series"] > $epb["series"] ) return 1; if( $epa["series"] < $epb["series"] ) return -1; // Episode sort if( $epa["episode"] > $epb["episode"] ) return 1; if( $epa["episode"] < $epb["episode"] ) return -1; // Air time compare $sta = strtotime( $a->attributes()->start ); $stb = strtotime( $b->attributes()->start ); if( $sta > $stb ) return 1; if( $sta < $stb ) return -1; return 0; } function parseEpisodeNum($num){ if( !preg_match( "/(?P[^\.]*)\.(?P[^\.\/]+)(\/(?P\d+))?/", $num, $m ) ) return false; return array( "series" => (int)$m["series"]+1, "episode" => (int)$m["episode"]+1, "totalepisodes" => (int)$m["totalepisodes"] ); } ?>