torrentMonitored.pl 2.5 KB

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