Plex-Meta-Manager/modules/anidb.py

145 lines
6 KiB
Python
Raw Normal View History

import time
2022-03-15 00:11:16 +00:00
from datetime import datetime
2021-01-20 21:37:59 +00:00
from modules import util
from modules.util import Failed
logger = util.logger
2021-01-20 21:37:59 +00:00
2021-07-13 20:53:49 +00:00
builders = ["anidb_id", "anidb_relation", "anidb_popular", "anidb_tag"]
2021-07-14 14:47:20 +00:00
base_url = "https://anidb.net"
urls = {
"anime": f"{base_url}/anime",
"popular": f"{base_url}/latest/anime/popular/?h=1",
"relation": "/relation/graph",
"tag": f"{base_url}/tag",
"login": f"{base_url}/perl-bin/animedb.pl"
}
2021-03-30 05:50:53 +00:00
2022-03-15 00:11:16 +00:00
class AniDBObj:
def __init__(self, anidb, anidb_id, language):
self.anidb = anidb
self.anidb_id = anidb_id
self.language = language
2022-03-16 21:42:13 +00:00
response = self.anidb._request(f"{urls['anime']}/{anidb_id}")
2022-03-15 00:11:16 +00:00
def parse_page(xpath, is_list=False, is_float=False, is_date=False, fail=False):
parse_results = response.xpath(xpath)
try:
if len(parse_results) > 0:
parse_results = [r.strip() for r in parse_results if len(r) > 0]
if parse_results:
if is_list:
return parse_results
elif is_float:
return float(parse_results[0])
elif is_date:
return datetime.strptime(parse_results[0], "%d.%m.%Y")
else:
return parse_results[0]
except (ValueError, TypeError):
pass
if fail:
raise Failed(f"AniDB Error: No Anime Found for AniDB ID: {self.anidb_id}")
elif is_list:
return []
elif is_float:
return 0
else:
return None
self.official_title = parse_page(f"//th[text()='Main Title']/parent::tr/td/span/text()", fail=True)
self.title = parse_page(f"//th[text()='Official Title']/parent::tr/td/span/span/span[text()='{self.language}']/parent::span/parent::span/parent::td/label/text()")
self.rating = parse_page(f"//th[text()='Rating']/parent::tr/td/span/a/span/text()", is_float=True)
self.average = parse_page(f"//th[text()='Average']/parent::tr/td/span/a/span/text()", is_float=True)
self.released = parse_page(f"//th[text()='Year']/parent::tr/td/span/text()", is_date=True)
self.tags = [g.capitalize() for g in parse_page("//th/a[text()='Tags']/parent::th/parent::tr/td/span/a/span/text()", is_list=True)]
self.description = response.xpath(f"string(//div[@itemprop='description'])")
2021-06-14 15:24:11 +00:00
class AniDB:
2022-03-15 00:11:16 +00:00
def __init__(self, config, language):
2021-03-05 20:33:24 +00:00
self.config = config
2022-03-15 00:11:16 +00:00
self.language = language
self.username = None
self.password = None
def login(self, username, password):
self.username = username
self.password = password
logger.secret(self.username)
logger.secret(self.password)
data = {"show": "main", "xuser": self.username, "xpass": self.password, "xdoautologin": "on"}
if not self._request(urls["login"], data=data).xpath("//li[@class='sub-menu my']/@title"):
2021-08-04 14:20:52 +00:00
raise Failed("AniDB Error: Login failed")
2021-07-11 08:12:52 +00:00
2022-03-15 00:11:16 +00:00
def _request(self, url, data=None):
2021-11-03 14:38:43 +00:00
if self.config.trace_mode:
logger.debug(f"URL: {url}")
if data:
2022-03-15 00:11:16 +00:00
return self.config.post_html(url, data=data, headers=util.header(self.language))
2021-07-14 14:47:20 +00:00
else:
2022-03-15 00:11:16 +00:00
return self.config.get_html(url, headers=util.header(self.language))
2021-07-11 08:12:52 +00:00
2022-03-15 00:11:16 +00:00
def _popular(self):
response = self._request(urls["popular"])
2022-07-15 13:56:54 +00:00
return util.get_int_list(response.xpath("//td[@class='thumb anime']/a/@href"), "AniDB ID")
2021-01-20 21:37:59 +00:00
2022-03-15 00:11:16 +00:00
def _relations(self, anidb_id):
response = self._request(f"{urls['anime']}/{anidb_id}{urls['relation']}")
2021-05-07 19:53:54 +00:00
return util.get_int_list(response.xpath("//area/@href"), "AniDB ID")
2022-03-15 00:11:16 +00:00
def _validate(self, anidb_id):
response = self._request(f"{urls['anime']}/{anidb_id}")
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
2022-03-15 00:11:16 +00:00
def validate_anidb_ids(self, anidb_ids):
2021-07-21 17:40:05 +00:00
anidb_list = util.get_int_list(anidb_ids, "AniDB ID")
2021-01-20 21:37:59 +00:00
anidb_values = []
for anidb_id in anidb_list:
try:
2022-03-15 00:11:16 +00:00
anidb_values.append(self._validate(anidb_id))
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
2022-03-15 00:11:16 +00:00
def _tag(self, tag, limit):
2021-07-11 08:12:52 +00:00
anidb_ids = []
2021-07-14 14:47:20 +00:00
current_url = f"{urls['tag']}/{tag}"
2021-07-13 20:53:49 +00:00
while True:
2022-03-15 00:11:16 +00:00
response = self._request(current_url)
2021-07-14 14:47:20 +00:00
anidb_ids.extend(util.get_int_list(response.xpath("//td[@class='name main anime']/a/@href"), "AniDB ID"))
2021-07-11 08:12:52 +00:00
next_page_list = response.xpath("//li[@class='next']/a/@href")
2021-07-13 20:53:49 +00:00
if len(anidb_ids) >= limit or len(next_page_list) == 0:
break
time.sleep(2)
2021-07-14 14:47:20 +00:00
current_url = f"{base_url}{next_page_list[0]}"
2021-07-13 20:53:49 +00:00
return anidb_ids[:limit]
2021-07-11 08:12:52 +00:00
2022-03-15 00:11:16 +00:00
def get_anime(self, anidb_id):
return AniDBObj(self, anidb_id, self.language)
def get_anidb_ids(self, method, data):
2021-04-16 05:45:43 +00:00
anidb_ids = []
2021-01-20 21:37:59 +00:00
if method == "anidb_popular":
2021-08-01 04:35:42 +00:00
logger.info(f"Processing AniDB Popular: {data} Anime")
2022-03-15 00:11:16 +00:00
anidb_ids.extend(self._popular()[:data])
2021-07-11 08:12:52 +00:00
elif method == "anidb_tag":
2021-08-01 04:35:42 +00:00
logger.info(f"Processing AniDB Tag: {data['limit'] if data['limit'] > 0 else 'All'} Anime from the Tag ID: {data['tag']}")
2022-03-15 00:11:16 +00:00
anidb_ids = self._tag(data["tag"], data["limit"])
2021-08-01 04:35:42 +00:00
elif method == "anidb_id":
logger.info(f"Processing AniDB ID: {data}")
anidb_ids.append(data)
elif method == "anidb_relation":
logger.info(f"Processing AniDB Relation: {data}")
2022-03-15 00:11:16 +00:00
anidb_ids.extend(self._relations(data))
2021-01-20 21:37:59 +00:00
else:
2021-08-01 04:35:42 +00:00
raise Failed(f"AniDB Error: Method {method} not supported")
2021-05-24 03:38:46 +00:00
logger.debug("")
2021-07-03 01:47:09 +00:00
logger.debug(f"{len(anidb_ids)} AniDB IDs Found: {anidb_ids}")
2021-08-07 06:01:21 +00:00
return anidb_ids