From 54b9d532623ce5732dfdbc8f80c30b08631111a6 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Wed, 3 Nov 2021 10:38:43 -0400 Subject: [PATCH] added trace --- modules/anidb.py | 16 +++++++++++----- modules/anilist.py | 5 +++++ modules/config.py | 1 + modules/icheckmovies.py | 2 ++ modules/imdb.py | 15 +++++++++------ modules/letterboxd.py | 6 ++++++ modules/mal.py | 4 ++++ modules/omdb.py | 2 ++ modules/tmdb.py | 2 ++ modules/trakt.py | 4 ++++ modules/tvdb.py | 4 ++++ plex_meta_manager.py | 9 ++++++--- 12 files changed, 56 insertions(+), 14 deletions(-) diff --git a/modules/anidb.py b/modules/anidb.py index 315a9cd8..713e312f 100644 --- a/modules/anidb.py +++ b/modules/anidb.py @@ -22,15 +22,21 @@ class AniDB: if params and not self._login(self.username, self.password).xpath("//li[@class='sub-menu my']/@title"): raise Failed("AniDB Error: Login failed") - def _request(self, url, language=None, post=None): - if post: - return self.config.post_html(url, post, headers=util.header(language)) + def _request(self, url, language=None, data=None): + if self.config.trace_mode: + logger.debug(f"URL: {url}") + if data: + return self.config.post_html(url, data=data, headers=util.header(language)) else: return self.config.get_html(url, headers=util.header(language)) def _login(self, username, password): - data = {"show": "main", "xuser": username, "xpass": password, "xdoautologin": "on"} - return self._request(urls["login"], post=data) + return self._request(urls["login"], data={ + "show": "main", + "xuser": username, + "xpass": password, + "xdoautologin": "on" + }) def _popular(self, language): response = self._request(urls["popular"], language=language) diff --git a/modules/anilist.py b/modules/anilist.py index c743cf91..eeff1c00 100644 --- a/modules/anilist.py +++ b/modules/anilist.py @@ -61,8 +61,13 @@ class AniList: self.options["Tag Category"][media_tag["category"].lower().replace(" ", "-")] = media_tag["category"] def _request(self, query, variables, level=1): + if self.config.trace_mode: + logger.debug(f"Query: {query}") + logger.debug(f"Variables: {variables}") response = self.config.post(base_url, json={"query": query, "variables": variables}) json_obj = response.json() + if self.config.trace_mode: + logger.debug(f"Response: {json_obj}") if "errors" in json_obj: if json_obj['errors'][0]['message'] == "Too Many Requests.": wait_time = int(response.headers["Retry-After"]) if "Retry-After" in response.headers else 0 diff --git a/modules/config.py b/modules/config.py index 9b29f1e6..47531f36 100644 --- a/modules/config.py +++ b/modules/config.py @@ -42,6 +42,7 @@ class Config: self.default_dir = default_dir self.test_mode = attrs["test"] if "test" in attrs else False + self.trace_mode = attrs["trace"] if "trace" in attrs else False self.run_start_time = attrs["time"] self.run_hour = datetime.strptime(attrs["time"], "%H:%M").hour self.requested_collections = util.get_list(attrs["collections"]) if "collections" in attrs else None diff --git a/modules/icheckmovies.py b/modules/icheckmovies.py index ebccc9bc..8bfaaef2 100644 --- a/modules/icheckmovies.py +++ b/modules/icheckmovies.py @@ -12,6 +12,8 @@ class ICheckMovies: self.config = config def _request(self, url, language, xpath): + if self.config.trace_mode: + logger.debug(f"URL: {url}") return self.config.get_html(url, headers=util.header(language)).xpath(xpath) def _parse_list(self, list_url, language): diff --git a/modules/imdb.py b/modules/imdb.py index dcc1e917..1a131125 100644 --- a/modules/imdb.py +++ b/modules/imdb.py @@ -66,9 +66,12 @@ class IMDb: parsed_url = urlparse(imdb_url) params = parse_qs(parsed_url.query) imdb_base = parsed_url._replace(query=None).geturl() - params.pop("start", None) - params.pop("count", None) - params.pop("page", None) + params.pop("start", None) # noqa + params.pop("count", None) # noqa + params.pop("page", None) # noqa + if self.config.trace_mode: + logger.debug(f"URL: {imdb_base}") + logger.debug(f"Params: {params}") if limit < 1 or total < limit: limit = total @@ -80,10 +83,10 @@ class IMDb: start_num = (i - 1) * item_count + 1 util.print_return(f"Parsing Page {i}/{num_of_pages} {start_num}-{limit if i == num_of_pages else i * item_count}") if imdb_base.startswith((urls["list"], urls["keyword"])): - params["page"] = i + params["page"] = i # noqa else: - params["count"] = remainder if i == num_of_pages else item_count - params["start"] = start_num + params["count"] = remainder if i == num_of_pages else item_count # noqa + params["start"] = start_num # noqa ids_found = self.config.get_html(imdb_base, headers=headers, params=params).xpath(xpath["imdb_id"]) if imdb_base.startswith((urls["list"], urls["keyword"])) and i == num_of_pages: ids_found = ids_found[:remainder] diff --git a/modules/letterboxd.py b/modules/letterboxd.py index 7ad7d0c5..d577646c 100644 --- a/modules/letterboxd.py +++ b/modules/letterboxd.py @@ -12,6 +12,8 @@ class Letterboxd: self.config = config def _parse_list(self, list_url, language): + if self.config.trace_mode: + logger.debug(f"URL: {list_url}") response = self.config.get_html(list_url, headers=util.header(language)) letterboxd_ids = response.xpath("//li[contains(@class, 'poster-container')]/div/@data-film-id") items = [] @@ -25,6 +27,8 @@ class Letterboxd: return items def _tmdb(self, letterboxd_url, language): + if self.config.trace_mode: + logger.debug(f"URL: {letterboxd_url}") response = self.config.get_html(letterboxd_url, headers=util.header(language)) ids = response.xpath("//a[@data-track-action='TMDb']/@href") if len(ids) > 0 and ids[0]: @@ -34,6 +38,8 @@ class Letterboxd: raise Failed(f"Letterboxd Error: TMDb Movie ID not found at {letterboxd_url}") def get_list_description(self, list_url, language): + if self.config.trace_mode: + logger.debug(f"URL: {list_url}") response = self.config.get_html(list_url, headers=util.header(language)) descriptions = response.xpath("//meta[@property='og:description']/@content") return descriptions[0] if len(descriptions) > 0 and len(descriptions[0]) > 0 else None diff --git a/modules/mal.py b/modules/mal.py index 196f2135..192e541d 100644 --- a/modules/mal.py +++ b/modules/mal.py @@ -128,7 +128,11 @@ class MyAnimeList: def _request(self, url, authorization=None): new_authorization = authorization if authorization else self.authorization + if self.config.trace_mode: + logger.debug(f"URL: {url}") response = self.config.get_json(url, headers={"Authorization": f"Bearer {new_authorization['access_token']}"}) + if self.config.trace_mode: + logger.debug(f"Response: {response}") if "error" in response: raise Failed(f"MyAnimeList Error: {response['error']}") else: return response diff --git a/modules/omdb.py b/modules/omdb.py index 45051c92..fa388d96 100644 --- a/modules/omdb.py +++ b/modules/omdb.py @@ -48,6 +48,8 @@ class OMDb: omdb_dict, expired = self.config.Cache.query_omdb(imdb_id) if omdb_dict and expired is False: return OMDbObj(imdb_id, omdb_dict) + if self.config.trace_mode: + logger.debug(f"IMDb ID: {imdb_id}") response = self.config.get(base_url, params={"i": imdb_id, "apikey": self.apikey}) if response.status_code < 400: omdb = OMDbObj(imdb_id, response.json()) diff --git a/modules/tmdb.py b/modules/tmdb.py index 7fee8109..b5c3bf4c 100644 --- a/modules/tmdb.py +++ b/modules/tmdb.py @@ -202,6 +202,8 @@ class TMDb: for date_attr in discover_dates: if date_attr in attrs: attrs[date_attr] = util.validate_date(attrs[date_attr], f"tmdb_discover attribute {date_attr}", return_as="%Y-%m-%d") + if self.config.trace_mode: + logger.debug(f"Params: {attrs}") self.Discover.discover_movies(attrs) if is_movie else self.Discover.discover_tv_shows(attrs) total_pages = int(self.TMDb.total_pages) total_results = int(self.TMDb.total_results) diff --git a/modules/trakt.py b/modules/trakt.py index 899fd2f1..d0bc2d04 100644 --- a/modules/trakt.py +++ b/modules/trakt.py @@ -107,6 +107,8 @@ class Trakt: output_json = [] pages = 1 current = 1 + if self.config.trace_mode: + logger.debug(f"URL: {base_url}{url}") while current <= pages: if pages == 1: response = self.config.get(f"{base_url}{url}", headers=headers) @@ -116,6 +118,8 @@ class Trakt: response = self.config.get(f"{base_url}{url}?page={current}", headers=headers) if response.status_code == 200: json_data = response.json() + if self.config.trace_mode: + logger.debug(f"Response: {json_data}") if isinstance(json_data, dict): return json_data else: diff --git a/modules/tvdb.py b/modules/tvdb.py index 532f8d9e..d9a05322 100644 --- a/modules/tvdb.py +++ b/modules/tvdb.py @@ -27,6 +27,8 @@ class TVDbObj: else: raise Failed(f"TVDb Error: {self.tvdb_url} must begin with {urls['movies'] if self.is_movie else urls['series']}") + if self.config.trace_mode: + logger.debug(f"URL: {tvdb_url}") response = self.config.get_html(self.tvdb_url, headers=util.header(self.language)) results = response.xpath(f"//*[text()='TheTVDB.com {self.media_type} ID']/parent::node()/span/text()") if len(results) > 0: @@ -111,6 +113,8 @@ class TVDb: def _ids_from_url(self, tvdb_url): ids = [] tvdb_url = tvdb_url.strip() + if self.config.trace_mode: + logger.debug(f"URL: {tvdb_url}") if tvdb_url.startswith((urls["list"], urls["alt_list"])): try: response = self.config.get_html(tvdb_url, headers=util.header(self.tvdb_language)) diff --git a/plex_meta_manager.py b/plex_meta_manager.py index 38b1429d..b75c205f 100644 --- a/plex_meta_manager.py +++ b/plex_meta_manager.py @@ -17,6 +17,7 @@ if sys.version_info[0] != 3 or sys.version_info[1] < 6: parser = argparse.ArgumentParser() parser.add_argument("-db", "--debug", dest="debug", help=argparse.SUPPRESS, action="store_true", default=False) +parser.add_argument("-tr", "--trace", dest="trace", help=argparse.SUPPRESS, action="store_true", default=False) parser.add_argument("-c", "--config", dest="config", help="Run with desired *.yml file", type=str) parser.add_argument("-t", "--time", "--times", dest="times", help="Times to update each day use format HH:MM (Default: 03:00) (comma-separated list)", default="03:00", type=str) parser.add_argument("-re", "--resume", dest="resume", help="Resume collection run from a specific collection", type=str) @@ -51,6 +52,7 @@ def get_arg(env_str, default, arg_bool=False, arg_int=False): test = get_arg("PMM_TEST", args.test, arg_bool=True) debug = get_arg("PMM_DEBUG", args.debug, arg_bool=True) +trace = get_arg("PMM_TRACE", args.trace, arg_bool=True) run = get_arg("PMM_RUN", args.run, arg_bool=True) no_countdown = get_arg("PMM_NO_COUNTDOWN", args.no_countdown, arg_bool=True) no_missing = get_arg("PMM_NO_MISSING", args.no_missing, arg_bool=True) @@ -93,7 +95,7 @@ def fmt_filter(record): return True cmd_handler = logging.StreamHandler() -cmd_handler.setLevel(logging.DEBUG if test or debug else logging.INFO) +cmd_handler.setLevel(logging.DEBUG if test or debug or trace else logging.INFO) logger.addHandler(cmd_handler) @@ -625,7 +627,8 @@ try: "test": test, "collections": collections, "libraries": libraries, - "resume": resume + "resume": resume, + "trace": trace }) else: times_to_run = util.get_list(times) @@ -639,7 +642,7 @@ try: else: raise Failed(f"Argument Error: blank time argument") for time_to_run in valid_times: - schedule.every().day.at(time_to_run).do(start, {"config_file": config_file, "time": time_to_run}) + schedule.every().day.at(time_to_run).do(start, {"config_file": config_file, "time": time_to_run, "trace": trace}) while True: schedule.run_pending() if not no_countdown: