season.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?
  2. require 'config.php';
  3. # True if the provided season folder is being monitored
  4. function isMonitored($season_path) {
  5. if (file_exists(dirname($season_path) . '/skip')) {
  6. return false;
  7. }
  8. if (file_exists($season_path . '/season_done')) {
  9. return false;
  10. }
  11. $season = 0;
  12. if (preg_match('/^Season\s+(\d+)$/i', basename($season_path), $matches)) {
  13. $season = $matches[1];
  14. }
  15. if (!$season) {
  16. return false;
  17. }
  18. return true;
  19. }
  20. # Get the season search status, including the URL (if any) for the provided path
  21. function seasonSearch($season_path) {
  22. $retval = isMonitored($season_path);
  23. if ($retval) {
  24. $file = $season_path . '/url';
  25. if (is_readable($file)) {
  26. $retval = trim(file_get_contents($file));
  27. }
  28. }
  29. return $retval;
  30. }
  31. # Find all the season in a provided series folder and determine which are being monitored
  32. function findSeasons($path) {
  33. $retval = array();
  34. # Check for the skip file
  35. $skip = false;
  36. if (file_exists($path . '/skip')) {
  37. $skip = true;
  38. }
  39. $dir = opendir($path);
  40. if ($dir === FALSE) {
  41. die('Unable to opendir(): ' . htmlspecialchars($path) . "\n");
  42. }
  43. while (false !== ($season = readdir($dir))) {
  44. # Skip junk
  45. if (isJunk($show)) {
  46. continue;
  47. }
  48. # We only care about directories
  49. $season_path = $path . '/' . $season;
  50. if (!is_dir($season_path)) {
  51. continue;
  52. }
  53. # Record the season number and search status
  54. if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
  55. $retval[ $matches[1] ] = seasonSearch($season_path);
  56. }
  57. }
  58. closedir($dir);
  59. # Sort numerically; the directory listing typically returns a lexicographic order
  60. ksort($retval, SORT_NUMERIC);
  61. return $retval;
  62. }
  63. # Save the search status for all seasons in a series
  64. function saveSeasons($series_path, $data, $series_last, $seasons_last) {
  65. # Do nothing if we are or just were in "skip" mode
  66. if ($series['skip'] || $series_last['skip']) {
  67. return;
  68. }
  69. # Fresh data from disk
  70. $series = readFlags($series_path);
  71. $seasons = findSeasons($series_path);
  72. # For each season
  73. foreach ($seasons as $season => $status) {
  74. $season_path = $series_path . '/Season ' . $season;
  75. $monitored = $data[ 'season_' . $season ];
  76. $monitored_path = $season_path . '/season_done';
  77. if ($monitored) {
  78. if (file_exists($monitored_path)) {
  79. unlink($monitored_path);
  80. }
  81. } else {
  82. if (!file_exists($monitored_path)) {
  83. touch($monitored_path);
  84. }
  85. }
  86. $url = $data[ 'url_' . $season ];
  87. $url_path = $season_path . '/url';
  88. if ($url) {
  89. if (isMonitored($season_path)) {
  90. file_put_contents($url_path, $url . "\n");
  91. }
  92. } else {
  93. if ($seasons_last[ $season ] && isMonitored($season_path)) {
  94. if (file_exists($url_path)) {
  95. unlink($url_path);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. # Add a folder for the provided show and season
  102. function addSeason($show, $season) {
  103. global $TV_DIR;
  104. # Ensure the show exists
  105. $show_path = $TV_DIR . '/' . $show;
  106. if (!is_dir($show_path)) {
  107. die('Invalid show: ' . htmlspecialchars($show) . "\n");
  108. }
  109. # Ensure the season does not exist
  110. $season_path = $show_path . '/Season ' . intval($season);
  111. if (file_exists($season_path)) {
  112. die('Invalid season: ' . htmlspecialchars($season) . "\n");
  113. }
  114. # Create the directory
  115. mkdir($season_path);
  116. }
  117. ?>