1from . import api_utils
2try:
3    from urllib import quote
4except ImportError: # py2 / py3
5    from urllib.parse import quote
6
7API_KEY = '384afe262ee0962545a752ff340e3ce4'
8API_URL = 'https://webservice.fanart.tv/v3/movies/{}'
9
10ARTMAP = {
11    'movielogo': 'clearlogo',
12    'hdmovielogo': 'clearlogo',
13    'hdmovieclearart': 'clearart',
14    'movieart': 'clearart',
15    'moviedisc': 'discart',
16    'moviebanner': 'banner',
17    'moviethumb': 'landscape',
18    'moviebackground': 'fanart',
19    'movieposter': 'poster'
20}
21
22def get_details(uniqueids, clientkey, language, set_tmdbid):
23    media_id = _get_mediaid(uniqueids)
24    if not media_id:
25        return {}
26
27    movie_data = _get_data(media_id, clientkey)
28    movieset_data = _get_data(set_tmdbid, clientkey)
29    if not movie_data and not movieset_data:
30        return {}
31
32    movie_art = {}
33    movieset_art = {}
34    if movie_data:
35        movie_art = _parse_data(movie_data, language)
36    if movieset_data:
37        movieset_art = _parse_data(movieset_data, language)
38        movieset_art = {'set.' + key: value for key, value in movieset_art.items()}
39
40    available_art = movie_art
41    available_art.update(movieset_art)
42
43    return {'available_art': available_art}
44
45def _get_mediaid(uniqueids):
46    for source in ('tmdb', 'imdb', 'unknown'):
47        if source in uniqueids:
48            return uniqueids[source]
49
50def _get_data(media_id, clientkey):
51    headers = {'api-key': API_KEY}
52    if clientkey:
53        headers['client-key'] = clientkey
54    api_utils.set_headers(headers)
55    fanarttv_url = API_URL.format(media_id)
56    return api_utils.load_info(fanarttv_url, default={})
57
58def _parse_data(data, language):
59    result = {}
60    for arttype, artlist in data.items():
61        if arttype not in ARTMAP:
62            continue
63        for image in artlist:
64            image_lang = _get_imagelanguage(arttype, image)
65            if image_lang and image_lang != language:
66                continue
67
68            generaltype = ARTMAP[arttype]
69            if generaltype == 'poster' and not image_lang:
70                generaltype = 'keyart'
71            if artlist and generaltype not in result:
72                result[generaltype] = []
73
74            url = quote(image['url'], safe="%/:=&?~#+!$,;'@()*[]")
75            resultimage = {'url': url, 'preview': url.replace('.fanart.tv/fanart/', '.fanart.tv/preview/')}
76            result[generaltype].append(resultimage)
77
78    return result
79
80def _get_imagelanguage(arttype, image):
81    if 'lang' not in image or arttype == 'moviebackground':
82        return None
83    if arttype in ('movielogo', 'hdmovielogo', 'hdmovieclearart', 'movieart', 'moviebanner',
84            'moviethumb', 'moviedisc'):
85        return image['lang'] if image['lang'] not in ('', '00') else 'en'
86    # movieposter may or may not have a title and thus need a language
87    return image['lang'] if image['lang'] not in ('', '00') else None
88