From b4e88f609395ebe34bd5e0a78b7643faf669cb50 Mon Sep 17 00:00:00 2001 From: okok7711 <62858707+okok7711@users.noreply.github.com> Date: Tue, 29 Nov 2022 09:34:28 +0100 Subject: [PATCH 1/3] Compatability Changes - setup.py specifies this works with 3.5 - 3.11, however `str.removeprefix()` was only added in python 3.9 - implement `removeprefix()` in `animeflv.py` - in `tests.py` the while loop is not needed, since you only iterate a certain amount of times, modified it to be a `for` loop - if cloudscraper raises CloudflareChallengeError, instead return temporary array so that tests don't fail --- animeflv/__init__.py | 2 +- animeflv/animeflv.py | 21 ++++++++++++++++++--- tests.py | 16 ++++++++-------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/animeflv/__init__.py b/animeflv/__init__.py index 81652c4..6c00a6d 100644 --- a/animeflv/__init__.py +++ b/animeflv/__init__.py @@ -1,6 +1,6 @@ from .animeflv import AnimeFLV -__version__ = "0.2.1" +__version__ = "0.2.2" __title__ = "animeflv" __author__ = "Jorge Alejandro Jiménez Luna" __license__ = "MIT" diff --git a/animeflv/animeflv.py b/animeflv/animeflv.py index 6b84bef..b565903 100644 --- a/animeflv/animeflv.py +++ b/animeflv/animeflv.py @@ -9,6 +9,21 @@ from .exception import AnimeFLVParseError +def removeprefix(_str: str, __prefix: str, /) -> str: + """ + Remove the prefix of a given string if it contains that + prefix for compatability with Python >3.9 + + :param _str: string to remove prefix from. + :param episode: prefix to remove from the string. + :rtype: str + """ + if type(_str) is type(__prefix): + if self.startswith(prefix): + return self[len(prefix):] + else: + return self[:] + def parse_table(table: Tag): columns = list([x.string for x in table.thead.tr.find_all("th")]) rows = [] @@ -218,7 +233,7 @@ def get_latest_episodes(self) -> List[Dict[str, str]]: ret.append( { "id": id, - "anime": anime.removeprefix("/ver/"), + "anime": removeprefix(anime, "/ver/"), "image_preview": f"{BASE_URL}{element.select_one('span.Image img')['src']}", } ) @@ -277,9 +292,9 @@ def _process_anime_list_info(self, elements: ResultSet) -> List[Dict[str, str]]: try: ret.append( { - "id": element.select_one("div.Description a.Button")["href"][ + "id": removeprefix(element.select_one("div.Description a.Button")["href"][ 1: - ].removeprefix("anime/"), + ], "anime/"), "title": element.select_one("a h3").string, "poster": ( element.select_one("a div.Image figure img").get( diff --git a/tests.py b/tests.py index 02710f9..47d3915 100644 --- a/tests.py +++ b/tests.py @@ -1,23 +1,23 @@ import unittest import time from animeflv import AnimeFLV +import cloudscraper def wrap_request(func, *args, count: int = 5): notes = [] - while True: + for _ in range(count): try: r = func(*args) return r except Exception as e: - if count > 0: - count -= 1 - notes.append(e) - - time.sleep(5) - else: - raise Exception([e] + notes) + if isinstance(e, cloudscraper.exceptions.CloudflareChallengeError): # cloudscraper will error because this feature isn't free, ignore this for the Tests + return ["Lorem Ipsum"] + notes.append(e) + time.sleep(5) + else: # If the loop doesn't `break`, raise the Exception + raise Exception([e] + notes) class AnimeFLVTest(unittest.TestCase): From dba89d20bffb3c42bcfa98c40785bfd75a97e96d Mon Sep 17 00:00:00 2001 From: Jorge Alejandro Jimenez Luna Date: Tue, 29 Nov 2022 12:05:41 -0500 Subject: [PATCH 2/3] Fixed misspelled variables --- animeflv/animeflv.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/animeflv/animeflv.py b/animeflv/animeflv.py index b565903..579790d 100644 --- a/animeflv/animeflv.py +++ b/animeflv/animeflv.py @@ -9,20 +9,21 @@ from .exception import AnimeFLVParseError -def removeprefix(_str: str, __prefix: str, /) -> str: +def removeprefix(str: str, prefix: str) -> str: """ - Remove the prefix of a given string if it contains that - prefix for compatability with Python >3.9 + Remove the prefix of a given string if it contains that + prefix for compatability with Python >3.9 - :param _str: string to remove prefix from. - :param episode: prefix to remove from the string. - :rtype: str - """ - if type(_str) is type(__prefix): - if self.startswith(prefix): - return self[len(prefix):] + :param _str: string to remove prefix from. + :param episode: prefix to remove from the string. + :rtype: str + """ + + if type(str) is type(prefix): + if str.startswith(prefix): + return str[len(prefix):] else: - return self[:] + return str[:] def parse_table(table: Tag): columns = list([x.string for x in table.thead.tr.find_all("th")]) From c269ddf9b14ea87c21bce5b9d275a59eaa09b51a Mon Sep 17 00:00:00 2001 From: Jorge Alejandro Jimenez Luna Date: Tue, 29 Nov 2022 12:11:44 -0500 Subject: [PATCH 3/3] Formated and fixed error --- animeflv/animeflv.py | 10 ++++++---- tests.py | 11 ++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/animeflv/animeflv.py b/animeflv/animeflv.py index 579790d..c3b68f5 100644 --- a/animeflv/animeflv.py +++ b/animeflv/animeflv.py @@ -21,10 +21,11 @@ def removeprefix(str: str, prefix: str) -> str: if type(str) is type(prefix): if str.startswith(prefix): - return str[len(prefix):] + return str[len(prefix) :] else: return str[:] + def parse_table(table: Tag): columns = list([x.string for x in table.thead.tr.find_all("th")]) rows = [] @@ -293,9 +294,10 @@ def _process_anime_list_info(self, elements: ResultSet) -> List[Dict[str, str]]: try: ret.append( { - "id": removeprefix(element.select_one("div.Description a.Button")["href"][ - 1: - ], "anime/"), + "id": removeprefix( + element.select_one("div.Description a.Button")["href"][1:], + "anime/", + ), "title": element.select_one("a h3").string, "poster": ( element.select_one("a div.Image figure img").get( diff --git a/tests.py b/tests.py index 47d3915..83c159d 100644 --- a/tests.py +++ b/tests.py @@ -1,7 +1,7 @@ import unittest import time from animeflv import AnimeFLV -import cloudscraper +from cloudscraper.exceptions import CloudflareChallengeError def wrap_request(func, *args, count: int = 5): @@ -11,13 +11,14 @@ def wrap_request(func, *args, count: int = 5): try: r = func(*args) return r + except CloudflareChallengeError: + # cloudscraper will error because this feature isn't free, ignore this for the Tests + return ["Lorem Ipsum"] except Exception as e: - if isinstance(e, cloudscraper.exceptions.CloudflareChallengeError): # cloudscraper will error because this feature isn't free, ignore this for the Tests - return ["Lorem Ipsum"] notes.append(e) time.sleep(5) - else: # If the loop doesn't `break`, raise the Exception - raise Exception([e] + notes) + else: # If the loop doesn't `break`, raise the Exception + raise Exception(notes) class AnimeFLVTest(unittest.TestCase):