torrentMonitored.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Includes
  5. use File::Basename;
  6. # Parameters
  7. my @BASE_DIRS = ();
  8. my @skipped = ();
  9. my @monitored = ();
  10. my @done = ();
  11. # Command-line parameters
  12. my ($prg, $base_dir) = @ARGV;
  13. # TV path(s)
  14. {
  15. my $base = undef();
  16. if (defined($base_dir) && -d $base_dir) {
  17. $base = $base_dir;
  18. } else {
  19. my $cmd = $ENV{'HOME'} . '/bin/video/mediaPath';
  20. if (-x $cmd) {
  21. $base = `${cmd}`;
  22. }
  23. }
  24. if (!defined($base)) {
  25. die("No base directory available\n");
  26. }
  27. foreach my $dir ('TV', 'Downloads') {
  28. my $path = $base . '/' . $dir;
  29. if (-d $path) {
  30. push(@BASE_DIRS, $path);
  31. }
  32. }
  33. }
  34. # For each directory
  35. for my $BASE_DIR (@BASE_DIRS) {
  36. # For each show
  37. opendir(BASE_DIR, $BASE_DIR)
  38. or die("Unable to open input directory: ${!}\n");
  39. while (my $dir = readdir(BASE_DIR)) {
  40. # Skip the stock entries
  41. if (!$dir || $dir eq '.' || $dir eq '..') {
  42. next;
  43. }
  44. # Construct the path
  45. my $path = $BASE_DIR . '/' . $dir;
  46. # Skip non-folders
  47. if (!-d $path) {
  48. next;
  49. }
  50. # Look for "skip" files
  51. if (-e $path . '/skip') {
  52. push(@skipped, $dir);
  53. next;
  54. }
  55. # Look for season folders
  56. opendir(SUB_DIR, $path)
  57. or die('Unable to open subfolder "' . $path . '": ' . $! . "\n");
  58. while (my $subdir = readdir(SUB_DIR)) {
  59. # Skip the stock entries
  60. if (!$dir || $dir eq '.' || $dir eq '..') {
  61. next;
  62. }
  63. # Construct the path
  64. my $subpath = $path . '/' . $subdir;
  65. # Skip non-folders
  66. if (!-d $subpath) {
  67. next;
  68. }
  69. # Skip symlinks
  70. if (-l $subpath) {
  71. next;
  72. }
  73. # Look for "season" folders
  74. if ($subdir =~ /^Season\s+(\d+)$/i) {
  75. my $season_num = $1;
  76. # Construct a name
  77. my $name = '';
  78. if ($prg) {
  79. $name = $subpath;
  80. } else {
  81. $name = $dir . ' - ' . $subdir;
  82. }
  83. # Check if the season is done
  84. if (-e $subpath . '/season_done') {
  85. push(@done, $name);
  86. # Season 0 is always done
  87. } elsif ($season_num == 0) {
  88. push(@done, $name);
  89. # Otherwise record the season
  90. } else {
  91. push(@monitored, $name);
  92. }
  93. }
  94. }
  95. close(SUB_DIR);
  96. }
  97. close(BASE_DIR);
  98. }
  99. # Sort
  100. @done = sort(@done);
  101. @skipped = sort(@skipped);
  102. @monitored = sort(@monitored);
  103. # Print results
  104. if ($prg && $prg =~ /NULL/i) {
  105. foreach my $show (@monitored) {
  106. print $show . "\0";
  107. }
  108. } elsif ($prg && $prg =~ /STORE/i) {
  109. my %shows = ();
  110. foreach my $show (@monitored) {
  111. $shows{ basename(dirname($show)) } = 1;
  112. }
  113. foreach my $key (keys(%shows)) {
  114. print $key . "\n";
  115. }
  116. } else {
  117. if (scalar(@done) > 0) {
  118. print "\nDone:\n";
  119. foreach my $show (@done) {
  120. print $show . "\n";
  121. }
  122. } else {
  123. print "\nNo seasons done\n";
  124. }
  125. if (scalar(@skipped) > 0) {
  126. print "\nSkipped:\n";
  127. foreach my $show (@skipped) {
  128. print $show . "\n";
  129. }
  130. } else {
  131. print "\nNo shows skipped\n";
  132. }
  133. if (scalar(@monitored) > 0) {
  134. print "\nMonitored:\n";
  135. foreach my $show (@monitored) {
  136. print $show . "\n";
  137. }
  138. } else {
  139. print "\nNo shows monitored\n";
  140. }
  141. }
  142. # Cleanup
  143. exit(0);