Project

General

Profile

Feature #2584 » episodes.pl

Rob vh, 2015-01-04 14:48

 
1
use strict;
2
use warnings;
3
use JSON qw(decode_json);
4
use File::Slurp;
5
use File::Copy;
6

    
7
my $renamed = 0;
8
my $dir = "/home/hts/.hts/tvheadend/dvr/log";
9
opendir(DIR,$dir) or die "Cannot open " . $dir;
10
while (my $file = readdir(DIR)) {
11
	next if ($file =~ m/^\./);
12
	next unless (-f "$dir/$file");
13
	my $recording = decode_json read_file "$dir/$file";
14
	my $desc = $recording->{"description"}->{"dut"};
15
	next unless ($desc);
16
	my $suffix = "";
17
# (s 1/afl 2)
18
	my ($s,$e) = ($desc =~ /\(s\ ([0-9]*)\/afl\ ([0-9]*)\)/);
19
	if ($e) {
20
		$suffix = "s" . $s . "e" . ($e > 9 ? $e : "0" . $e);
21
	} else {
22
# (s 1)
23
		($s) = ($desc =~ /\(s\ ([0-9]*)\)/);
24
		if ($s) {
25
			$suffix = "s" . $s;
26
		} else {
27
# (afl 2)
28
			($e) = ($desc =~ /\(afl\ ([0-9]*)\)/);
29
			if ($e) {
30
				$suffix = "e" . ($e > 9 ? $e : "0" . $e);
31
			} else {
32
# Episode 2
33
				($e) = ($desc =~ /Episode\ ([0-9]*)/);
34
				if ($e) {
35
					$suffix = "e" . ($e > 9 ? $e : "0" . $e);
36
				}
37
			}
38
		}
39
	}
40
        next unless ($suffix);
41
	$suffix = "." . $suffix . ".";
42
	my $fn = $recording->{"filename"};
43
	next if (!$fn || ($fn =~ /\.s[0-9]+e[0-9]+\./) || ($fn =~ /\.S[0-9]+e[0-9]+\./) || index($fn,$suffix)>0);
44
	my ($before,$after) = ($fn =~ /(.*)\.([a-z]*)$/);
45
	my $newfn = $before . $suffix . $after;
46
        move $fn, $newfn or print "Rename failed for $newfn \n";
47
	$renamed ++;
48
}
49
closedir(DIR);
50
print "Renamed $renamed files. \n";
51
exit(0);
(1-1/2)