Plex-Meta-Manager/modules/anidb.py

96 lines
4.3 KiB
Python
Raw Normal View History

2021-07-14 14:47:20 +00:00
import logging, time
2021-01-20 21:37:59 +00:00
from modules import util
from modules.util import Failed
logger = logging.getLogger("Plex Meta Manager")
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
2021-06-14 15:24:11 +00:00
class AniDB:
2021-07-14 14:47:20 +00:00
def __init__(self, config, params):
2021-03-05 20:33:24 +00:00
self.config = config
2021-07-14 14:47:20 +00:00
self.username = params["username"] if params else None
self.password = params["password"] if params else None
2021-07-13 20:53:49 +00:00
if params:
2021-07-14 14:47:20 +00:00
if not self._login(self.username, self.password).xpath("//li[@class='sub-menu my']/@title"):
2021-07-11 08:12:52 +00:00
raise Failed("AniDB Error: Login failed")
2021-07-14 14:47:20 +00:00
def _request(self, url, language=None, postData=None):
if postData:
return self.config.post_html(url, postData, headers=util.header(language))
else:
return self.config.get_html(url, headers=util.header(language))
2021-07-11 08:12:52 +00:00
def _login(self, username, password):
2021-07-14 14:47:20 +00:00
data = {"show": "main", "xuser": username, "xpass": password, "xdoautologin": "on"}
return self._request(urls["login"], postData=data)
2021-01-20 21:37:59 +00:00
2021-05-07 19:53:54 +00:00
def _popular(self, language):
2021-07-14 14:47:20 +00:00
response = self._request(urls["popular"], language=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):
2021-07-14 14:47:20 +00:00
response = self._request(f"{urls['anime']}/{anidb_id}{urls['relation']}", language=language)
2021-05-07 19:53:54 +00:00
return util.get_int_list(response.xpath("//area/@href"), "AniDB ID")
def _validate(self, anidb_id, language):
2021-07-14 14:47:20 +00:00
response = self._request(f"{urls['anime']}/{anidb_id}", language=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
2021-07-21 17:40:05 +00:00
def validate_anidb_ids(self, anidb_ids, language):
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:
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-07-11 08:12:52 +00:00
def _tag(self, tag, limit, language):
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:
2021-07-14 14:47:20 +00:00
response = self._request(current_url, language=language)
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
2021-05-09 05:37:45 +00:00
def get_items(self, method, data, language):
2021-01-20 21:37:59 +00:00
pretty = util.pretty_names[method] if method in util.pretty_names else method
2021-04-16 05:45:43 +00:00
anidb_ids = []
2021-01-20 21:37:59 +00:00
if method == "anidb_popular":
2021-05-09 05:37:45 +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-07-11 08:12:52 +00:00
elif method == "anidb_tag":
anidb_ids = self._tag(data["tag"], data["limit"], language)
logger.info(f"Processing {pretty}: {data['limit'] if data['limit'] > 0 else 'All'} Anime from the Tag ID: {data['tag']}")
2021-01-20 21:37:59 +00:00
else:
2021-05-09 05:37:45 +00:00
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-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}")
logger.debug(f"{len(movie_ids)} TMDb IDs Found: {movie_ids}")
logger.debug(f"{len(show_ids)} TVDb IDs Found: {show_ids}")
2021-07-13 20:53:49 +00:00
return movie_ids, show_ids