flexget.plugins.metainfo.thetvdb_lookup
Covered: 82 lines
Missed: 41 lines
Skipped 28 lines
Percent: 66 %
  1
import logging
  2
from flexget.plugin import get_plugin_by_name, register_plugin, DependencyError, priority
  4
try:
  5
    from flexget.plugins.api_tvdb import lookup_series, lookup_episode, get_mirror
  6
except ImportError:
  7
    raise DependencyError(issued_by='thetvdb_lookup', missing='api_tvdb',
  8
                          message='thetvdb_lookup requires the `api_tvdb` plugin')
 10
log = logging.getLogger('thetvdb_lookup')
 13
class PluginThetvdbLookup(object):
 14
    """Retrieves TheTVDB information for entries. Uses series_name,
 15
    series_season, series_episode from series plugin.
 17
    Example:
 19
    thetvdb_lookup: yes
 21
    Primarily used for passing thetvdb information to other plugins.
 22
    Among these is the IMDB url for the series.
 24
    This information is provided (via entry):
 25
    series info:
 26
      series_name_thetvdb
 27
      series_rating
 28
      series_status (Continuing or Ended)
 29
      series_runtime (show runtime in minutes)
 30
      series_first_air_date
 31
      series_air_time
 32
      series_content_rating
 33
      series_genres
 34
      series_network
 35
      series_banner_url
 36
      series_fanart_url
 37
      series_poster_url
 38
      series_airs_day_of_week
 39
      series_actors
 40
      series_language (en, fr, etc.)
 41
      imdb_url (if available)
 42
      zap2it_id (if available)
 43
    episode info: (if episode is found)
 44
      ep_name
 45
      ep_overview
 46
      ep_directors
 47
      ep_writers
 48
      ep_air_date
 49
      ep_rating
 50
      ep_guest_stars
 51
      ep_image_url
 52
    """
 55
    series_map = {
 56
        'series_name_tvdb': 'seriesname',
 57
        'series_rating': 'rating',
 58
        'series_status': 'status',
 59
        'series_runtime': 'runtime',
 60
        'series_first_air_date': 'firstaired',
 61
        'series_air_time': 'airs_time',
 62
        'series_content_rating': 'contentrating',
 63
        'series_genres': 'genre',
 64
        'series_network': 'network',
 65
        'series_banner_url': lambda series: series.banner and get_mirror('banner') + series.banner,
 66
        'series_fanart_url': lambda series: series.fanart and get_mirror('banner') + series.fanart,
 67
        'series_poster_url': lambda series: series.poster and get_mirror('banner') + series.poster,
 68
        'series_airs_day_of_week': 'airs_dayofweek',
 69
        'series_language': 'language',
 70
        'imdb_url': lambda series: series.imdb_id and 'http://www.imdb.com/title/%s' % series.imdb_id,
 71
        'imdb_id': 'imdb_id',
 72
        'zap2it_id': 'zap2it_id',
 73
        'thetvdb_id': 'id'}
 75
    episode_map = {
 76
        'ep_name': 'episodename',
 77
        'ep_air_date': 'firstaired',
 78
        'ep_rating': 'rating',
 79
        'ep_image_url': lambda ep: ep.filename and get_mirror('banner') + ep.filename,
 80
        'ep_overview': 'overview',
 81
        'ep_writers': 'writer',
 82
        'ep_directors': 'director',
 83
        'ep_guest_stars': 'gueststars'}
 85
    def validator(self):
 86
        from flexget import validator
 87
        return validator.factory('boolean')
 89
    def on_process_start(self, feed, config):
 90
        """Register the usable set plugin keywords"""
 91
        try:
 92
            set_plugin = get_plugin_by_name('set')
 93
            set_plugin.instance.register_key('thetvdb_id', 'integer')
 94
        except DependencyError:
 95
            pass
 97
    def lazy_series_lookup(self, entry, field):
 98
        """Does the lookup for this entry and populates the entry fields."""
 99
        try:
100
            series = lookup_series(entry.get('series_name', eval_lazy=False), tvdb_id=entry.get('thetvdb_id', eval_lazy=False))
101
            entry.update_using_map(self.series_map, series)
102
        except LookupError, e:
103
            log.debug('Error looking up tvdb series information for %s: %s' % (entry['title'], e.message))
104
            entry.unregister_lazy_fields(self.series_map, self.lazy_series_lookup)
106
            entry.unregister_lazy_fields(self.episode_map, self.lazy_episode_lookup)
108
        return entry[field]
110
    def lazy_episode_lookup(self, entry, field):
111
        try:
112
            season_offset = entry.get('thetvdb_lookup_season_offset', 0)
113
            episode_offset = entry.get('thetvdb_lookup_episode_offset', 0)
114
            if not isinstance(season_offset, int):
115
                log.error('thetvdb_lookup_season_offset must be an integer')
116
                season_offset = 0
117
            if not isinstance(episode_offset, int):
118
                log.error('thetvdb_lookup_episode_offset must be an integer')
119
                episode_offset = 0
120
            if season_offset != 0 or episode_offset != 0:
121
                log.debug('Using offset for tvdb lookup: season: %s, episode: %s' % (season_offset, episode_offset))
123
            episode = lookup_episode(entry.get('series_name', eval_lazy=False),
124
                                     entry['series_season'] + season_offset,
125
                                     entry['series_episode'] + episode_offset,
126
                                     tvdb_id=entry.get('thetvdb_id', eval_lazy=False))
127
            entry.update_using_map(self.episode_map, episode)
128
        except LookupError, e:
129
            log.debug('Error looking up tvdb episode information for %s: %s' % (entry['title'], e.message))
130
            entry.unregister_lazy_fields(self.episode_map, self.lazy_episode_lookup)
132
        return entry[field]
135
    @priority(110)
136
    def on_feed_metainfo(self, feed, config):
137
        if not config:
138
            return
140
        for entry in feed.entries:
142
            if entry.get('series_name') or entry.get('thetvdb_id', eval_lazy=False):
143
                entry.register_lazy_fields(self.series_map, self.lazy_series_lookup)
146
                if entry.get('series_season') and entry.get('series_episode'):
147
                    entry.register_lazy_fields(self.episode_map, self.lazy_episode_lookup)
150
register_plugin(PluginThetvdbLookup, 'thetvdb_lookup', api_ver=2)