torrentMonitored.pl 2.6 KB

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