Plex-Meta-Manager/modules/anidb.py

70 lines
3 KiB
Python
Raw Normal View History

2021-01-20 21:37:59 +00:00
import logging, requests
from lxml import html
from modules import util
from modules.util import Failed
from retrying import retry
logger = logging.getLogger("Plex Meta Manager")
2021-03-30 05:50:53 +00:00
builders = ["anidb_id", "anidb_relation", "anidb_popular"]
2021-01-20 21:37:59 +00:00
class AniDBAPI:
2021-03-05 20:33:24 +00:00
def __init__(self, config):
self.config = config
2021-01-20 21:37:59 +00:00
self.urls = {
"anime": "https://anidb.net/anime",
"popular": "https://anidb.net/latest/anime/popular/?h=1",
"relation": "/relation/graph"
}
@retry(stop_max_attempt_number=6, wait_fixed=10000)
2021-05-07 19:53:54 +00:00
def _request(self, url, language):
2021-01-21 21:42:31 +00:00
return html.fromstring(requests.get(url, headers={"Accept-Language": language, "User-Agent": "Mozilla/5.0 x64"}).content)
2021-01-20 21:37:59 +00:00
2021-05-07 19:53:54 +00:00
def _popular(self, language):
response = self._request(self.urls["popular"], language)
2021-01-20 21:37:59 +00:00
return util.get_int_list(response.xpath("//td[@class='name anime']/a/@href"), "AniDB ID")
2021-05-07 19:53:54 +00:00
def _relations(self, anidb_id, language):
response = self._request(f"{self.urls['anime']}/{anidb_id}{self.urls['relation']}", language)
return util.get_int_list(response.xpath("//area/@href"), "AniDB ID")
def _validate(self, anidb_id, language):
response = self._request(f"{self.urls['anime']}/{anidb_id}", language)
2021-02-24 06:44:06 +00:00
ids = response.xpath(f"//*[text()='a{anidb_id}']/text()")
2021-01-20 21:37:59 +00:00
if len(ids) > 0:
return util.regex_first_int(ids[0], "AniDB ID")
2021-02-24 06:44:06 +00:00
raise Failed(f"AniDB Error: AniDB ID: {anidb_id} not found")
2021-01-20 21:37:59 +00:00
def validate_anidb_list(self, anidb_list, language):
anidb_values = []
for anidb_id in anidb_list:
try:
2021-05-07 19:53:54 +00:00
anidb_values.append(self._validate(anidb_id, language))
2021-01-20 21:37:59 +00:00
except Failed as e:
logger.error(e)
if len(anidb_values) > 0:
return anidb_values
2021-02-24 06:44:06 +00:00
raise Failed(f"AniDB Error: No valid AniDB IDs in {anidb_list}")
2021-01-20 21:37:59 +00:00
2021-03-05 20:33:24 +00:00
def get_items(self, method, data, language, status_message=True):
2021-01-20 21:37:59 +00:00
pretty = util.pretty_names[method] if method in util.pretty_names else method
if status_message:
2021-02-24 06:44:06 +00:00
logger.debug(f"Data: {data}")
2021-04-16 05:45:43 +00:00
anidb_ids = []
2021-01-20 21:37:59 +00:00
if method == "anidb_popular":
if status_message:
2021-02-24 06:44:06 +00:00
logger.info(f"Processing {pretty}: {data} Anime")
2021-05-07 19:53:54 +00:00
anidb_ids.extend(self._popular(language)[:data])
2021-01-20 21:37:59 +00:00
else:
2021-02-24 06:44:06 +00:00
if status_message: logger.info(f"Processing {pretty}: {data}")
2021-04-16 05:45:43 +00:00
if method == "anidb_id": anidb_ids.append(data)
2021-05-07 19:53:54 +00:00
elif method == "anidb_relation": anidb_ids.extend(self._relations(data, language))
2021-02-24 06:44:06 +00:00
else: raise Failed(f"AniDB Error: Method {method} not supported")
2021-05-08 23:49:55 +00:00
movie_ids, show_ids = self.config.Convert.anidb_to_ids(anidb_ids)
2021-01-20 21:37:59 +00:00
if status_message:
2021-04-16 05:45:43 +00:00
logger.debug(f"AniDB IDs Found: {anidb_ids}")
2021-02-24 06:44:06 +00:00
logger.debug(f"TMDb IDs Found: {movie_ids}")
logger.debug(f"TVDb IDs Found: {show_ids}")
2021-01-20 21:37:59 +00:00
return movie_ids, show_ids