Updated for Ubuntu 22.04. Also fixed merge videos cmin check
[videoscripts/.git] / remove_empty_tv_dirs
1 #!/usr/bin/perl
2 # Author: Alan J. Pippin
3 # Description: Find and remove TV recording directories that don't contain any recordings in them
4
5 use File::Copy;
6 use File::Basename;
7 use Getopt::Std;
8 use File::stat;
9 use Time::localtime;
10 use File::Pid;
11 use Data::Dumper;
12
13 ####################################################################################################
14 # Configuration parameters
15 my $tv_src_dir = "/tvpool/tv";
16 my $log = "/var/log/organize/remove_empty_tv_dirs.log";
17 my $tv_file_ext = "-iregex \".*\.mov\" -o -iregex \".*\.avi\" -o -iregex \".*\.mp4\" -o -iregex \".*\.mpg\" -o -iregex \".*\.ts\" -o -iregex \".*\.mkv\"";
18 my $find_tv_show_dirs = "find \"$tv_src_dir/\" -maxdepth 1 -cmin +14400 -type d";
19 my $find_tv_season_dirs = "find \"$tv_src_dir/\" -maxdepth 2 -mindepth 2 -cmin +14400 -type d";
20 my $find_tv_shows = "find \"$tv_src_dir/\" -cmin +14400 $tv_file_ext";
21
22 ####################################################################################################
23 # Find empty TV show directories to remove
24
25 my %tv_show_dirs;
26
27 # Find all of our TV season dirs and store them in a hash
28 foreach $dir (`$find_tv_season_dirs`) {
29     chomp($dir);
30     my $season = basename($dir);
31     my $show = basename(dirname($dir));
32     #print "-> Found dir $dir show $show season $season\n";
33     $tv_show_dirs{$show}{$season}{has_recordings} = 0;
34 }
35 foreach $dir (`$find_tv_show_dirs`) {
36     chomp($dir);
37     my $show = basename($dir);
38     #print "-> Found dir $dir show $show\n";
39     $tv_show_dirs{$show}{has_recordings} = 0;
40 }
41
42 # Find all of our TV show recordings and store them in a hash
43 foreach $file (`$find_tv_shows`) {
44     chomp($file);
45     my $recording = basename($file);
46     my $season = basename(dirname($file));
47     my $show = basename(dirname(dirname($file)));
48     #print "-> Found show $show season $season and recording $recording\n";
49     $tv_show_dirs{$show}{$season}{has_recordings} = 1;
50     $tv_show_dirs{$show}{has_recordings} = 1;
51 }
52
53 #print Dumper(\%tv_show_dirs);
54
55 # For each season directory that doesn't have recordings, remove it
56 foreach $show (keys %tv_show_dirs) {
57     foreach $season (keys %{$tv_show_dirs{$show}}) {
58         if($season eq "has_recordings") { next; }
59         if ($tv_show_dirs{$show}{$season}{has_recordings} == 0) {
60             print "-> Removing season dir '$show/$season'\n";
61             system("rm -rf \"$tv_src_dir/$show/$season/\"");
62         }
63     }
64 }
65
66 # For each show directory that doesn't have any recordings under it, remove it
67 foreach $show (keys %tv_show_dirs) {
68     my $has_recordings = 0;
69     if($show eq ".grab") { next; }
70     foreach $season (keys %{$tv_show_dirs{$show}}) {
71         if ($tv_show_dirs{$show}{$season}{has_recordings} == 1) {
72             $has_recordings = 1;
73         }
74     }
75     if($has_recordings == 0) { 
76         print "-> Removing show dir '$show'\n";
77         system("rm -rf \"$tv_src_dir/$show/\"");
78     }
79 }