Plex-Meta-Manager/modules/omdb.py

63 lines
2.2 KiB
Python
Raw Normal View History

2021-03-10 16:51:27 +00:00
import logging, requests
2021-03-05 16:04:28 +00:00
from modules import util
from modules.util import Failed
from retrying import retry
logger = logging.getLogger("Plex Meta Manager")
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"]
2021-06-14 15:24:11 +00:00
class OMDb:
2021-03-05 16:04:28 +00:00
def __init__(self, params, Cache=None):
self.url = "http://www.omdbapi.com/"
self.apikey = params["apikey"]
self.limit = False
self.Cache = Cache
self.get_omdb("tt0080684")
2021-03-05 20:33:24 +00:00
@retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_failed)
2021-03-05 16:04:28 +00:00
def get_omdb(self, imdb_id):
expired = None
2021-03-05 20:33:24 +00:00
if self.Cache:
2021-03-05 16:04:28 +00:00
omdb_dict, expired = self.Cache.query_omdb(imdb_id)
if omdb_dict and expired is False:
2021-06-23 19:44:00 +00:00
return OMDbObj(imdb_id, omdb_dict)
2021-03-05 16:04:28 +00:00
response = requests.get(self.url, params={"i": imdb_id, "apikey": self.apikey})
if response.status_code < 400:
2021-06-23 19:44:00 +00:00
omdb = OMDbObj(imdb_id, response.json())
2021-03-05 20:33:24 +00:00
if self.Cache:
2021-03-05 16:04:28 +00:00
self.Cache.update_omdb(expired, omdb)
return omdb
else:
error = response.json()['Error']
if error == "Request limit reached!":
self.limit = True
raise Failed(f"OMDb Error: {error}")