Ejemplo n.º 1
0
 def __init__(self, config):
     self.omdb = Omdb()
     self.config = config
Ejemplo n.º 2
0
class Classifier(object):

    def __init__(self, config):
        self.omdb = Omdb()
        self.config = config

    def get_types(self, path):
        types = []
        if os.path.isdir(path):
            for folder, _, files in os.walk(path):
                for filename in files:
                    types.append(mimetypes.guess_type(filename)[0])

        else:
            types.append(mimetypes.guess_type(path)[0])

        return filter(None, list(set(types)))

    def classify(self, path):
        types = self.get_types(path)
        logger.debug('%s: found types: %s' % (path, types))

        classifications = [
            self.classify_by_nfo(path),
            Classification.none(path),
        ]

        if any(['video' in t for t in types]):
            classifications.extend([
                detect_tv(path),
                detect_movie(path),
            ])

        if any(['audio' in t for t in types]):
            classifications.extend([
                detect_music(path, types),
            ])

        return max(classifications)

    def actions_for(self, classification):
        actions = self.config.get('actions', [])
        if not actions:
            logger.warn("No actions registered in config")
            return []

        return [a for a in actions
                if a['media_type'] == classification.media_type and
                a['confidence'] <= classification.score]


    def classify_by_nfo(self, path):

        if os.path.isdir(path):
            nfos = find_nfos(path)
            ids = [extract_imdb_id(nfo) for nfo in nfos]
            omdb_responses = filter(None, [self.omdb.api(imdb=i) for i in ids])
            omdb_types = map(operator.itemgetter('Type'), omdb_responses)

            if 'movie' in omdb_types:
                return Classification(path, MEDIA_TYPES.movie, 10)

            elif set(['episode', 'series']).intersection(omdb_types):
                return Classification(path, MEDIA_TYPES.tv, 10)

        return Classification.none(path)