Created
June 13, 2010 18:06
-
-
Save wilkie/436850 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use LWP::Simple; | |
use LWP::UserAgent; | |
use HTTP::Request; | |
use HTTP::Response; | |
use HTML::LinkExtor; | |
use HTML::Entities; | |
use Cwd; | |
use Roman; | |
# D:/shows/The Office/Season 3" | |
my $curdir = getcwd(); | |
$curdir =~ /\/([^\/]+)\/(?:Season|Series)\s([^\/]+$)/; | |
my $showtitle = $1; | |
my $season = $2; | |
$showtitle =~ s/\bthe\b//gi; | |
$showtitle =~ s/\s+//g; | |
$showtitle =~ s/\(/_/g; | |
$showtitle =~ s/\)//g; | |
$showtitle =~ s/\'//g; | |
my $URL = "http://epguides.com/".$showtitle."/"; | |
my $dirtarget = $curdir . "/"; | |
#mode = 0 print | |
#mode = 1 rename files | |
my $mode = 0; | |
if ($#ARGV + 1 >= 1) | |
{ | |
$mode = $ARGV[0]; | |
if ($mode > 1) | |
{ | |
$mode = 0; | |
} | |
} | |
my $browser = LWP::UserAgent->new(); | |
$browser->timeout(10); | |
my $request = HTTP::Request->new(GET => $URL); | |
my $response = $browser->request($request); | |
if ($response->is_error()) {printf "%s\n", $response->status_line;} | |
my $contents = $response->content(); | |
my @acontents = split('\n',$contents); | |
my @episodes = grep(/\b$season-\s*\d+\b/,@acontents); | |
my @epnames; | |
foreach (@episodes) | |
{ | |
if ($_ =~ />([^>]+)<\/a>\s*$/) | |
{ | |
my $epname = $1; | |
my $idx = $_; | |
$idx =~ /\b$season-\s*(\d+)\b/; | |
$idx = $1; | |
$epname = decode_entities($epname); | |
$epnames[$idx-1] = $epname; | |
} | |
} | |
opendir(IMD, $dirtarget) || die("Cannot open directory"); | |
my @thefiles= readdir(IMD); | |
for (my $i=0;$i<@epnames;$i++) | |
{ | |
my $t = $i + 1; | |
my $d = $i + 2; | |
for (my $j=0;$j<@thefiles;$j++) | |
{ | |
# if ($thefiles[$j] =~ /((?:\b$season|episode\s*|e\s*)0?$t[^\d])\b/i) | |
# | |
# @ - season number | |
# # - episode number | |
# | |
# s@@e## | |
# @## | |
# @x## | |
# @.## | |
# episode ## | |
# season @ episode ## | |
#if ($thefiles[$j] =~ /((?:[^\de]$season|episode\s*|\bs0?\Q$season\E(e|x)\s*)0?$t\b(?:([^\d]\Q$season\E?(e|x)?\s*0?$d[^\d])|[^\d]))/i) | |
if ($thefiles[$j] =~ /(s0?\Q$season\Ee0?$t(?:[^\d]|$)|(?:[^\d]|^)0?\Q$season\E0?$t(?:[^\d]|$)|(?:[^\ds]|^)0?\Q$season\E(?:x|\.)0?$t(?:[^\d]|$)|(?:\bseason\s*\Q$season\E\s*)?\bepisode\s*$t[^\d]\b)/i) | |
{ | |
my $name=""; | |
if ($t < 10) | |
{ | |
$name.= "0"; | |
} | |
$name.=$t; | |
if ($2) | |
{ | |
$name.=","; | |
if ($d <10) | |
{ | |
$name.="0"; | |
} | |
$name.= $d." - ".$epnames[$i].", ".$epnames[$i+1]; | |
} | |
else | |
{ | |
$name.=" - ".$epnames[$i]; | |
} | |
$name =~ s/[?:]//g; | |
$name =~ s/\//-/g; | |
$thefiles[$j] =~ /(\.[^.]+)$/; | |
if ($mode == 0) | |
{ | |
print $name.$1."\n"; | |
print $thefiles[$j]."\n"; | |
} | |
else | |
{ | |
rename($dirtarget.$thefiles[$j],$dirtarget.$name.$1); | |
} | |
} | |
} | |
} | |
closedir(IMD); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment