From a3994f62e8cf4a0e94f86f5b19c76c8f7e75e61c Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sat, 16 Jun 2012 14:37:18 +0100 Subject: [PATCH] Update duplicate checking to allow for subtitles and mixed filetypes --- .../Source/Plugin/TV.class.php | 30 ++++++++++++++----- .../Utility/MediaFile.class.php | 20 +++++++++++++ .../Utility/MediaFilesIterator.class.php | 9 +++--- .../Utility/VideoFilesIterator.class.php | 14 +++++++++ 4 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 source/lib/DownloadDispatcher/Utility/MediaFile.class.php create mode 100644 source/lib/DownloadDispatcher/Utility/VideoFilesIterator.class.php diff --git a/source/lib/DownloadDispatcher/Source/Plugin/TV.class.php b/source/lib/DownloadDispatcher/Source/Plugin/TV.class.php index 0247905..bec85c9 100644 --- a/source/lib/DownloadDispatcher/Source/Plugin/TV.class.php +++ b/source/lib/DownloadDispatcher/Source/Plugin/TV.class.php @@ -99,6 +99,9 @@ class DownloadDispatcher_Source_Plugin_TV extends DownloadDispatcher_Source_Plug $normalised_file = $this->normalise($file); if (array_key_exists($normalised_file, $this->output_dir_cache)) { $season = $this->season($file); + if (!$season) { + $season = $this->season($dir); + } $full_output_dir = "{$this->output_dir}/{$this->output_dir_cache[$normalised_file]}/Season {$season}"; @@ -138,15 +141,16 @@ class DownloadDispatcher_Source_Plugin_TV extends DownloadDispatcher_Source_Plug protected function normalise($name) { $normalised_name = $name; - if (preg_match('/(?:\[ www.[a-zA-Z0-9.]+ \] - )?(.*?)([\s.]+us)?([\s\.](19|20)\d{2})?[\s\.](\d+x\d+|s\d+e\d+|\d{3,4}).*/i', $normalised_name, $matches)) { + if (preg_match('/(?:\[ www.[a-zA-Z0-9.]+ \] - )?(.*?)([\s.]+us)?([\s\.](19|20)\d{2})?[\s\.](\d+x\d+|s\d+[.-_]?e\d+|\d{3,4}).*/i', $normalised_name, $matches)) { $normalised_name = $matches[1]; } $normalised_name = preg_replace('/[^a-zA-Z0-9]/', ' ', $normalised_name); $normalised_name = preg_replace('/ +/', ' ', $normalised_name); $normalised_name = strtolower($normalised_name); + $normalised_name = preg_replace('/season \d+( complete)?/', '', $normalised_name); $normalised_name = trim($normalised_name); - + DownloadDispatcher_LogEntry::debug($this->log, "Normalised '{$name}' to '{$normalised_name}'"); return $normalised_name; } @@ -161,7 +165,7 @@ class DownloadDispatcher_Source_Plugin_TV extends DownloadDispatcher_Source_Plug return null; }; - if (preg_match('/(\d+)\d{2}(?!\d|[\s\.](?:\d+x\d+|s\d+ep?\d+))|(\d+)x\d+|s(\d+)e\d+/i', $name, $matches)) { + if (preg_match('/(\d+)\d{2}(?!\d|[\s\.](?:\d+x\d+|s\d[._-]?+ep?\d+))|(\d+)x\d+|s(\d+)e\d+|season (\d+)/i', $name, $matches)) { return $set_season($matches); } else { return 0; @@ -178,22 +182,32 @@ class DownloadDispatcher_Source_Plugin_TV extends DownloadDispatcher_Source_Plug return null; }; -# if (preg_match('/\d+x(\d+)|s\d+e(\d+)|(?:(?:19|20)\d{2}[\s\.]+)?\d+(\d{2})/i', $name, $matches)) { - if (preg_match('/\d+(\d{2})(?!\d|[\s\.](?:\d+x\d+|s\d+ep?\d+))|\d+x(\d+)|s\d+e(\d+)/i', $name, $matches)) { + if (preg_match('/\d+(\d{2})(?!\d|[\s\.](?:\d+x\d+|s\d[._-]?+ep?\d+))|\d+x(\d+)|s\d+e(\d+)|^(\d{1,2})/i', $name, $matches)) { return $set_episode($matches); } else { return 0; } } + + protected function filetype($file) { + if (preg_match('/\.([^.]*)$/', $file, $matches)) { + return $matches[1]; + } + + return null; + } protected function checkDuplicates($dir, $file) { $episode = $this->episode($file); - - $iterator = new DownloadDispatcher_Utility_MediaFilesIterator(new DownloadDispatcher_Utility_VisibleFilesIterator(new DirectoryIterator($dir))); + + $iterator = new DownloadDispatcher_Utility_VideoFilesIterator(new DownloadDispatcher_Utility_VisibleFilesIterator(new DirectoryIterator($dir))); foreach ($iterator as /** @var SplFileInfo */ $existing_file) { $existing_episode = $this->episode($existing_file->getFilename()); if ($existing_episode == $episode) { - throw new DownloadDispatcher_Exception_DuplicateContent($file); + // Only reject duplicates with the same extension, so we can keep meta data or high/low def copies + if ($this->filetype($file) == $this->filetype($existing_file->getFilename())) { + throw new DownloadDispatcher_Exception_DuplicateContent($file); + } } } } diff --git a/source/lib/DownloadDispatcher/Utility/MediaFile.class.php b/source/lib/DownloadDispatcher/Utility/MediaFile.class.php new file mode 100644 index 0000000..7a8b475 --- /dev/null +++ b/source/lib/DownloadDispatcher/Utility/MediaFile.class.php @@ -0,0 +1,20 @@ + diff --git a/source/lib/DownloadDispatcher/Utility/MediaFilesIterator.class.php b/source/lib/DownloadDispatcher/Utility/MediaFilesIterator.class.php index b5c8d62..f406e1a 100644 --- a/source/lib/DownloadDispatcher/Utility/MediaFilesIterator.class.php +++ b/source/lib/DownloadDispatcher/Utility/MediaFilesIterator.class.php @@ -6,10 +6,9 @@ class DownloadDispatcher_Utility_MediaFilesIterator extends FilterIterator { if (preg_match('/^sample/', $filename)) { return false; } - if (preg_match('/(? \ No newline at end of file +?> diff --git a/source/lib/DownloadDispatcher/Utility/VideoFilesIterator.class.php b/source/lib/DownloadDispatcher/Utility/VideoFilesIterator.class.php new file mode 100644 index 0000000..227a686 --- /dev/null +++ b/source/lib/DownloadDispatcher/Utility/VideoFilesIterator.class.php @@ -0,0 +1,14 @@ +current()->getFilename(); + if (preg_match('/^sample/', $filename)) { + return false; + } + + return DownloadDispatcher_Utility_MediaFile::isVideoFile($filename); + } +} + +?>