Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created June 3, 2009 06:35
Show Gist options
  • Save miyagawa/122822 to your computer and use it in GitHub Desktop.
Save miyagawa/122822 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw(basename);
use LWP::Simple;
for my $file (@ARGV) {
my $meta = meta_file($file);
if (my $info = parse_info($file)) {
write_meta($meta, $info);
fetch_cover_art($info->{series});
} else {
warn "Can't get info from $file\n";
}
}
sub meta_file {
my $file = shift;
$file =~ s/\.\w+$/.xml/;
return $file;
}
sub parse_info {
my $file = basename(shift);
$file =~ s/^\[.*?\]\s*//;
if ($file =~ s/\s+\-\s+(\d+)\s+RAW.*$//i) {
return {
series => $file,
episode => $1 + 0,
};
} elsif ($file =~ s/\s+ep(\d+)\s+.*$//i) {
return {
series => $file,
episode => $1 + 0,
};
}
return;
}
sub encode_xml {
local $_ = shift;
s/&/&/g;
s/</&lt;/g;
s/>/&gt;/g;
s/"/&quot;/g;
return $_;
}
sub write_meta {
my($meta, $info) = @_;
return if -e $meta;
open my $fh, ">", $meta or die "$meta: $!";
print $fh <<XML;
<media>
<seriesName>@{[ encode_xml($info->{series}) ]}</seriesName>
<episodeNumber>1@{[ sprintf '%02d', $info->{episode} ]}</episodeNumber>
<season>1</season>
<episode>@{[ $info->{episode} ]}</episode>
</media>
XML
}
sub fetch_cover_art {
my $series = shift;
my $dir = "$ENV{HOME}/Library/Application Support/Sapphire/Collection Art/\@TV/$series";
mkdir $dir, 0777 unless -e $dir;
my $cover = "$dir/cover.jpg";
return if -e $cover;
print "Cover URL for $series: ";
chomp(my $url = <STDIN>);
if ($url) {
LWP::Simple::mirror($url, $cover);
warn "Downloaded $cover\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment