]> git.rmz.io Git - dotfiles.git/blob - bin/renameseries
nvim: add FPP copyright snippet
[dotfiles.git] / bin / renameseries
1 #! /usr/bin/perl
2
3 use warnings;
4 use strict;
5 # getopts module
6 use Getopt::Std;
7 # getstore madule
8 use LWP::Simple;
9 # Curent Working Directory module
10 use Cwd;
11 use HTML::Entities;
12 #use utf8;
13
14 # eguides URL
15 my $url = 'http://epguides.com';
16
17 # get options
18 our($opt_n, $opt_s, $opt_h);
19 getopts('ns:h') or $opt_h = 1; # sets opt_n to true if -n is given
20 #+or dies when options are unknown
21
22 if ($opt_h) {
23 print "Usage: renameseries [OPTION]... [DIRECTORY]\n";
24 print " or: renameseries [OPTION]... -s SERIE [DIRECTORY]\n";
25 print "Rename all files in DIRECTORY to the name downloaded from epguides.com\n";
26 exit 0;
27 }
28
29 my $path = getcwd();
30 #print $path;
31 my $series_name;
32 if ($opt_s) {
33 $series_name = $opt_s;
34 } else {
35 $path .= @ARGV==1?"/$ARGV[0]":"";
36 # $path =~ /.*\/(.*?)(?: *\[.*?\])*(?:\/Season \d+)/;
37 my @paths = split("\\/",$path);
38 foreach my $p (reverse(@paths)) {
39 if ($p =~ /Season|Series/ || !$p ) {
40 next;
41 }
42 $series_name = $p;
43 # print "$p\n";
44 last;
45 }
46 # $series_name = $1 or die("Could not find Series name in path");
47 # print $series_name . "\n";
48
49 # substitute 'The' at the beginning
50 #or substitute any of the folowing characters '-' ',' '.' ' '
51 #or substitute [.*]
52 $series_name =~ s/(^The|[- ,\.]|\[.*\])//g;
53 }
54 #print $series_name;
55 $path = @ARGV==1?"./$ARGV[0]":"./";
56
57 my $tmp = '/tmp/renameseries';
58 getstore( "$url/$series_name", $tmp);
59 open FILE, $tmp or die "Could not download '$series_name' from '$url'";
60 my @lines = <FILE>;
61 close FILE;
62 unlink($tmp);
63
64 #use diagnostics;
65 #my @old_file_names = <"$path". *>;
66 opendir(my $dh , $path) or die "Couldn't open dir '$path': $!";
67 my @old_file_names;# = readdir $dh;
68
69 @old_file_names = grep { /^/ && -f "$path/$_" } readdir($dh);
70
71 #foreach my $old_file_name (@old_file_names) {
72 # print $old_file_name . "\n";
73 #}
74
75 if ($opt_n) {
76 print "Path : " . $path . "\n";
77 }
78 foreach my $old_file_name (@old_file_names) {
79 # print $old_file_name . "\n";
80 if ($old_file_name =~ /0?(?<season>\d+).*?0?(?<episode>\d+)(.*\.(?<extension>.*)|.*)/) {
81 # print $+{episode} . "\n";
82 my $episode = length($+{episode}) == 1 ? '0' . $+{episode} : $+{episode};
83 my $pattern = '(0?'.$+{season}.')-([0 ]?'.$+{episode}.').*?>(.*?) *<\/a>';
84 my $extension = $+{extension} ? $+{extension} : "avi";
85 foreach my $line (@lines) {
86 if ($line =~ /$pattern/) {
87 my $new_file_name = $1 . "x" . $episode . " " .$3 . ".$extension";
88 decode_entities($new_file_name);
89 # print "pattern -> " . $pattern . "\n";
90 if ($new_file_name eq $old_file_name) {
91 last;
92 }
93 if ($opt_n) {
94 print $old_file_name . " -> " . $new_file_name . "\n";
95 } else {
96 rename("$path/$old_file_name", "$path/$new_file_name");
97 }
98 last;
99 }
100 }
101 }
102 }