From 9831f8fa7febb1d7ecfe32bf5a68fd8cd231c93f Mon Sep 17 00:00:00 2001
From: Nic Wolfe <nic@wolfeden.ca>
Date: Wed, 19 Jan 2011 21:55:01 -0700
Subject: [PATCH] Add WDTV metadata support

---
 sickbeard/metadata/__init__.py |   4 +-
 sickbeard/metadata/wdtv.py     | 129 +++++++++++++++++++++++++++++++++
 sickbeard/webserve.py          |   4 +-
 3 files changed, 134 insertions(+), 3 deletions(-)
 create mode 100644 sickbeard/metadata/wdtv.py

diff --git a/sickbeard/metadata/__init__.py b/sickbeard/metadata/__init__.py
index 51f0f6e2a..4bd27b5d8 100644
--- a/sickbeard/metadata/__init__.py
+++ b/sickbeard/metadata/__init__.py
@@ -1,7 +1,7 @@
-__all__ = ['generic', 'helpers', 'xbmc', 'mediabrowser', 'ps3']
+__all__ = ['generic', 'helpers', 'xbmc', 'mediabrowser', 'ps3', 'wdtv']
 
 import sys
-import xbmc, mediabrowser, ps3
+import xbmc, mediabrowser, ps3, wdtv
 
 def available_generators():
     return filter(lambda x: x not in ('generic', 'helpers'), __all__)
diff --git a/sickbeard/metadata/wdtv.py b/sickbeard/metadata/wdtv.py
new file mode 100644
index 000000000..7721613fd
--- /dev/null
+++ b/sickbeard/metadata/wdtv.py
@@ -0,0 +1,129 @@
+# Author: Nic Wolfe <nic@wolfeden.ca>
+# URL: http://code.google.com/p/sickbeard/
+#
+# This file is part of Sick Beard.
+#
+# Sick Beard is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Sick Beard is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Sick Beard.  If not, see <http://www.gnu.org/licenses/>.
+
+import datetime
+
+import sickbeard
+
+import generic
+
+from sickbeard.common import *
+from sickbeard import logger, exceptions, helpers
+from lib.tvdb_api import tvdb_api, tvdb_exceptions
+
+from sickbeard import encodingKludge as ek
+
+class WDTVMetadata(generic.GenericMetadata):
+    """
+    Metadata generation class for WDTV
+
+    The following file structure is used:
+    
+    show_root/folder.jpg                                     (poster)
+    show_root/Season 01/folder.jpg                           (episode thumb)
+    show_root/Season 01/show - 1x01 - episode.jpg            (existing video)
+    """
+    
+    def __init__(self,
+                 show_metadata=False,
+                 episode_metadata=False,
+                 poster=False,
+                 fanart=False,
+                 episode_thumbnails=False,
+                 season_thumbnails=False):
+
+        generic.GenericMetadata.__init__(self,
+                                         show_metadata,
+                                         episode_metadata,
+                                         poster,
+                                         fanart,
+                                         episode_thumbnails,
+                                         season_thumbnails)
+        
+        self.name = 'WDTV'
+
+        self.eg_show_metadata = "<i>not supported</i>"
+        self.eg_episode_metadata = "<i>not supported</i>"
+        self.eg_fanart = "<i>not supported</i>"
+        self.eg_poster = "folder.jpg"
+        self.eg_episode_thumbnails = "Season##\\<i>filename</i>.jpg"
+        self.eg_season_thumbnails = "Season##\\folder.jpg"
+    
+    # all of the following are not supported, so do nothing
+    def create_show_metadata(self, show_obj):
+        pass
+    
+    def create_episode_metadata(self, ep_obj):
+        pass
+    
+    def create_fanart(self, show_obj):
+        pass
+    
+    def get_episode_thumb_path(self, ep_obj):
+        """
+        Returns the path where the episode thumbnail should be stored. Defaults to
+        the same path as the episode file but with a .cover.jpg extension.
+        
+        ep_obj: a TVEpisode instance for which to create the thumbnail
+        """
+        if ek.ek(os.path.isfile, ep_obj.location):
+            tbn_filename = helpers.replaceExtension(ep_obj.location, 'jpg')
+        else:
+            return None
+
+        return tbn_filename
+    
+    def get_season_thumb_path(self, show_obj, season):
+        """
+        Season thumbs for MediaBrowser go in Show Dir/Season X/folder.jpg
+        
+        If no season folder exists, None is returned
+        """
+        
+        dir_list = [x for x in ek.ek(os.listdir, show_obj.location) if ek.ek(os.path.isdir, ek.ek(os.path.join, show_obj.location, x))]
+        
+        season_dir_regex = '^Season\s+(\d+)$'
+        
+        season_dir = None
+        
+        for cur_dir in dir_list:
+            if season == 0 and cur_dir == 'Specials':
+                season_dir = cur_dir
+                break
+            
+            match = re.match(season_dir_regex, cur_dir, re.I)
+            if not match:
+                continue
+        
+            cur_season = int(match.group(1))
+            
+            if cur_season == season:
+                season_dir = cur_dir
+                break
+
+        if not season_dir:
+            logger.log(u"Unable to find a season dir for season "+str(season), logger.DEBUG)
+            return None
+
+        logger.log(u"Using "+str(season_dir)+"/folder.jpg as season dir for season "+str(season), logger.DEBUG)
+
+        return ek.ek(os.path.join, show_obj.location, season_dir, 'folder.jpg')
+
+# present a standard "interface"
+metadata_class = WDTVMetadata
+
diff --git a/sickbeard/webserve.py b/sickbeard/webserve.py
index 47acfe072..baeb71642 100644
--- a/sickbeard/webserve.py
+++ b/sickbeard/webserve.py
@@ -467,7 +467,8 @@ class ConfigGeneral:
                     naming_multi_ep_type=None, naming_ep_name=None,
                     naming_use_periods=None, naming_sep_type=None, naming_quality=None,
                     anyQualities = [], bestQualities = [], naming_dates=None,
-                    xbmc_data=None, mediabrowser_data=None, sony_ps3_data=None, use_banner=None):
+                    xbmc_data=None, mediabrowser_data=None, sony_ps3_data=None,
+                    wdtv_data=None, use_banner=None):
 
         results = []
 
@@ -542,6 +543,7 @@ class ConfigGeneral:
         sickbeard.metadata_provider_dict['XBMC'].set_config(xbmc_data)
         sickbeard.metadata_provider_dict['MediaBrowser'].set_config(mediabrowser_data)
         sickbeard.metadata_provider_dict['Sony PS3'].set_config(sony_ps3_data)
+        sickbeard.metadata_provider_dict['WDTV'].set_config(wdtv_data)
         
         sickbeard.SEASON_FOLDERS_FORMAT = season_folders_format
         sickbeard.SEASON_FOLDERS_DEFAULT = int(season_folders_default)
-- 
GitLab