-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathfanart.py
220 lines (202 loc) · 8.04 KB
/
fanart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from functools import lru_cache
from app.utils import RequestUtils, ExceptionUtils
from app.utils.types import MediaType
from config import Config, FANART_MOVIE_API_URL, FANART_TV_API_URL
class Fanart:
_proxies = Config().get_proxies()
_movie_image_types = ['movieposter',
'hdmovielogo',
'moviebackground',
'moviedisc',
'moviebanner',
'moviethumb']
_tv_image_types = ['hdtvlogo',
'tvthumb',
'showbackground',
'tvbanner',
'seasonposter',
'seasonbanner',
'seasonthumb',
'tvposter',
'hdclearart']
_season_types = ['seasonposter',
'seasonthumb',
'seasonbanner']
_images = {}
def __init__(self):
self.init_config()
def init_config(self):
self._images = {}
def __get_fanart_images(self, media_type, queryid):
if not media_type or not queryid:
return
try:
ret = self.__request_fanart(media_type=media_type, queryid=queryid)
if ret and ret.status_code == 200:
if media_type == MediaType.MOVIE:
for image_type in self._movie_image_types:
images = ret.json().get(image_type)
if isinstance(images, list):
self._images[image_type] = images[0].get('url') if isinstance(images[0], dict) else ""
else:
self._images[image_type] = ""
else:
for image_type in self._tv_image_types:
images = ret.json().get(image_type)
if isinstance(images, list):
if image_type in self._season_types:
if not self._images.get(image_type):
self._images[image_type] = {}
for image in images:
if image.get("season") not in self._images[image_type].keys():
self._images[image_type][image.get("season")] = image.get("url")
else:
self._images[image_type] = images[0].get('url') if isinstance(images[0], dict) else ""
else:
if image_type in self._season_types:
self._images[image_type] = {}
else:
self._images[image_type] = ""
except Exception as e2:
ExceptionUtils.exception_traceback(e2)
@classmethod
@lru_cache(maxsize=256)
def __request_fanart(cls, media_type, queryid):
if media_type == MediaType.MOVIE:
image_url = FANART_MOVIE_API_URL % queryid
else:
image_url = FANART_TV_API_URL % queryid
try:
return RequestUtils(proxies=cls._proxies, timeout=5).get_res(image_url)
except Exception as err:
ExceptionUtils.exception_traceback(err)
return None
def get_backdrop(self, media_type, queryid, default=""):
"""
获取横幅背景图
"""
if not media_type or not queryid:
return ""
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type == MediaType.MOVIE:
return self._images.get("moviethumb", default)
else:
return self._images.get("tvthumb", default)
def get_poster(self, media_type, queryid, default=None):
"""
获取海报
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type == MediaType.MOVIE:
return self._images.get("movieposter", default)
else:
return self._images.get("tvposter", default)
def get_background(self, media_type, queryid, default=None):
"""
获取海报
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type == MediaType.MOVIE:
return self._images.get("moviebackground", default)
else:
return self._images.get("showbackground", default)
def get_banner(self, media_type, queryid, default=None):
"""
获取海报
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type == MediaType.MOVIE:
return self._images.get("moviebanner", default)
else:
return self._images.get("tvbanner", default)
def get_disc(self, media_type, queryid, default=None):
"""
获取光盘封面
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type == MediaType.MOVIE:
return self._images.get("moviedisc", default)
else:
return None
def get_logo(self, media_type, queryid, default=None):
"""
获取海报
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type == MediaType.MOVIE:
return self._images.get("hdmovielogo", default)
else:
return self._images.get("hdtvlogo", default)
def get_thumb(self, media_type, queryid, default=None):
"""
获取缩略图
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type == MediaType.MOVIE:
return self._images.get("moviethumb", default)
else:
return self._images.get("tvthumb", default)
def get_clearart(self, media_type, queryid, default=None):
"""
获取clearart
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type == MediaType.TV:
return self._images.get("hdclearart", default)
else:
return None
def get_seasonposter(self, media_type, queryid, season, default=None):
"""
获取seasonposter
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type != MediaType.TV:
return None
return self._images.get("seasonposter", {}).get(season, "") or default
def get_seasonthumb(self, media_type, queryid, season, default=None):
"""
获取seasonposter
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type != MediaType.TV:
return None
return self._images.get("seasonthumb", {}).get(season, "") or default
def get_seasonbanner(self, media_type, queryid, season, default=None):
"""
获取seasonbanner
"""
if not media_type or not queryid:
return None
if not self._images:
self.__get_fanart_images(media_type=media_type, queryid=queryid)
if media_type != MediaType.TV:
return None
return self._images.get("seasonbanner", {}).get(season, "") or default