Last active
August 29, 2015 13:55
-
-
Save chrisruffalo/8708875 to your computer and use it in GitHub Desktop.
Helper for renaming recalcitrant movie files.
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 -w | |
use File::Basename; | |
use IMDB::Film; | |
use XML::Writer; | |
use IO::File; | |
use String::Util qw(trim); | |
@files = (<*.avi>, <*.mkv>, <*.mp4>); | |
@suffixlist = (".avi", ".mkv", ".mp4"); | |
foreach $file (@files) { | |
($name, , $suffix) = fileparse($file, @suffixlist); | |
# substitute out spaces for imdb matching | |
my $imdb_name = $name; | |
$imdb_name =~ s/\./ /g; | |
# remove and match year | |
$imdb_name =~ s/([1-2][0-9][0-9][0-9])//g; | |
$found_year = $1; | |
#trim values | |
$imdb_name = trim($imdb_name); | |
# print values | |
if(defined($found_year)) { | |
$found_year = trim($found_year); | |
print "Processing: ${imdb_name} from ${found_year} (${file})\n"; | |
} else { | |
print "Processing: ${imdb_name} (${file})\n"; | |
} | |
my %imdbArguments = ('crit' => "$imdb_name"); | |
if(defined($found_year)) { | |
$imdbArguments{'year'} = $found_year; | |
} | |
my $imdbObj = new IMDB::Film(%imdbArguments); | |
my $title = $imdbObj->title(); | |
# re-query with file name if title is not found | |
# which is good for some files that may have some | |
# periods that aren't for replacing spaces | |
if(!defined($title)) { | |
$imdbArguments{'crit'} = "$name"; | |
$imdbObj = new IMDB::Film(%imdbArguments); | |
} | |
my $year = $imdbObj->year(); | |
my $id = $imdbObj->id(); | |
my $nfo = "${name}.nfo"; | |
# delete old nfo if any exists | |
unlink($nfo); | |
my $output = IO::File->new(">${nfo}"); | |
my $writer = XML::Writer->new(OUTPUT => $output); | |
$writer->startTag("movie"); | |
my $url; | |
if(defined($id)) { | |
$id = "tt${id}"; | |
$url = "http://www.imdb.com/title/${id}/"; | |
print "Id: $id\n"; | |
$writer->startTag("id"); | |
$writer->characters("${id}"); | |
$writer->endTag("id"); | |
$writer->startTag("imdb_id"); | |
$writer->characters("${id}"); | |
$writer->endTag("imdb_id"); | |
$writer->startTag("imdb"); | |
$writer->characters("${id}"); | |
$writer->endTag("imdb"); | |
} | |
if(defined($title)) { | |
print "Title: $title\n"; | |
$writer->startTag("title"); | |
$writer->characters("${title}"); | |
$writer->endTag("title"); | |
} | |
if(defined($year)) { | |
print "Year: $year\n"; | |
$writer->startTag("year"); | |
$writer->characters("${year}"); | |
$writer->endTag("year"); | |
} | |
$writer->endTag("movie"); | |
$writer->end(); | |
# write scrape url to the end of the file on another line | |
if(defined($url)) { | |
print $output "${url}"; | |
} | |
$output->close(); | |
# check name, fix with year | |
if(defined($year) && !($name =~ /$year/)) { | |
my $newFile = "${name}.${year}${suffix}"; | |
print "Renaming to: $newFile\n"; | |
rename($file,$newFile); | |
# move nfo too | |
my $newNfo = "${name}.${year}.nfo"; | |
rename($nfo,$newNfo); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment