change up anidb login

This commit is contained in:
meisnate12 2021-07-13 16:53:49 -04:00
parent fe80501bfd
commit 97a01a6496
3 changed files with 34 additions and 37 deletions

View file

@ -1,4 +1,4 @@
import logging, requests,time import logging, requests, time
from lxml import html from lxml import html
from modules import util from modules import util
from modules.util import Failed from modules.util import Failed
@ -6,15 +6,12 @@ from retrying import retry
logger = logging.getLogger("Plex Meta Manager") logger = logging.getLogger("Plex Meta Manager")
builders = ["anidb_id", "anidb_relation", "anidb_popular", 'anidb_tag'] builders = ["anidb_id", "anidb_relation", "anidb_popular", "anidb_tag"]
class AniDB: class AniDB:
def __init__(self, params, config): def __init__(self, params, config):
self.config = config self.config = config
# Create a session so if we login we can continue to use the same session
self.anidb_session = requests.Session()
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",
@ -22,18 +19,15 @@ class AniDB:
"anidb_tag": "https://anidb.net/tag", "anidb_tag": "https://anidb.net/tag",
"login": "https://anidb.net/perl-bin/animedb.pl" "login": "https://anidb.net/perl-bin/animedb.pl"
} }
if params:
if params and "username" in params and "password" in params: if not self._login(params["username"], params["password"]).xpath("//li[@class='sub-menu my']/@title"):
result = str(self._login(params["username"], params["password"]).content)
# Login response does not use proper status codes so we have to check the content of the document
if "Wrong username/password" in result:
raise Failed("AniDB Error: Login failed") raise Failed("AniDB Error: Login failed")
@retry(stop_max_attempt_number=6, wait_fixed=10000) @retry(stop_max_attempt_number=6, wait_fixed=10000)
def _request(self, url, language): def _request(self, url, language):
return html.fromstring(self.anidb_session.get(url, headers={"Accept-Language": language, "User-Agent": "Mozilla/5.0 x64"}).content) return html.fromstring(self.config.session.get(url, headers={"Accept-Language": language, "User-Agent": "Mozilla/5.0 x64"}).content)
@retry(stop_max_attempt_number=6, wait_fixed=10000)
def _login(self, username, password): def _login(self, username, password):
data = { data = {
"show": "main", "show": "main",
@ -41,7 +35,7 @@ class AniDB:
"xpass": password, "xpass": password,
"xdoautologin": "on" "xdoautologin": "on"
} }
return self.anidb_session.post(self.urls["login"], data, headers={"Accept-Language": "en-US,en;q=0.5", "User-Agent": "Mozilla/5.0 x64"}) return html.fromstring(self.config.session.post(self.urls["login"], data, headers={"Accept-Language": "en-US,en;q=0.5", "User-Agent": "Mozilla/5.0 x64"}).content)
def _popular(self, language): def _popular(self, language):
response = self._request(self.urls["popular"], language) response = self._request(self.urls["popular"], language)
@ -71,24 +65,17 @@ class AniDB:
def _tag(self, tag, limit, language): def _tag(self, tag, limit, language):
anidb_ids = [] anidb_ids = []
next_page = True current_url = f"{self.urls['anidb_tag']}/{tag}"
current_url = self.urls["anidb_tag"] + "/" + str(tag) while True:
while next_page:
logger.debug(f"Sending request to {current_url}")
response = self._request(current_url, language) response = self._request(current_url, language)
int_list = util.get_int_list(response.xpath("//td[@class='name main anime']/a/@href"), "AniDB ID") int_list = util.get_int_list(response.xpath("//td[@class='name main anime']/a/@href"), "AniDB ID")
anidb_ids.extend(int_list) anidb_ids.extend(int_list)
next_page_list = response.xpath("//li[@class='next']/a/@href") next_page_list = response.xpath("//li[@class='next']/a/@href")
logger.debug(f"next page list {next_page_list}") if len(anidb_ids) >= limit or len(next_page_list) == 0:
if len(next_page_list) != 0 and len(anidb_ids) <= limit: break
logger.debug(f"Loading next anidb page") time.sleep(2)
time.sleep(2)# Sleep as we are paging through anidb and don't want the ban hammer current_url = f"https://anidb.net{next_page_list[0]}"
current_url = "https://anidb.net" + next_page_list[0] return anidb_ids[:limit]
else:
logger.debug(f"Got to last page")
next_page = False
anidb_ids = anidb_ids[:limit]
return anidb_ids
def get_items(self, method, data, language): def get_items(self, method, data, language):
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

View file

@ -61,6 +61,7 @@ modifier_alias = {".greater": ".gt", ".less": ".lt"}
all_builders = anidb.builders + anilist.builders + icheckmovies.builders + imdb.builders + letterboxd.builders + mal.builders + plex.builders + tautulli.builders + tmdb.builders + trakttv.builders + tvdb.builders all_builders = anidb.builders + anilist.builders + icheckmovies.builders + imdb.builders + letterboxd.builders + mal.builders + plex.builders + tautulli.builders + tmdb.builders + trakttv.builders + tvdb.builders
dictionary_builders = [ dictionary_builders = [
"filters", "filters",
"anidb_tag",
"anilist_genre", "anilist_genre",
"anilist_season", "anilist_season",
"anilist_tag", "anilist_tag",
@ -950,6 +951,17 @@ class CollectionBuilder:
new_dictionary["limit"] = get_int(method_name, "limit", dict_data, dict_methods, 100, maximum=1000) new_dictionary["limit"] = get_int(method_name, "limit", dict_data, dict_methods, 100, maximum=1000)
self.methods.append((method_name, [new_dictionary])) self.methods.append((method_name, [new_dictionary]))
elif method_name == "anidb_tag":
new_dictionary = {}
dict_methods = {dm.lower(): dm for dm in dict_data}
if "tag" not in dict_methods:
raise Failed("Collection Error: anidb_tag tag attribute is required")
elif not dict_data[dict_methods["tag"]]:
raise Failed("Collection Error: anidb_tag tag attribute is blank")
else:
new_dictionary["tag"] = util.regex_first_int(dict_data[dict_methods["username"]], "AniDB Tag ID")
new_dictionary["limit"] = get_int(method_name, "limit", dict_data, dict_methods, 0, minimum=0)
self.methods.append((method_name, [new_dictionary]))
elif "anilist" in method_name: elif "anilist" in method_name:
new_dictionary = {"sort_by": "score"} new_dictionary = {"sort_by": "score"}
dict_methods = {dm.lower(): dm for dm in dict_data} dict_methods = {dm.lower(): dm for dm in dict_data}

View file

@ -1,4 +1,4 @@
import logging, os import logging, os, requests
from datetime import datetime from datetime import datetime
from modules import util from modules import util
from modules.anidb import AniDB from modules.anidb import AniDB
@ -188,6 +188,8 @@ class Config:
util.print_multiline(options) util.print_multiline(options)
return default return default
self.session = requests.Session()
self.general = {} self.general = {}
self.general["cache"] = check_for_attribute(self.data, "cache", parent="settings", var_type="bool", default=True) self.general["cache"] = check_for_attribute(self.data, "cache", parent="settings", var_type="bool", default=True)
self.general["cache_expiration"] = check_for_attribute(self.data, "cache_expiration", parent="settings", var_type="int", default=60) self.general["cache_expiration"] = check_for_attribute(self.data, "cache_expiration", parent="settings", var_type="int", default=60)
@ -274,9 +276,8 @@ class Config:
util.separator() util.separator()
self.AniDB = None self.AniDB = None
anidb_username = check_for_attribute(self.data, "username", parent="anidb", throw=False, default=False) if "anidb" in self.data:
anidb_password = check_for_attribute(self.data, "username", parent="anidb", throw=False, default=False) util.separator()
if "anidb" in self.data and anidb_username and anidb_password:
logger.info("Connecting to AniDB...") logger.info("Connecting to AniDB...")
self.anidb = {} self.anidb = {}
try: try:
@ -285,13 +286,10 @@ class Config:
self.AniDB = AniDB(self.anidb, self) self.AniDB = AniDB(self.anidb, self)
except Failed as e: except Failed as e:
logger.error(e) logger.error(e)
logger.info(f"My Anime List Connection {'Failed' if self.MyAnimeList is None else 'Successful'}") logger.info(f"My Anime List Connection {'Failed Continuing as Guest ' if self.MyAnimeList is None else 'Successful'}")
else: if self.AniDB is None:
logger.info("Using guest authentication for AniDB")
self.AniDB = AniDB(None, self) self.AniDB = AniDB(None, self)
util.separator()
self.TVDb = TVDb(self) self.TVDb = TVDb(self)
self.IMDb = IMDb(self) self.IMDb = IMDb(self)
self.Convert = Convert(self) self.Convert = Convert(self)