pull/83/head
meisnate12 4 years ago
parent fd0301e68a
commit 4a8f825f7b

@ -43,6 +43,8 @@ sonarr: # Can be individually specified
root_folder_path: "S:/TV Shows" root_folder_path: "S:/TV Shows"
add: false add: false
search: false search: false
omdb:
apikey: ########
trakt: trakt:
client_id: ################################################################ client_id: ################################################################
client_secret: ################################################################ client_secret: ################################################################

@ -7,10 +7,8 @@ from retrying import retry
logger = logging.getLogger("Plex Meta Manager") logger = logging.getLogger("Plex Meta Manager")
class AniDBAPI: class AniDBAPI:
def __init__(self, Cache=None, TMDb=None, Trakt=None): def __init__(self, config):
self.Cache = Cache self.config = config
self.TMDb = TMDb
self.Trakt = Trakt
self.urls = { self.urls = {
"anime": "https://anidb.net/anime", "anime": "https://anidb.net/anime",
"popular": "https://anidb.net/latest/anime/popular/?h=1", "popular": "https://anidb.net/latest/anime/popular/?h=1",
@ -62,7 +60,7 @@ class AniDBAPI:
return anidb_values return anidb_values
raise Failed(f"AniDB Error: No valid AniDB IDs in {anidb_list}") raise Failed(f"AniDB Error: No valid AniDB IDs in {anidb_list}")
def get_items(self, config, method, data, language, status_message=True): def get_items(self, method, data, language, status_message=True):
pretty = util.pretty_names[method] if method in util.pretty_names else method pretty = util.pretty_names[method] if method in util.pretty_names else method
if status_message: if status_message:
logger.debug(f"Data: {data}") logger.debug(f"Data: {data}")
@ -81,7 +79,7 @@ class AniDBAPI:
for anidb_id in anime_ids: for anidb_id in anime_ids:
try: try:
for imdb_id in self.convert_anidb_to_imdb(anidb_id): for imdb_id in self.convert_anidb_to_imdb(anidb_id):
tmdb_id, _ = config.convert_from_imdb(imdb_id, language) tmdb_id, _ = self.config.convert_from_imdb(imdb_id, language)
if tmdb_id: movie_ids.append(tmdb_id) if tmdb_id: movie_ids.append(tmdb_id)
else: raise Failed else: raise Failed
except Failed: except Failed:

@ -648,10 +648,10 @@ class CollectionBuilder:
elif "tautulli" in method: elif "tautulli" in method:
items = self.library.Tautulli.get_items(self.library, time_range=value["list_days"], stats_count=value["list_size"], list_type=value["list_type"], stats_count_buffer=value["list_buffer"]) items = self.library.Tautulli.get_items(self.library, time_range=value["list_days"], stats_count=value["list_size"], list_type=value["list_type"], stats_count_buffer=value["list_buffer"])
items_found += len(items) items_found += len(items)
elif "anidb" in method: items_found += check_map(self.config.AniDB.get_items(self.config, method, value, self.library.Plex.language)) elif "anidb" in method: items_found += check_map(self.config.AniDB.get_items(method, value, self.library.Plex.language))
elif "mal" in method: items_found += check_map(self.config.MyAnimeList.get_items(method, value)) elif "mal" in method: items_found += check_map(self.config.MyAnimeList.get_items(method, value))
elif "tvdb" in method: items_found += check_map(self.config.TVDb.get_items(method, value, self.library.Plex.language)) elif "tvdb" in method: items_found += check_map(self.config.TVDb.get_items(method, value, self.library.Plex.language))
elif "imdb" in method: items_found += check_map(self.config.IMDb.get_items(self.config, method, value, self.library.Plex.language)) elif "imdb" in method: items_found += check_map(self.config.IMDb.get_items(method, value, self.library.Plex.language))
elif "letterboxd" in method: items_found += check_map(self.config.Letterboxd.get_items(method, value, self.library.Plex.language)) elif "letterboxd" in method: items_found += check_map(self.config.Letterboxd.get_items(method, value, self.library.Plex.language))
elif "tmdb" in method: items_found += check_map(self.config.TMDb.get_items(method, value, self.library.is_movie)) elif "tmdb" in method: items_found += check_map(self.config.TMDb.get_items(method, value, self.library.is_movie))
elif "trakt" in method: items_found += check_map(self.config.Trakt.get_items(method, value, self.library.is_movie)) elif "trakt" in method: items_found += check_map(self.config.Trakt.get_items(method, value, self.library.is_movie))

@ -52,7 +52,6 @@ class Cache:
) )
self.expiration = expiration self.expiration = expiration
self.cache_path = cache self.cache_path = cache
self.omdb_expiration = expiration
def get_ids_from_imdb(self, imdb_id): def get_ids_from_imdb(self, imdb_id):
tmdb_id, tmdb_expired = self.get_tmdb_id("movie", imdb_id=imdb_id) tmdb_id, tmdb_expired = self.get_tmdb_id("movie", imdb_id=imdb_id)
@ -197,11 +196,11 @@ class Cache:
omdb_dict["Type"] = row["type"] if row["type"] else None omdb_dict["Type"] = row["type"] if row["type"] else None
datetime_object = datetime.strptime(row["expiration_date"], "%Y-%m-%d") datetime_object = datetime.strptime(row["expiration_date"], "%Y-%m-%d")
time_between_insertion = datetime.now() - datetime_object time_between_insertion = datetime.now() - datetime_object
expired = time_between_insertion.days > self.omdb_expiration expired = time_between_insertion.days > self.expiration
return omdb_dict, expired return omdb_dict, expired
def update_omdb(self, expired, omdb): def update_omdb(self, expired, omdb):
expiration_date = datetime.now() if expired is True else (datetime.now() - timedelta(days=random.randint(1, self.omdb_expiration))) expiration_date = datetime.now() if expired is True else (datetime.now() - timedelta(days=random.randint(1, self.expiration)))
with sqlite3.connect(self.cache_path) as connection: with sqlite3.connect(self.cache_path) as connection:
connection.row_factory = sqlite3.Row connection.row_factory = sqlite3.Row
with closing(connection.cursor()) as cursor: with closing(connection.cursor()) as cursor:

@ -180,8 +180,6 @@ class Config:
self.omdb = {} self.omdb = {}
try: try:
self.omdb["apikey"] = check_for_attribute(self.data, "apikey", parent="omdb", throw=True) self.omdb["apikey"] = check_for_attribute(self.data, "apikey", parent="omdb", throw=True)
self.omdb["omdb_cache"] = check_for_attribute(self.data, "omdb_cache", parent="omdb", options=" true (Use a cache to store data)\n false (Do not use a cache to store data)", var_type="bool", default=self.general["cache"])
self.omdb["omdb_cache_expiration"] = check_for_attribute(self.data, "omdb_cache_expiration", parent="omdb", var_type="int", default=self.general["cache_expiration"])
self.OMDb = OMDbAPI(self.omdb, Cache=self.Cache) self.OMDb = OMDbAPI(self.omdb, Cache=self.Cache)
except Failed as e: except Failed as e:
logger.error(e) logger.error(e)
@ -226,9 +224,9 @@ class Config:
else: else:
logger.warning("mal attribute not found") logger.warning("mal attribute not found")
self.TVDb = TVDbAPI(self, Cache=self.Cache, TMDb=self.TMDb, Trakt=self.Trakt) self.TVDb = TVDbAPI(self)
self.IMDb = IMDbAPI(Cache=self.Cache, TMDb=self.TMDb, Trakt=self.Trakt, TVDb=self.TVDb) if self.TMDb or self.Trakt else None self.IMDb = IMDbAPI(self)
self.AniDB = AniDBAPI(Cache=self.Cache, TMDb=self.TMDb, Trakt=self.Trakt) self.AniDB = AniDBAPI(self)
self.Letterboxd = LetterboxdAPI() self.Letterboxd = LetterboxdAPI()
util.separator() util.separator()

@ -7,23 +7,22 @@ from retrying import retry
logger = logging.getLogger("Plex Meta Manager") logger = logging.getLogger("Plex Meta Manager")
class IMDbAPI: class IMDbAPI:
def __init__(self, Cache=None, TMDb=None, Trakt=None, TVDb=None): def __init__(self, config):
if TMDb is None and Trakt is None: self.config = config
raise Failed("IMDb Error: IMDb requires either TMDb or Trakt") self.urls = {
self.Cache = Cache "list": "https://www.imdb.com/list/ls",
self.TMDb = TMDb "search": "https://www.imdb.com/search/title/?"
self.Trakt = Trakt }
self.TVDb = TVDb
def get_imdb_ids_from_url(self, imdb_url, language, limit): def get_imdb_ids_from_url(self, imdb_url, language, limit):
imdb_url = imdb_url.strip() imdb_url = imdb_url.strip()
if not imdb_url.startswith("https://www.imdb.com/list/ls") and not imdb_url.startswith("https://www.imdb.com/search/title/?"): if not imdb_url.startswith(self.urls["list"]) and not imdb_url.startswith(self.urls["search"]):
raise Failed(f"IMDb Error: {imdb_url} must begin with either:\n| https://www.imdb.com/list/ls (For Lists)\n| https://www.imdb.com/search/title/? (For Searches)") raise Failed(f"IMDb Error: {imdb_url} must begin with either:\n| {self.urls['list']} (For Lists)\n| {self.urls['search']} (For Searches)")
if imdb_url.startswith("https://www.imdb.com/list/ls"): if imdb_url.startswith(self.urls["list"]):
try: list_id = re.search("(\\d+)", str(imdb_url)).group(1) try: list_id = re.search("(\\d+)", str(imdb_url)).group(1)
except AttributeError: raise Failed(f"IMDb Error: Failed to parse List ID from {imdb_url}") except AttributeError: raise Failed(f"IMDb Error: Failed to parse List ID from {imdb_url}")
current_url = f"https://www.imdb.com/search/title/?lists=ls{list_id}" current_url = f"{self.urls['search']}lists=ls{list_id}"
else: else:
current_url = imdb_url current_url = imdb_url
header = {"Accept-Language": language} header = {"Accept-Language": language}
@ -52,7 +51,7 @@ class IMDbAPI:
def send_request(self, url, header): def send_request(self, url, header):
return html.fromstring(requests.get(url, headers=header).content) return html.fromstring(requests.get(url, headers=header).content)
def get_items(self, config, method, data, language, status_message=True): def get_items(self, method, data, language, status_message=True):
pretty = util.pretty_names[method] if method in util.pretty_names else method pretty = util.pretty_names[method] if method in util.pretty_names else method
if status_message: if status_message:
logger.debug(f"Data: {data}") logger.debug(f"Data: {data}")
@ -61,7 +60,7 @@ class IMDbAPI:
if method == "imdb_id": if method == "imdb_id":
if status_message: if status_message:
logger.info(f"Processing {pretty}: {data}") logger.info(f"Processing {pretty}: {data}")
tmdb_id, tvdb_id = config.convert_from_imdb(data, language) tmdb_id, tvdb_id = self.config.convert_from_imdb(data, language)
if tmdb_id: movie_ids.append(tmdb_id) if tmdb_id: movie_ids.append(tmdb_id)
if tvdb_id: show_ids.append(tvdb_id) if tvdb_id: show_ids.append(tvdb_id)
elif method == "imdb_list": elif method == "imdb_list":
@ -74,7 +73,7 @@ class IMDbAPI:
for i, imdb_id in enumerate(imdb_ids, 1): for i, imdb_id in enumerate(imdb_ids, 1):
length = util.print_return(length, f"Converting IMDb ID {i}/{total_ids}") length = util.print_return(length, f"Converting IMDb ID {i}/{total_ids}")
try: try:
tmdb_id, tvdb_id = config.convert_from_imdb(imdb_id, language) tmdb_id, tvdb_id = self.config.convert_from_imdb(imdb_id, language)
if tmdb_id: movie_ids.append(tmdb_id) if tmdb_id: movie_ids.append(tmdb_id)
if tvdb_id: show_ids.append(tvdb_id) if tvdb_id: show_ids.append(tvdb_id)
except Failed as e: logger.warning(e) except Failed as e: logger.warning(e)

@ -36,24 +36,21 @@ class OMDbAPI:
def __init__(self, params, Cache=None): def __init__(self, params, Cache=None):
self.url = "http://www.omdbapi.com/" self.url = "http://www.omdbapi.com/"
self.apikey = params["apikey"] self.apikey = params["apikey"]
self.cache = params["omdb_cache"]
self.cache_expiration = params["omdb_cache_expiration"]
self.limit = False self.limit = False
Cache.omdb_expiration = self.cache_expiration
self.Cache = Cache self.Cache = Cache
self.get_omdb("tt0080684") self.get_omdb("tt0080684")
#@retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_failed) @retry(stop_max_attempt_number=6, wait_fixed=10000, retry_on_exception=util.retry_if_not_failed)
def get_omdb(self, imdb_id): def get_omdb(self, imdb_id):
expired = None expired = None
if self.cache and self.Cache: if self.Cache:
omdb_dict, expired = self.Cache.query_omdb(imdb_id) omdb_dict, expired = self.Cache.query_omdb(imdb_id)
if omdb_dict and expired is False: if omdb_dict and expired is False:
return OMDbObj(omdb_dict) return OMDbObj(omdb_dict)
response = requests.get(self.url, params={"i": imdb_id, "apikey": self.apikey}) response = requests.get(self.url, params={"i": imdb_id, "apikey": self.apikey})
if response.status_code < 400: if response.status_code < 400:
omdb = OMDbObj(response.json()) omdb = OMDbObj(response.json())
if self.cache and self.Cache: if self.Cache:
self.Cache.update_omdb(expired, omdb) self.Cache.update_omdb(expired, omdb)
return omdb return omdb
else: else:

@ -54,11 +54,8 @@ class TVDbObj:
self.TVDb = TVDb self.TVDb = TVDb
class TVDbAPI: class TVDbAPI:
def __init__(self, config, Cache=None, TMDb=None, Trakt=None): def __init__(self, config):
self.config = config self.config = config
self.Cache = Cache
self.TMDb = TMDb
self.Trakt = Trakt
self.site_url = "https://www.thetvdb.com" self.site_url = "https://www.thetvdb.com"
self.alt_site_url = "https://thetvdb.com" self.alt_site_url = "https://thetvdb.com"
self.list_url = f"{self.site_url}/lists/" self.list_url = f"{self.site_url}/lists/"

Loading…
Cancel
Save