use LWP 5.64; # Loads all important LWP classes, and makes # sure your version is reasonably recent. $browser = LWP::UserAgent->new; print "\n\n\nIPlayer Refresh Cache v0.4\n"; print "**************************\n\n"; # Parameters $showPID = 'b006q2x0'; $episodePID = 'b04n1wqm'; $ProgramFiles = $ENV{'ProgramFiles(x86)'}; print "Directory for Program Files(x86): ".$ProgramFiles."\n"; $Get_iPlayerPath = $ProgramFiles.'/Get-iPlayer'; print "Directory for Get_iPlayer files: ".$Get_iPlayerPath."\n"; $PerlPath = $Get_iPlayerPath.'/Perl.exe'; print "Directory for Perl.exe: ".$PerlPath."\n"; $UserProfile = $ENV{'USERPROFILE'}; print "Directory for User Files: ".$UserProfile."\n"; $Get_iPlayer_Cache_Path = $UserProfile.'/.get_iplayer'; print "Directory for iPlayer Cache File: ".$Get_iPlayer_Cache_Path."\n"; $Refreshed_Cache = $Get_iPlayer_Cache_Path."/tv.cache"; print "Directory for Refreshed Cache File: ".$Refreshed_Cache."\n"; $index_number = 0; #Cache Format: index|type|name|pid|available|episode|seriesnum|episodenum|versions|duration|desc|channel|categories|thumbnail|timeadded|guidance|web $Cache[$index_number++] = "#index|type|name|pid|available|episode|seriesnum|episodenum|versions|duration|desc|channel|categories|thumbnail|timeadded|guidance|web\n"; open REFRESHED_CACHE_FILE, ">", $Refreshed_Cache or die "Couldn't open ".$Refreshed_Cache; print REFRESHED_CACHE_FILE $Cache[0]; #Main @all_shows = &search_all_shows; for $j (0 .. $#all_shows) { @all_episodes = &search_ePIDs_for_show($all_shows[$j]); for $i (0 .. $#all_episodes) { $name = $all_episodes[$i][2]; if (index($name, '.') != -1) { substr ($name, index($name, '.')) = ""; } if (index($name, ':') != -1) { substr ($name, index($name, ':')) = ""; } $episode = $all_episodes[$i][5]; if (index($episode, 'Series ') != -1) { if (index($episode, ':') != -1) { substr ($episode, 0, index($episode, ':')+1) = ""; } } $name = &remove_spaces($name); $episode = &remove_spaces($episode); $all_episodes[$i][3] = &remove_spaces($all_episodes[$i][3]); $all_episodes[$i][4] = &remove_spaces($all_episodes[$i][4]); $all_episodes[$i][6] = &remove_spaces($all_episodes[$i][6]); $time_added = time(); print REFRESHED_CACHE_FILE $index_number++ .'|'; #index print REFRESHED_CACHE_FILE "tv" .'|'; #type print REFRESHED_CACHE_FILE $name .'|'; #name print REFRESHED_CACHE_FILE $all_episodes[$i][0] .'|'; #ePID print REFRESHED_CACHE_FILE "Unknown" .'|'; #available print REFRESHED_CACHE_FILE $episode .'|'; #episode print REFRESHED_CACHE_FILE $index_number .'|'; #seriesnum print REFRESHED_CACHE_FILE $index_number .'|'; #episodenum print REFRESHED_CACHE_FILE "default," .'|'; #versions print REFRESHED_CACHE_FILE "Unknown" .'|'; #duration print REFRESHED_CACHE_FILE $all_episodes[$i][3] .'|'; #desc print REFRESHED_CACHE_FILE $all_episodes[$i][6] .'|'; #channel print REFRESHED_CACHE_FILE '|'; #categories print REFRESHED_CACHE_FILE $all_episodes[$i][4] .'|'; #thumbnail print REFRESHED_CACHE_FILE $time_added .'|'; #timeadded print REFRESHED_CACHE_FILE '|'; #guidance print REFRESHED_CACHE_FILE "http://www.bbc.co.uk/iplayer/episodes/".$all_episodes[$i][1].'|'; #web print REFRESHED_CACHE_FILE "\n"; #new line } } close REFRESHED_CACHE_FILE; sub search_all_shows { my (@ops) = @_; my $i = 0; my @all_shows; my @AtoZIndex = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0-9'); print "\n\n\nSearching for list of all TV shows\n\n"; print "Search Results:\n"; for my $j (0 .. $#AtoZIndex) { my $url = "http://www.bbc.co.uk/iplayer/a-z/".$AtoZIndex[$j]; print '"'.$AtoZIndex[$j].'" shows have been searched.'."\n"; my $result = $browser->get($url); die "Can't GET $url" if (! defined $result); my $html = $result->content; while ($html =~ /\/iplayer\/brand\//) { #print "Found Show - ", ; #my $sPID = substr $html, index($html, '/iplayer/brand/') + 15, 8; #print "$sPID\n"; $all_shows[$i++] = substr $html, index($html, '/iplayer/brand/') + 15, 8; substr ($html, index($html, '/iplayer/brand/'), 13) = ""; } } print "\nFound ".$i." shows\n\n\n"; return @all_shows; } sub search_ePIDs_for_show { my (@ops) = @_; my $i = 0; my @all_episodes; #print "\n\n\nSearching for episodes (sPID: $ops[0])\n\n"; #print "Search Results:\n"; my $url = "http://www.bbc.co.uk/iplayer/episodes/$ops[0]"; my $result = $browser->get($url); die "Can't GET $url" if (! defined $result); my $html = $result->content; my $html_1 = $html; my $html_2 = $html; my $html_3 = $html; my $html_4 = $html; my $html_5 = $html; $channel = &extract_string($html_1, 'class="master-brand">', ''); #print "Channel:".$channel."*\n"; while ($html =~ /\/iplayer\/episode\//) { #print "Found PID - ", ; my $ePID = substr $html, index($html, '/iplayer/episode/') + 17, 8; $all_episodes[$i][0] = $ePID; #store ePID print $all_episodes[$i][0]." - "; $all_episodes[$i][1] = $ops[0]; #store sPID #print $all_episodes[$i][1]." - "; substr ($html_2, 0, index($html_2, 'data-ip-id="') + 12) = ""; $all_episodes[$i][2] = &extract_string($html_2, 'title="', '"'); print $all_episodes[$i][2]."\n"; substr ($html_3, 0, index($html_3, 'data-ip-id="') + 12) = ""; $all_episodes[$i][3] = &extract_string($html_3, 'class="synopsis">', '

'); #print $all_episodes[$i][3]."\n"; substr ($html_4, 0, index($html_4, 'data-ip-id="') + 12) = ""; $all_episodes[$i][4] = &extract_string($html_4, 'data-ip-src="', '"'); #print $all_episodes[$i][4]."\n"; substr ($html_5, 0, index($html_5, 'data-ip-id="') + 12) = ""; $all_episodes[$i][5] = &extract_string($html_5, 'class="subtitle">', ''); #print $all_episodes[$i][5]."\n\n"; $all_episodes[$i++][6] = $channel; substr ($html, index($html, '/iplayer/episode/'), 15) = ""; } #print "Found ".(($#all_episodes)+1)." episode(s)\n\n"; return @all_episodes,; } sub remove_spaces { #remove spaces at the end and beginning of a string my (@ops) = @_; $out_string = $ops[0]; $out_string =~ s/^\s+|\s+$//g; return $out_string; } sub extract_string { #remove substring from a string my (@ops) = @_; my $out_string = ""; if (index($ops[0], $ops[1]) != -1) { if (index($ops[0], $ops[2]) != -1) { my $start_index = index($ops[0], $ops[1]) + length($ops[1]); substr ($ops[0], 0, $start_index) = ""; $out_string = substr ($ops[0], 0, index($ops[0], $ops[2])); } } return $out_string; }