You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Plex-Meta-Manager/modules/anidb.py

67 lines
2.9 KiB

4 years ago
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")
builders = ["anidb_id", "anidb_relation", "anidb_popular"]
4 years ago
class AniDB:
4 years ago
def __init__(self, config):
self.config = config
4 years ago
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)
4 years ago
def _request(self, url, language):
4 years ago
return html.fromstring(requests.get(url, headers={"Accept-Language": language, "User-Agent": "Mozilla/5.0 x64"}).content)
4 years ago
4 years ago
def _popular(self, language):
response = self._request(self.urls["popular"], language)
4 years ago
return util.get_int_list(response.xpath("//td[@class='name anime']/a/@href"), "AniDB ID")
4 years ago
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)
ids = response.xpath(f"//*[text()='a{anidb_id}']/text()")
4 years ago
if len(ids) > 0:
return util.regex_first_int(ids[0], "AniDB ID")
raise Failed(f"AniDB Error: AniDB ID: {anidb_id} not found")
4 years ago
def validate_anidb_list(self, anidb_list, language):
anidb_values = []
for anidb_id in anidb_list:
try:
4 years ago
anidb_values.append(self._validate(anidb_id, language))
4 years ago
except Failed as e:
logger.error(e)
if len(anidb_values) > 0:
return anidb_values
raise Failed(f"AniDB Error: No valid AniDB IDs in {anidb_list}")
4 years ago
def get_items(self, method, data, language):
4 years ago
pretty = util.pretty_names[method] if method in util.pretty_names else method
anidb_ids = []
4 years ago
if method == "anidb_popular":
logger.info(f"Processing {pretty}: {data} Anime")
4 years ago
anidb_ids.extend(self._popular(language)[:data])
4 years ago
else:
logger.info(f"Processing {pretty}: {data}")
if method == "anidb_id": anidb_ids.append(data)
4 years ago
elif method == "anidb_relation": anidb_ids.extend(self._relations(data, language))
else: raise Failed(f"AniDB Error: Method {method} not supported")
4 years ago
movie_ids, show_ids = self.config.Convert.anidb_to_ids(anidb_ids)
logger.debug("")
logger.debug(f"AniDB IDs Found: {anidb_ids}")
logger.debug(f"TMDb IDs Found: {movie_ids}")
logger.debug(f"TVDb IDs Found: {show_ids}")
4 years ago
return movie_ids, show_ids