<commit_before>import requests
import xbmc

from devhelper.pykodi import log

import mediatypes
import providers
from base import AbstractProvider
from sorteddisplaytuple import SortedDisplay

# TODO: I can get good sized thumbnails from tmdb with 'w780' instead of 'original' in the URL
# TODO: Reweigh ratings, as this does the exact opposite of thetvdb, and weighs each single rating very lowly, images rarely hit 6 stars, nor go lower than 4.7
class TheMovieDBProvider(AbstractProvider):
    name = 'themoviedb.org'
    mediatype = mediatypes.MOVIE

    apikey = '***REMOVED***'
    cfgurl = 'http://api.themoviedb.org/3/configuration'
    apiurl = 'http://api.themoviedb.org/3/movie/%s/images'
    artmap = {'backdrops': 'fanart', 'posters': 'poster'}

    def __init__(self):
        super(TheMovieDBProvider, self).__init__()
        self.session.headers['Accept'] = 'application/json'

    def log(self, message, level=xbmc.LOGDEBUG):
        log(message, level, tag=self.name)

    def _get_base_url(self):
        response = self.session.get(self.cfgurl, params={'api_key': self.apikey}, timeout=5)
        return response.json()['images']['base_url']

    def get_images(self, mediaid):
        self.log("Getting art for '%s'." % mediaid)
        response = self.session.get(self.apiurl % mediaid, params={'api_key': self.apikey}, timeout=5)
        if response.status_code == requests.codes.not_found:
            return {}
        response.raise_for_status()
        base_url = self._get_base_url()
        data = response.json()
        result = {}
        for arttype, artlist in data.iteritems():
            generaltype = self.artmap.get(arttype)
            if not generaltype:
                continue
            if artlist and generaltype not in result:
                result[generaltype] = []
            for image in artlist:
                resultimage = {'url': base_url + 'original' + image['file_path'], 'provider': self.name}
                resultimage['language'] = image['iso_639_1']
                if image['vote_count']:
                    resultimage['rating'] = SortedDisplay(image['vote_average'], '{0:.1f}'.format(image['vote_average']))
                else:
                    resultimage['rating'] = SortedDisplay(5, 'Not rated')
                resultimage['size'] = SortedDisplay(image['width'], '%sx%s' % (image['width'], image['height']))
                if arttype == 'poster' and image['aspect_ratio'] > 0.685 or image['aspect_ratio'] < 0.66:
                    resultimage['status'] = providers.GOOFY_IMAGE
                result[generaltype].append(resultimage)
        return result


class TheMovieDBEpisodeProvider(AbstractProvider):
    name = 'themoviedb.org'
    mediatype = mediatypes.EPISODE

    apikey = '***REMOVED***'
    cfgurl = 'http://api.themoviedb.org/3/configuration'
    tvdbidsearch_url = 'http://api.themoviedb.org/3/find/%s?external_source=tvdb_id'

    apiurl = 'http://api.themoviedb.org/3/tv/%s/season/%s/episode/%s/images'
    artmap = {'stills': 'fanart'}

    def __init__(self):
        super(TheMovieDBEpisodeProvider, self).__init__()
        self.session.headers['Accept'] = 'application/json'

    def log(self, message, level=xbmc.LOGDEBUG):
        log(message, level, tag=self.name + '.episode')

    def _get_base_url(self):
        response = self.session.get(self.cfgurl, params={'api_key': self.apikey}, timeout=5)
        return response.json()['images']['base_url']

    def get_images(self, mediaid):
        self.log("Getting art for '%s'." % mediaid)
        response = self.session.get(self.tvdbidsearch_url % mediaid, params={'api_key': self.apikey}, timeout=5)
        if response.status_code == requests.codes.not_found:
            return {}
        response.raise_for_status()
        data = response.json()
        if not data['tv_episode_results']:
            return {}
        data = data['tv_episode_results'][0]
        response = self.session.get(self.apiurl % (data['show_id'], data['season_number'], data['episode_number']), params={'api_key': self.apikey}, timeout=5)
        if response.status_code == requests.codes.not_found:
            return {}
        response.raise_for_status()
        data = response.json()
        base_url = self._get_base_url()
        result = {}
        for arttype, artlist in data.iteritems():
            generaltype = self.artmap.get(arttype)
            if not generaltype:
                continue
            if artlist and generaltype not in result:
                result[generaltype] = []
            for image in artlist:
                resultimage = {'url': base_url + 'original' + image['file_path'], 'provider': self.name}
                resultimage['language'] = image['iso_639_1']
                if image['vote_count']:
                    resultimage['rating'] = SortedDisplay(image['vote_average'], '{0:.1f}'.format(image['vote_average']))
                else:
                    resultimage['rating'] = SortedDisplay(5, 'Not rated')
                resultimage['size'] = SortedDisplay(image['width'], '%sx%s' % (image['width'], image['height']))
                result[generaltype].append(resultimage)
        return result<commit_msg>Pull some common TMDB bits into an abc reweigh TMDB ratings<commit_after>import requests
import xbmc

from abc import ABCMeta

from devhelper.pykodi import log

import mediatypes
import providers
from base import AbstractProvider
from sorteddisplaytuple import SortedDisplay

# TODO: I can get good sized thumbnails from tmdb with 'w780' instead of 'original' in the URL
# TODO: Reweigh ratings, as this does the exact opposite of thetvdb, and weighs each single rating very lowly, images rarely hit 6 stars, nor go lower than 4.7

class TheMovieDBAbstractProvider(AbstractProvider):
    # pylint: disable=W0223
    __metaclass__ = ABCMeta

    name = 'themoviedb.org'
    cfgurl = 'http://api.themoviedb.org/3/configuration'
    apikey = '***REMOVED***'

    def __init__(self):
        super(TheMovieDBAbstractProvider, self).__init__()
        self.session.headers['Accept'] = 'application/json'

    def _get_base_url(self):
        response = self.session.get(self.cfgurl, params={'api_key': self.apikey}, timeout=5)
        return response.json()['images']['base_url']

    def _get_rating(self, image):
        if image['vote_count']:
            # Reweigh ratings, as themoviedb does the exact opposite of thetvdb, and weighs each single rating very lowly, images rarely hit 6 stars, nor go lower than 4.7
            rating = image['vote_average']
            rating = 5 + (rating - 5) * 3
            return SortedDisplay(rating, '{0:.1f}'.format(image['vote_average']))
        else:
            return SortedDisplay(5, 'Not rated')

class TheMovieDBProvider(TheMovieDBAbstractProvider):
    mediatype = mediatypes.MOVIE

    apiurl = 'http://api.themoviedb.org/3/movie/%s/images'
    artmap = {'backdrops': 'fanart', 'posters': 'poster'}

    def log(self, message, level=xbmc.LOGDEBUG):
        log(message, level, tag=self.name)

    def get_images(self, mediaid):
        self.log("Getting art for '%s'." % mediaid)
        response = self.session.get(self.apiurl % mediaid, params={'api_key': self.apikey}, timeout=5)
        if response.status_code == requests.codes.not_found:
            return {}
        response.raise_for_status()
        base_url = self._get_base_url()
        data = response.json()
        result = {}
        for arttype, artlist in data.iteritems():
            generaltype = self.artmap.get(arttype)
            if not generaltype:
                continue
            if artlist and generaltype not in result:
                result[generaltype] = []
            for image in artlist:
                resultimage = {'url': base_url + 'original' + image['file_path'], 'provider': self.name}
                resultimage['language'] = image['iso_639_1']
                resultimage['rating'] = self._get_rating(image)
                resultimage['size'] = SortedDisplay(image['width'], '%sx%s' % (image['width'], image['height']))
                if arttype == 'poster' and image['aspect_ratio'] > 0.685 or image['aspect_ratio'] < 0.66:
                    resultimage['status'] = providers.GOOFY_IMAGE
                result[generaltype].append(resultimage)
        return result

class TheMovieDBEpisodeProvider(TheMovieDBAbstractProvider):
    mediatype = mediatypes.EPISODE

    tvdbidsearch_url = 'http://api.themoviedb.org/3/find/%s?external_source=tvdb_id'
    apiurl = 'http://api.themoviedb.org/3/tv/%s/season/%s/episode/%s/images'
    artmap = {'stills': 'fanart'}

    def __init__(self):
        super(TheMovieDBEpisodeProvider, self).__init__()
        self.session.headers['Accept'] = 'application/json'

    def log(self, message, level=xbmc.LOGDEBUG):
        log(message, level, tag=self.name + '.episode')

    def get_images(self, mediaid):
        self.log("Getting art for '%s'." % mediaid)
        response = self.session.get(self.tvdbidsearch_url % mediaid, params={'api_key': self.apikey}, timeout=5)
        if response.status_code == requests.codes.not_found:
            return {}
        response.raise_for_status()
        data = response.json()
        if not data['tv_episode_results']:
            return {}
        data = data['tv_episode_results'][0]
        response = self.session.get(self.apiurl % (data['show_id'], data['season_number'], data['episode_number']), params={'api_key': self.apikey}, timeout=5)
        if response.status_code == requests.codes.not_found:
            return {}
        response.raise_for_status()
        data = response.json()
        base_url = self._get_base_url()
        result = {}
        for arttype, artlist in data.iteritems():
            generaltype = self.artmap.get(arttype)
            if not generaltype:
                continue
            if artlist and generaltype not in result:
                result[generaltype] = []
            for image in artlist:
                resultimage = {'url': base_url + 'original' + image['file_path'], 'provider': self.name}
                resultimage['language'] = image['iso_639_1']
                resultimage['rating'] = self._get_rating(image)
                resultimage['size'] = SortedDisplay(image['width'], '%sx%s' % (image['width'], image['height']))
                result[generaltype].append(resultimage)
        return result
{'fraction_non_alphanumeric': 0.12832580924022668, 'fraction_numerical': 0.007780232446450869, 'mean_word_length': 2.752072072072072, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5815444', 'n_tokens_mistral': 3179, 'n_tokens_neox': 3040, 'n_words': 683}