]> git.rmz.io Git - dotfiles.git/blob - bin/renameseries.cron
lazyvim: absorb Snacks utils
[dotfiles.git] / bin / renameseries.cron
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 # getopts module
7 use Getopt::Std;
8 # getstore module
9 use LWP::Simple;
10 use HTML::Entities;
11 use File::Copy;
12 use File::Basename;
13 use File::Path qw(make_path remove_tree);
14 #use File::Glob ':glob';
15
16 # forward declaration
17 sub parse_file;
18 sub parse_dirs;
19
20 # get options
21 our($opt_n, $opt_h);
22 getopts('nh') or $opt_h = 1; # sets opt_n to true if -n is given
23 #+or dies when options are unknown
24
25 if ($opt_h) {
26 print "Usage: renameseries [OPTION]... SOURCE DESTINATION\n";
27 print "Rename all files in SOURCE to the name downloaded from epguides.com then copy ".
28 "the files to DESTINATION.\n";
29 print "\n";
30 print " -n will only print what would happen, doesn't change anything\n";
31 print " -h shows this help message\n";
32 exit 0;
33 }
34
35 @ARGV == 2 or die;
36
37 my $url = 'http://epguides.com';
38 my $src = $ARGV[0];
39 my $dst = $ARGV[1];
40
41 parse_dirs($src);
42
43 sub parse_dirs()
44 {
45 my $dir = $_[0];
46 opendir(my $dh, $dir) or die "Couldn't open dir '$dir': $!";
47 my @files = readdir $dh;
48 closedir $dh;
49
50 foreach my $file (@files) {
51 next if $file =~ /^\./;
52 my $path = "$dir/$file";
53 if (-d $path) {
54 parse_dirs($path);
55 } else {
56 parse_file($dir,$file);
57 }
58 }
59 }
60
61 sub parse_file
62 {
63 my $path = $_[0]."/".$_[1];
64 my $file = $_[1];
65
66 if ($file =~ /(?<name>.*?)\.S?0*(?<season>\d+)(E|x)0*(?<episode>\d+).*\.(?<extension>.*)/i) {
67 if ($opt_n) {
68 print "Parsing '$file\n'";
69 }
70 my $season = $+{season};
71 my $episode = length($+{episode}) == 1 ? '0' . $+{episode} : $+{episode};
72 my $pattern = '(0?'.$+{season}.')-([0 ]?'.$+{episode}.').*?>(.*?) *<\/a>';
73 my $extension = $+{extension} ? lc($+{extension}) : "avi";
74 (my $name = $+{name}) =~ s/\./ /g;
75
76 (my $series_name = $name) =~ s/^The //i;
77 $series_name =~ s/ //g;
78
79 # fix for Doctor Who
80 $series_name =~ s/(DoctorWho)(2005)/$1_$2/;
81 $series_name =~ s/(Doctor.Who).(2005)/$1_$2/;
82 $name =~ s/(Doctor Who) (2005)/$1 ($2)/;
83
84 my $tmp = '/tmp/renameseries';
85 if ($opt_n) { print "Downloading '$url/$series_name'\n"; }
86 getstore("$url/$series_name", $tmp);
87 open FILE, $tmp or die "Could not download '$series_name' from '$url'";
88 my @lines = <FILE>;
89 close FILE;
90 unlink($tmp);
91
92 foreach my $line (@lines) {
93 if ($line =~ /$pattern/) {
94 my $new_file_name = "$1x$episode $3.$extension";
95 if ($opt_n) { print "New Filename '$new_file_name'\n"; }
96 decode_entities($new_file_name);
97
98 my $new_path = "$dst/$name/Season $1";
99 make_path("$new_path");
100 $new_path = "$new_path/$new_file_name";
101
102 if (-e $new_path) {
103 if ($opt_n) {
104 print "File exists '$new_path'\n";
105 print "Unlink '$file'\n";
106 } else {
107 unlink("$path") or die "Couldn't unlink '$_[0]': $!";
108 }
109 } else {
110 if (!$opt_n) {
111 copy ("$path", "$new_path");
112 }
113 print "Copied \'$file\' \n=> \'$new_path\'\n";
114 }
115 last;
116 }
117 }
118 }
119 }