Plex-Meta-Manager/modules/omdb.py

78 lines
2.9 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
2022-09-26 13:59:54 +00:00
from json import JSONDecodeError
2021-03-05 16:04:28 +00:00
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:
2024-05-28 20:22:51 +00:00
def __init__(self, requests, cache, params):
self.requests = requests
self.cache = cache
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
2024-05-28 20:22:51 +00:00
if self.cache and not ignore_cache:
omdb_dict, expired = self.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)
2022-09-07 19:32:52 +00:00
logger.trace(f"IMDb ID: {imdb_id}")
2024-05-28 20:22:51 +00:00
response = self.requests.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())
2024-05-28 20:22:51 +00:00
if self.cache and not ignore_cache:
self.cache.update_omdb(expired, omdb, self.expiration)
2021-03-05 16:04:28 +00:00
return omdb
else:
2022-09-26 13:59:54 +00:00
try:
error = response.json()['Error']
if error == "Request limit reached!":
self.limit = True
except JSONDecodeError:
error = f"Invalid JSON: {response.content}"
2021-03-05 16:04:28 +00:00
raise Failed(f"OMDb Error: {error}")