<commit_before>import logging
from flexget.plugin import register_plugin, DependencyError
from flexget.utils import imdb

try:
    # TODO: Fix this after api_tmdb has module level functions
    from flexget.plugins.api_tmdb import ApiTmdb
    lookup = ApiTmdb.lookup
except ImportError:
    raise DependencyError(issued_by='tmdb_lookup', missing='api_tmdb')

log = logging.getLogger('tmdb_lookup')


class PluginTmdbLookup(object):
    """Retrieves tmdb information for entries.

    Example:
        tmdb_lookup: yes
    """

    field_map = {
        'tmdb_name': 'name',
        'tmdb_id': 'id',
        'imdb_id': 'imdb_id',
        'tmdb_year': 'year',
        'tmdb_popularity': 'popularity',
        'tmdb_rating': 'rating',
        'tmdb_genres': lambda movie: [genre.name for genre in movie.genres],
        'tmdb_released': 'released',
        'tmdb_votes': 'votes',
        'tmdb_certification': 'certification',
        'tmdb_posters': lambda movie: [poster.url for poster in movie.posters],
        'tmdb_runtime': 'runtime',
        'tmdb_tagline': 'tagline',
        'tmdb_budget': 'budget',
        'tmdb_revenue': 'revenue',
        'tmdb_homepage': 'homepage',
        'tmdb_trailer': 'trailer',
        # Generic fields filled by all movie lookup plugins:
        'movie_name': 'name',
        'movie_year': 'year'}

    def validator(self):
        from flexget import validator
        return validator.factory('boolean')

    def lazy_loader(self, entry, field):
        """Does the lookup for this entry and populates the entry fields."""
        imdb_id = entry.get('imdb_id', lazy=False) or imdb.extract_id(entry.get('imdb_url', lazy=False))
        try:
            movie = lookup(smart_match=entry['title'], tmdb_id=entry.get('tmdb_id', lazy=False), imdb_id=imdb_id)
            entry.update_using_map(self.field_map, movie)
        except LookupError, e:
            # Debug the debug statement :P
            log.debug('title: %r, error: %r' % (entry['title'], e))
            log.debug('error message: %r' % unicode(e))
            log.debug('Tmdb lookup for %s failed: %s' % (entry['title'], e))
            # Set all of our fields to None if the lookup failed
            entry.unregister_lazy_fields(self.field_map, self.lazy_loader)
        return entry[field]

    def lookup(self, entry):
        """Populates all lazy fields to an Entry. May be called by other plugins requiring tmdb info on an Entry."""
        entry.register_lazy_fields(self.field_map, self.lazy_loader)

    def on_feed_metainfo(self, feed, config):
        if not config:
            return
        for entry in feed.entries:<commit_msg>Probably fix crash with tmdb logging.<commit_after>import logging
from flexget.plugin import register_plugin, DependencyError
from flexget.utils import imdb

try:
    # TODO: Fix this after api_tmdb has module level functions
    from flexget.plugins.api_tmdb import ApiTmdb
    lookup = ApiTmdb.lookup
except ImportError:
    raise DependencyError(issued_by='tmdb_lookup', missing='api_tmdb')

log = logging.getLogger('tmdb_lookup')


class PluginTmdbLookup(object):
    """Retrieves tmdb information for entries.

    Example:
        tmdb_lookup: yes
    """

    field_map = {
        'tmdb_name': 'name',
        'tmdb_id': 'id',
        'imdb_id': 'imdb_id',
        'tmdb_year': 'year',
        'tmdb_popularity': 'popularity',
        'tmdb_rating': 'rating',
        'tmdb_genres': lambda movie: [genre.name for genre in movie.genres],
        'tmdb_released': 'released',
        'tmdb_votes': 'votes',
        'tmdb_certification': 'certification',
        'tmdb_posters': lambda movie: [poster.url for poster in movie.posters],
        'tmdb_runtime': 'runtime',
        'tmdb_tagline': 'tagline',
        'tmdb_budget': 'budget',
        'tmdb_revenue': 'revenue',
        'tmdb_homepage': 'homepage',
        'tmdb_trailer': 'trailer',
        # Generic fields filled by all movie lookup plugins:
        'movie_name': 'name',
        'movie_year': 'year'}

    def validator(self):
        from flexget import validator
        return validator.factory('boolean')

    def lazy_loader(self, entry, field):
        """Does the lookup for this entry and populates the entry fields."""
        imdb_id = entry.get('imdb_id', lazy=False) or imdb.extract_id(entry.get('imdb_url', lazy=False))
        try:
            movie = lookup(smart_match=entry['title'], tmdb_id=entry.get('tmdb_id', lazy=False), imdb_id=imdb_id)
            entry.update_using_map(self.field_map, movie)
        except LookupError, e:
            log.debug(u'Tmdb lookup for %s failed: %s' % (entry['title'], e.message))
            # Set all of our fields to None if the lookup failed
            entry.unregister_lazy_fields(self.field_map, self.lazy_loader)
        return entry[field]

    def lookup(self, entry):
        """Populates all lazy fields to an Entry. May be called by other plugins requiring tmdb info on an Entry."""
        entry.register_lazy_fields(self.field_map, self.lazy_loader)

    def on_feed_metainfo(self, feed, config):
        if not config:
            return
        for entry in feed.entries:
{'fraction_non_alphanumeric': 0.11516091508336565, 'fraction_numerical': 0.0, 'mean_word_length': 2.9142640364188166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4540478', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1611, 'n_words': 423}