diff --git a/couchpotato/core/media/show/providers/info/thetvdb.py b/couchpotato/core/media/show/providers/info/thetvdb.py index 4aa989cb..e1d749fe 100755 --- a/couchpotato/core/media/show/providers/info/thetvdb.py +++ b/couchpotato/core/media/show/providers/info/thetvdb.py @@ -12,7 +12,6 @@ from couchpotato.core.media.show.providers.base import ShowProvider from tvdb_api import tvdb_exceptions from tvdb_api.tvdb_api import Tvdb, Show - log = CPLog(__name__) autoload = 'TheTVDb' @@ -26,8 +25,6 @@ class TheTVDb(ShowProvider): # TODO: Expose apikey in setting so it can be changed by user def __init__(self): - addEvent('info.search', self.search, priority = 1) - addEvent('show.search', self.search, priority = 1) addEvent('show.info', self.getShowInfo, priority = 1) addEvent('season.info', self.getSeasonInfo, priority = 1) addEvent('episode.info', self.getEpisodeInfo, priority = 1) @@ -44,57 +41,6 @@ class TheTVDb(ShowProvider): self.tvdb = Tvdb(**self.tvdb_api_parms) self.valid_languages = self.tvdb.config['valid_languages'] - def search(self, q, limit = 12, language = 'en'): - ''' Find show by name - show = { 'id': 74713, - 'language': 'en', - 'lid': 7, - 'seriesid': '74713', - 'seriesname': u'Breaking Bad',} - ''' - - if self.isDisabled(): - return False - - if language != self.tvdb_api_parms['language'] and language in self.valid_languages: - self.tvdb_api_parms['language'] = language - self._setup() - - query = q - #query = simplifyString(query) - cache_key = 'thetvdb.cache.search.%s.%s' % (query, limit) - results = self.getCache(cache_key) - - if not results: - log.debug('Searching for show: %s', q) - - raw = None - try: - raw = self.tvdb.search(query) - except (tvdb_exceptions.tvdb_error, IOError), e: - log.error('Failed searching TheTVDB for "%s": %s', (query, traceback.format_exc())) - return False - - results = [] - if raw: - try: - nr = 0 - for show_info in raw: - - results.append(self._parseShow(show_info)) - nr += 1 - if nr == limit: - break - - log.info('Found: %s', [result['titles'][0] + ' (' + str(result.get('year', 0)) + ')' for result in results]) - self.setCache(cache_key, results) - return results - except (tvdb_exceptions.tvdb_error, IOError), e: - log.error('Failed parsing TheTVDB for "%s": %s', (q, traceback.format_exc())) - return False - - return results - def getShow(self, identifier = None): show = None try: @@ -400,9 +346,9 @@ class TheTVDb(ShowProvider): def isDisabled(self): if self.conf('api_key') == '': log.error('No API key provided.') - True + return True else: - False + return False config = [{ diff --git a/couchpotato/core/media/show/providers/info/trakt.py b/couchpotato/core/media/show/providers/info/trakt.py new file mode 100755 index 00000000..cac37c15 --- /dev/null +++ b/couchpotato/core/media/show/providers/info/trakt.py @@ -0,0 +1,86 @@ +import urllib + +from couchpotato.core.event import addEvent +from couchpotato.core.logger import CPLog +from couchpotato.core.media.show.providers.base import ShowProvider + +log = CPLog(__name__) + +autoload = 'Trakt' + + +class Trakt(ShowProvider): + api_key = 'c043de5ada9d180028c10229d2a3ea5b' + base_url = 'http://api.trakt.tv/%%s.json/%s' % api_key + + def __init__(self): + addEvent('info.search', self.search, priority = 1) + addEvent('show.search', self.search, priority = 1) + + def search(self, q, limit = 12): + if self.isDisabled(): + return False + + # Check for cached result + cache_key = 'trakt.cache.search.%s.%s' % (q, limit) + results = self.getCache(cache_key) or [] + + if results: + return results + + # Search + log.debug('Searching for show: "%s"', q) + response = self._request('search/shows', query=q, limit=limit) + + if not response: + return [] + + # Parse search results + for show in response: + results.append(self._parseShow(show)) + + log.info('Found: %s', [result['titles'][0] + ' (' + str(result.get('year', 0)) + ')' for result in results]) + + self.setCache(cache_key, results) + return results + + def _request(self, action, **kwargs): + url = self.base_url % action + + if kwargs: + url += '?' + urllib.urlencode(kwargs) + + return self.getJsonData(url) + + def _parseShow(self, show): + # Images + images = show.get('images', {}) + + poster = images.get('poster') + backdrop = images.get('backdrop') + + # Rating + rating = show.get('ratings', {}).get('percentage') + + # Build show dict + show_data = { + 'identifiers': { + 'thetvdb': show.get('tvdb_id'), + 'imdb': show.get('imdb_id'), + 'tvrage': show.get('tvrage_id'), + }, + 'type': 'show', + 'titles': [show.get('title')], + 'images': { + 'poster': [poster] if poster else [], + 'backdrop': [backdrop] if backdrop else [], + 'poster_original': [], + 'backdrop_original': [], + }, + 'year': show.get('year'), + 'rating': { + 'trakt': float(rating) / 10 + }, + } + + return dict((k, v) for k, v in show_data.iteritems() if v)