-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmdb.py
212 lines (195 loc) · 8.49 KB
/
tmdb.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
#!/usr/local/bin/env python
import datetime
from log import logger
from settings import LOG_LEVEL, TMDB_API_KEY
from tmdbv3api import TV, Movie, Search, TMDb
class TMDB:
def __init__(
self,
api_key: str = TMDB_API_KEY,
language: str = "zh",
movie: bool = False,
log_level: str = LOG_LEVEL,
) -> None:
self.tmdb = TMDb()
self.tmdb.api_key = api_key
self.tmdb.language = language
if log_level == "DEBUG":
self.tmdb.debug = True
self.is_movie = movie
self.tmdb_search = Search()
if self.is_movie:
self.tmdb_media = Movie()
else:
self.tmdb_media = TV()
self.tmdb_id = None
def get_name_from_tmdb(self, query_dict: dict, year_deviation: int = 0) -> tuple:
"""Get TV/Movie name from tmdb"""
search_func = (
self.tmdb_search.movies if self.is_movie else self.tmdb_search.tv_shows
)
query_title = query_dict["query"]
query_year = (
query_dict.get("year", datetime.date.today().year)
if self.is_movie
else query_dict.get("first_air_date_year", datetime.date.today().year)
)
retry = 0
name = ""
while retry < 3:
try:
while year_deviation >= 0:
res = (
search_func({"query": query_title, "year": query_year})
if self.is_movie
else search_func(
{"query": query_title, "first_air_date_year": query_year}
)
)
if not res:
logger.info(f"No result for {query_title}, exit")
year_deviation -= 1
if year_deviation > 0:
query_year -= 1
continue
else:
for rslt in res:
date = (
rslt.release_date
if self.is_movie
else rslt.first_air_date
)
year = date.split("-")[0] or query_year
title = rslt.title if self.is_movie else rslt.name
original_title = (
rslt.original_title
if self.is_movie
else rslt.original_name
)
logger.debug(rslt)
if query_title in [title, original_title] or len(res) == 1:
if rslt.original_language == "zh":
name = (
f"{original_title} ({year}) {{tmdb-{rslt.id}}}"
)
else:
# 不存在 zh-CN 翻译的情况下
if title == original_title:
# 获取详细信息
media = self.tmdb_media
media_details = media.details(rslt.id)
translations = media_details.get(
"translations"
).get("translations")
for translation in translations:
if (
translation.get("iso_3166_1") == "SG"
and translation.get("iso_639_1") == "zh"
):
title = (
translation.get("data")["name"]
if not self.is_movie
else translation.get("data")[
"title"
]
)
break
name = (
f"[{title}] {original_title} ({year}) {{tmdb-{rslt.id}}}"
if title and title != original_title
else f"{original_title} ({year}) {{tmdb-{rslt.id}}}"
)
self.tmdb_id = str(rslt.id)
logger.info(f"Renaming {query_title} to {name}")
break
break
break
except Exception as e:
logger.exception(e)
retry += 1
continue
return name.replace("/", "/"), self.tmdb_id
def get_info_from_tmdb_by_id(self, tmdb_id: str) -> dict:
"""Get movies/shows' details using tmdb_id"""
tmdb_name = ""
self.tmdb_id = tmdb_id
details = self.tmdb_media.details(self.tmdb_id)
date = details.release_date if self.is_movie else details.first_air_date
date_list = date.split("-")
if len(date_list) > 1:
year, month = date_list[:2]
else:
year, month = date_list[0], None
if not year or not month:
raise Exception("Not found first_air_date")
original_title = (
details.original_title if self.is_movie else details.original_name
)
title = details.title if self.is_movie else details.name
contries = "&".join(sorted(details.origin_country))
if details.original_language == "zh":
tmdb_name = f"{original_title} ({year}) {{tmdb-{tmdb_id}}}"
else:
if title == original_title:
translations = details.get("translations").get("translations")
for translation in translations:
if (
translation.get("iso_3166_1") == "SG"
and translation.get("iso_639_1") == "zh"
):
title = (
translation.get("data")["name"]
if not self.is_movie
else translation.get("data")["title"]
)
break
tmdb_name = (
f"[{title}] {original_title} ({year}) {{tmdb-{self.tmdb_id}}}"
if title and title != original_title
else f"{original_title} ({year}) {{tmdb-{self.tmdb_id}}}"
)
return {
"tmdb_name": tmdb_name.replace("/", "/"),
"title": title,
"year": year,
"month": month,
"country": contries,
}
def get_movie_certification(self) -> bool:
"""Get movie's certifacation"""
is_nc17 = False
_ = {
"US": "NC-17",
"HK": "III",
"JP": "R18+",
}
try:
rslts = self.tmdb_media.release_dates(self.tmdb_id).get("results")
except Exception as e:
logger.exception(f"Getting certifacation of {self.tmdb_id} failed")
logger.exception(e)
return is_nc17
iso_3166_1_list = [__.get("iso_3166_1") for __ in rslts]
for _iso, _cert in _.items():
# 没有定义的国家的分级信息,则直接跳过
# 以美国分级为主
if _iso not in iso_3166_1_list or (
"US" in iso_3166_1_list and _iso != "US"
):
continue
index = iso_3166_1_list.index(_iso)
rslt = rslts[index]
release_dates = rslt.get("release_dates")
for release_date in release_dates:
certification = release_date.get("certification")
logger.debug(
f"Getting certification of {self.tmdb_id} succeed: {certification}"
)
if certification == _cert:
return True
return is_nc17
if __name__ == "__main__":
tmdb = TMDB(movie=True)
print(tmdb.get_info_from_tmdb_by_id(tmdb_id=27205))
tmdb_tv = TMDB(movie=False)
print(tmdb_tv.get_info_from_tmdb_by_id(tmdb_id=64197))