Skip to content

Instantly share code, notes, and snippets.

@voodoojello
Created May 31, 2011 14:24
Show Gist options
  • Save voodoojello/1000589 to your computer and use it in GitHub Desktop.
Save voodoojello/1000589 to your computer and use it in GitHub Desktop.
Video Library (Re)Naming Via TVDB
#!/usr/bin/perl -w
#
# File: tvdb-ren.pl
# App Name: Video Library (Re)Naming Via TVDB
# App Version: 0.0.0.1a
# Created: Fri Apr 22 22:16:59 CDT 2011
# Modified: Tue May 31 09:17:03 CDT 2011
# Authored: Mark Page [[email protected]]
# File Version: 0.0.0.1A
#
#
use strict;
use warnings;
use XML::Simple;
use LWP::Simple qw(!head);
use Data::Dumper;
my $api_key = 'XXXXXXXXXXXXXXXX';
my @f_types = ('avi','mov','m4v','m4p','mp4');
my @l_files = ();
unless (@ARGV) {
print "\n >> Nothing to do!\n";
print "\nVideo Library (Re)Naming Via TVDB\n\n";
print "\tUsage: tvdb-ren.pl [-debug] [SeriesName (SeriesID)]\n\n";
}
else {
my ($seriesid,$debug);
my $media_root = '.';
my $search = join(' ',@ARGV);
my $xmlinit = new XML::Simple( suppressempty => 1, keyattr => 'list', );
if ($search =~ m/-debug/i) {
$search =~ s/-debug//; $search =~ s/^\s+//; $search =~ s/\s+$//;
$debug = 1;
}
if ($search !~ m/[a-zA-Z]/) {
$seriesid->{Series}->{seriesid} = $search;
}
else {
$seriesid = $xmlinit->XMLin(LWP::Simple::get('http://www.thetvdb.com/api/GetSeries.php?seriesname=' . $search));
}
if (ref($seriesid->{'Series'}) eq 'ARRAY') {
print "\n" . 'Multiple matches found:' . "\n\n";
for my $s (0 .. (scalar(@{$seriesid->{'Series'}}) - 1)) { print "\t" . ($s + 1) . ': ' . $seriesid->{'Series'}[$s]->{'SeriesName'} . ' [' . $seriesid->{'Series'}[$s]->{'seriesid'} . ']' . "\n"; }
print "\n\n";
}
elsif ($seriesid->{Series}->{seriesid}) {
my $mirrorpath = $xmlinit->XMLin(LWP::Simple::get('http://www.thetvdb.com/api/' . $api_key . '/mirrors.xml'));
my $seriesdata = $xmlinit->XMLin(LWP::Simple::get($mirrorpath->{'Mirror'}->{'mirrorpath'} . '/' . $api_key . '/series/' . $seriesid->{Series}->{seriesid} . '/all/en.xml'));
(my $seriesname = $seriesdata->{'Series'}->{'SeriesName'}) =~ s/\<|\>|\:|\"|\/|\\|\||\?|\*//g;
if ($debug) { die LWP::Simple::get($mirrorpath->{'Mirror'}->{'mirrorpath'} . '/' . $api_key . '/series/' . $seriesid->{Series}->{seriesid} . '/all/en.xml'); }
for my $t (0 .. (scalar(@f_types) - 1)) {
my $find = 'find "' . $media_root . '" -iname "*.' . $f_types[$t] . '"';
push(@l_files,`$find`);
}
chomp(@l_files);
my $script_path = $media_root . '/ren.sh';
my $nfo_path = $media_root . '/tvshow.nfo';
print "\n >> Checking " . scalar(@l_files) . " files...\n\n";
open(SCRIPT,'>',$script_path);
for my $e (0 .. (scalar(@{$seriesdata->{'Episode'}}) - 1)) {
my @output = ();
if ($seriesdata->{'Episode'}[$e]->{'EpisodeName'}) {
$output[0] = $seriesname;
$output[1] = $seriesdata->{'Episode'}[$e]->{'EpisodeName'};
$output[1] =~ s/^\s+//; $output[1] =~ s/\s+$//; $output[1] =~ s/\//\+/;
$output[1] =~ s/\<|\>|\:|\"|\/|\\|\||\?|\*//g;
my $alt_epid = $seriesdata->{'Episode'}[$e]->{'SeasonNumber'} . 'x' . $seriesdata->{'Episode'}[$e]->{'EpisodeNumber'};
if (length(int($seriesdata->{'Episode'}[$e]->{'SeasonNumber'})) < 2) {
$seriesdata->{'Episode'}[$e]->{'SeasonNumber'} = '0' . $seriesdata->{'Episode'}[$e]->{'SeasonNumber'};
}
if (length(int($seriesdata->{'Episode'}[$e]->{'EpisodeNumber'})) < 2) {
$seriesdata->{'Episode'}[$e]->{'EpisodeNumber'} = '0' . $seriesdata->{'Episode'}[$e]->{'EpisodeNumber'};
}
my $epid = 'S' . $seriesdata->{'Episode'}[$e]->{'SeasonNumber'} . 'E' . $seriesdata->{'Episode'}[$e]->{'EpisodeNumber'};
$output[2] = $epid;
if ($seriesdata->{'Episode'}[$e]->{'FirstAired'}) {
$output[2] .= ' (' . substr($seriesdata->{'Episode'}[$e]->{'FirstAired'},0,4) . ')';
}
for my $f (0 .. (scalar(@l_files) - 1)) {
my $ext = substr($l_files[$f],length($l_files[$f]) - 4,4);
my $new_name = join(' - ',@output) . $ext;
if ($l_files[$f] =~ m/$epid|$alt_epid/i) {
print SCRIPT 'mv "' . $l_files[$f] . '" "' . join(' - ',@output) . $ext . '"' . "\n";
}
}
}
}
close(SCRIPT);
print " >> Writing NFO file: \"$nfo_path\"...\n\n";
open(NFO,'>',"$nfo_path");
print NFO '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>' . "\n";
print NFO '<tvshow>' . "\n";
print NFO ' <title>' . $seriesdata->{'Series'}->{'SeriesName'} . '</title>' . "\n";
print NFO ' <episodeguideurl>http://thetvdb.com/?tab=series&id=' . $seriesid->{Series}->{seriesid} . '</episodeguideurl>' . "\n";
print NFO '</tvshow>' . "\n";
close(NFO);
print " >> Inspect and execute \"$script_path\" to complete...\n\n";
}
else {
die "\n" . ' >> No match found for "' . $search . '"...' . "\n\n";
}
}
exit;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment