2021-07-14 14:47:20 +00:00
|
|
|
import logging
|
2021-03-05 16:04:28 +00:00
|
|
|
from modules import util
|
|
|
|
from modules.util import Failed
|
|
|
|
|
|
|
|
logger = logging.getLogger("Plex Meta Manager")
|
|
|
|
|
2021-07-14 14:47:20 +00:00
|
|
|
base_url = "http://www.omdbapi.com/"
|
|
|
|
|
2021-03-05 16:04:28 +00:00
|
|
|
class OMDbObj:
|
2021-06-23 19:44:00 +00:00
|
|
|
def __init__(self, imdb_id, data):
|
|
|
|
self._imdb_id = imdb_id
|
2021-03-05 16:04:28 +00:00
|
|
|
self._data = data
|
2021-06-23 19:44:00 +00:00
|
|
|
if data["Response"] == "False":
|
|
|
|
raise Failed(f"OMDb Error: {data['Error']} IMDb ID: {imdb_id}")
|
2021-03-05 16:04:28 +00:00
|
|
|
self.title = data["Title"]
|
|
|
|
try:
|
|
|
|
self.year = int(data["Year"])
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
self.year = None
|
|
|
|
self.content_rating = data["Rated"]
|
|
|
|
self.genres = util.get_list(data["Genre"])
|
|
|
|
self.genres_str = data["Genre"]
|
|
|
|
try:
|
|
|
|
self.imdb_rating = float(data["imdbRating"])
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
self.imdb_rating = None
|
|
|
|
try:
|
|
|
|
self.imdb_votes = int(str(data["imdbVotes"]).replace(',', ''))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
self.imdb_votes = None
|
|
|
|
try:
|
|
|
|
self.metacritic_rating = int(data["Metascore"])
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
self.metacritic_rating = None
|
|
|
|
self.imdb_id = data["imdbID"]
|
|
|
|
self.type = data["Type"]
|
2022-01-06 06:25:23 +00:00
|
|
|
try:
|
|
|
|
self.series_id = data["seriesID"]
|
|
|
|
except (ValueError, TypeError, KeyError):
|
|
|
|
self.series_id = None
|
|
|
|
try:
|
|
|
|
self.season_num = int(data["Season"])
|
|
|
|
except (ValueError, TypeError, KeyError):
|
|
|
|
self.season_num = None
|
|
|
|
try:
|
|
|
|
self.episode_num = int(data["Episode"])
|
|
|
|
except (ValueError, TypeError, KeyError):
|
|
|
|
self.episode_num = None
|
|
|
|
|
2021-03-05 16:04:28 +00:00
|
|
|
|
2021-06-14 15:24:11 +00:00
|
|
|
class OMDb:
|
2021-07-14 14:47:20 +00:00
|
|
|
def __init__(self, config, params):
|
|
|
|
self.config = config
|
2021-03-05 16:04:28 +00:00
|
|
|
self.apikey = params["apikey"]
|
|
|
|
self.limit = False
|
2021-11-20 23:09:45 +00:00
|
|
|
self.get_omdb("tt0080684", ignore_cache=True)
|
2021-03-05 16:04:28 +00:00
|
|
|
|
2021-11-20 23:09:45 +00:00
|
|
|
def get_omdb(self, imdb_id, ignore_cache=False):
|
2021-03-05 16:04:28 +00:00
|
|
|
expired = None
|
2021-11-20 23:09:45 +00:00
|
|
|
if self.config.Cache and not ignore_cache:
|
2021-07-14 14:47:20 +00:00
|
|
|
omdb_dict, expired = self.config.Cache.query_omdb(imdb_id)
|
2021-03-05 16:04:28 +00:00
|
|
|
if omdb_dict and expired is False:
|
2021-06-23 19:44:00 +00:00
|
|
|
return OMDbObj(imdb_id, omdb_dict)
|
2021-11-03 14:38:43 +00:00
|
|
|
if self.config.trace_mode:
|
|
|
|
logger.debug(f"IMDb ID: {imdb_id}")
|
2021-07-14 14:47:20 +00:00
|
|
|
response = self.config.get(base_url, params={"i": imdb_id, "apikey": self.apikey})
|
2021-03-05 16:04:28 +00:00
|
|
|
if response.status_code < 400:
|
2021-06-23 19:44:00 +00:00
|
|
|
omdb = OMDbObj(imdb_id, response.json())
|
2021-11-20 23:09:45 +00:00
|
|
|
if self.config.Cache and not ignore_cache:
|
2021-07-14 14:47:20 +00:00
|
|
|
self.config.Cache.update_omdb(expired, omdb)
|
2021-03-05 16:04:28 +00:00
|
|
|
return omdb
|
|
|
|
else:
|
|
|
|
error = response.json()['Error']
|
|
|
|
if error == "Request limit reached!":
|
|
|
|
self.limit = True
|
|
|
|
raise Failed(f"OMDb Error: {error}")
|