Skip to content

Instantly share code, notes, and snippets.

@bradk
Forked from wilkie/episode_renamer.pl
Created February 18, 2010 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradk/307373 to your computer and use it in GitHub Desktop.
Save bradk/307373 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/perl
# AUTHORS: Dave Wilkinson and Bradley D. Kuhlman
# PURPOSE: Will rename media files consisting of episodes of a
# television series by using the epguides.com website from
# some cryptic, yet commonly found when downloaded on the
# internet, format to be `## - Title` where ## is the episode
# number.
# LIMITATIONS:
# - If it isn't on the website, it doesn't know what to do, so
# it gives up.
# - The show title should be the same as expected by the website
# - Has to follow the file hierarchy discussed in ASSUMPTIONS
# USAGE: Run in the directory of the season you will to rename
# ./episode_renamer.pl
# Lists the files it will rename
# ./episode_renamer.pl 1
# Will commit the changes
# ASSUMPTIONS:
# Your file hierarchy is like this:
# /.../Show Title/Season 1/
# OR
# /.../Show Title/Series 1/
# and you run the script from that directory
use strict;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use HTML::LinkExtor;
use HTML::Entities;
use Cwd;
# Get the current directory
my $curdir = getcwd();
# Figure out the show title and the season number
$curdir =~ /\/([^\/]+)\/(?:Season|Series)\s([^\/]+$)/;
my $showtitle = $1;
my $season = $2;
# The website we are using expects some changes for the url
$showtitle =~ s/\s+//g;
$showtitle =~ s/the//gi;
$showtitle =~ s/\(/_/g;
$showtitle =~ s/\)//g;
$showtitle =~ s/\'//g;
# figure out URL
my $URL = "http://epguides.com/".$showtitle."/";
my $dirtarget = $curdir . "/";
#mode = 0 print
#mode = 1 rename files
my $mode = 0;
# Check for the argument to commit
if ($#ARGV + 1 >= 1) {
$mode = $ARGV[0];
if ($mode > 1) {
$mode = 0;
}
}
# Download the episode guide
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);
# Grab every line pertaining to the current season
my @episodes = grep(/\b$season-\s*\d+\b/,@acontents);
my @epnames;
# collect all episodes in this season
foreach (@episodes) {
if ($_ =~ />(.+)</) {
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);
# loop through every episode in the season
for (my $i=0;$i<@epnames;$i++) {
my $t = $i +1;
my $d = $i +2;
# go through each file, if the file matches the episode, rename it
for (my $j=0;$j<@thefiles;$j++) {
#if ($thefiles[$j] =~ /((?:\b$season|episode\s*|e\s*)0?$t[^\d])\b/i)
if ($thefiles[$j] =~ /((?:\b$season|episode\s*|\bs0?\Q$season\Ee\s*)0?$t(?:[^\de-]|(-?e\s*0?$d[^\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";
}
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