diff --git a/modules/builder.py b/modules/builder.py index 7ac0b2b0..2892b140 100644 --- a/modules/builder.py +++ b/modules/builder.py @@ -560,7 +560,7 @@ class CollectionBuilder: if isinstance(imdb_list, dict): dict_methods = {dm.lower(): dm for dm in imdb_list} if "url" in dict_methods and imdb_list[dict_methods["url"]]: - imdb_url = config.IMDb.validate_imdb_url(imdb_list[dict_methods["url"]]) + imdb_url = config.IMDb.validate_imdb_url(imdb_list[dict_methods["url"]], self.library.Plex.language) else: raise Failed("Collection Error: imdb_list attribute url is required") if "limit" in dict_methods and imdb_list[dict_methods["limit"]]: @@ -568,7 +568,7 @@ class CollectionBuilder: else: list_count = 0 else: - imdb_url = config.IMDb.validate_imdb_url(str(imdb_list)) + imdb_url = config.IMDb.validate_imdb_url(str(imdb_list), self.library.Plex.language) list_count = 0 new_list.append({"url": imdb_url, "limit": list_count}) self.methods.append((method_name, new_list)) diff --git a/modules/imdb.py b/modules/imdb.py index b57b8fad..c4de979b 100644 --- a/modules/imdb.py +++ b/modules/imdb.py @@ -17,24 +17,29 @@ class IMDbAPI: "keyword": "https://www.imdb.com/search/keyword/?" } - def validate_imdb_url(self, imdb_url): + def validate_imdb_url(self, imdb_url, language): imdb_url = imdb_url.strip() if not imdb_url.startswith(self.urls["list"]) and not imdb_url.startswith(self.urls["search"]) and not imdb_url.startswith(self.urls["keyword"]): raise Failed(f"IMDb Error: {imdb_url} must begin with either:\n{self.urls['list']} (For Lists)\n{self.urls['search']} (For Searches)\n{self.urls['keyword']} (For Keyword Searches)") - return imdb_url + total, _ = self.get_total(self.fix_url(imdb_url), language) + if total > 0: + return imdb_url + raise Failed(f"IMDb Error: {imdb_url} failed to parse") - def get_imdb_ids_from_url(self, imdb_url, language, limit): + def fix_url(self, imdb_url): if imdb_url.startswith(self.urls["list"]): try: list_id = re.search("(\\d+)", str(imdb_url)).group(1) except AttributeError: raise Failed(f"IMDb Error: Failed to parse List ID from {imdb_url}") - current_url = f"{self.urls['search']}lists=ls{list_id}" + return f"{self.urls['search']}lists=ls{list_id}" + elif imdb_url.endswith("/"): + return imdb_url[:-1] else: - current_url = imdb_url + return imdb_url + + def get_total(self, imdb_url, language): header = {"Accept-Language": language} - length = 0 - imdb_ids = [] if imdb_url.startswith(self.urls["keyword"]): - results = self.send_request(current_url, header).xpath("//div[@class='desc']/text()") + results = self.send_request(imdb_url, header).xpath("//div[@class='desc']/text()") total = None for result in results: if "title" in result: @@ -45,13 +50,20 @@ class IMDbAPI: pass if total is None: raise Failed(f"IMDb Error: No Results at URL: {imdb_url}") - item_count = 50 + return total, 50 else: - try: results = self.send_request(current_url, header).xpath("//div[@class='desc']/span/text()")[0].replace(",", "") + try: results = self.send_request(imdb_url, header).xpath("//div[@class='desc']/span/text()")[0].replace(",", "") except IndexError: raise Failed(f"IMDb Error: Failed to parse URL: {imdb_url}") try: total = int(re.findall("(\\d+) title", results)[0]) except IndexError: raise Failed(f"IMDb Error: No Results at URL: {imdb_url}") - item_count = 250 + return total, 250 + + def get_imdb_ids_from_url(self, imdb_url, language, limit): + current_url = self.fix_url(imdb_url) + total, item_count = self.get_total(current_url, language) + header = {"Accept-Language": language} + length = 0 + imdb_ids = [] if "&start=" in current_url: current_url = re.sub("&start=\\d+", "", current_url) if "&count=" in current_url: current_url = re.sub("&count=\\d+", "", current_url) if "&page=" in current_url: current_url = re.sub("&page=\\d+", "", current_url)