Plex-Meta-Manager/modules/omdb.py

72 lines
2.8 KiB
Python
Raw Normal View History

from datetime import datetime
2021-03-05 16:04:28 +00:00
from modules import util
from modules.util import Failed
logger = util.logger
2021-03-05 16:04:28 +00:00
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}")
def _parse(key, is_int=False, is_float=False, is_date=False, replace=None):
try:
value = str(data[key]).replace(replace, '') if replace else data[key]
if is_int:
return int(value)
elif is_float:
return float(value)
elif is_date:
return datetime.strptime(value, "%d %b %Y")
else:
return value
except (ValueError, TypeError, KeyError):
return None
self.title = _parse("Title")
self.year = _parse("Year", is_int=True)
self.released = _parse("Released", is_date=True)
self.content_rating = _parse("Rated")
self.genres_str = _parse("Genre")
self.genres = util.get_list(self.genres_str)
self.imdb_rating = _parse("imdbRating", is_float=True)
self.imdb_votes = _parse("imdbVotes", is_int=True, replace=",")
self.metacritic_rating = _parse("Metascore", is_int=True)
self.imdb_id = _parse("imdbID")
self.type = _parse("Type")
self.series_id = _parse("seriesID")
self.season_num = _parse("Season", is_int=True)
self.episode_num = _parse("Episode", is_int=True)
2022-01-06 06:25:23 +00:00
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.expiration = params["expiration"]
2021-03-05 16:04:28 +00:00
self.limit = False
logger.secret(self.apikey)
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:
omdb_dict, expired = self.config.Cache.query_omdb(imdb_id, self.expiration)
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:
self.config.Cache.update_omdb(expired, omdb, self.expiration)
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}")