]>
git.rmz.io Git - dotfiles.git/blob - bin/renameseries
   9 # Curent Working Directory module 
  15 my $url = 'http://epguides.com'; 
  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 
  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"; 
  33         $series_name = $opt_s; 
  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 ) { 
  46 #       $series_name = $1 or die("Could not find Series name in path"); 
  47 #       print $series_name . "\n"; 
  49 #       substitute 'The' at the beginning 
  50 #or     substitute any of the folowing characters '-' ',' '.' ' '  
  52         $series_name =~ s/(^The|[- ,\.]|\[.*\])//g; 
  55 $path = @ARGV==1?"./$ARGV[0]":"./"; 
  57 my $tmp = '/tmp/renameseries'; 
  58 getstore
( "$url/$series_name",  $tmp); 
  59 open FILE
, $tmp or die "Could not download '$series_name' from '$url'"; 
  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; 
  69 @old_file_names = grep { /^/ && -f 
"$path/$_" } readdir($dh); 
  71 #foreach my $old_file_name (@old_file_names) { 
  72 #       print $old_file_name . "\n"; 
  76         print "Path : " . $path . "\n"; 
  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) { 
  94                                         print $old_file_name . " -> " . $new_file_name . "\n"; 
  96                                         rename("$path/$old_file_name", "$path/$new_file_name");