Skip to content

Commit

Permalink
Bugfix: Added up to 10 retries for defeating cloudflare protection
Browse files Browse the repository at this point in the history
  • Loading branch information
NotSimone committed Jul 3, 2024
1 parent d3b17d4 commit b971ffa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class KoboMetadata(Source):
name = "Kobo Metadata"
author = "NotSimone"
version = (1, 6, 2)
version = (1, 6, 3)
minimum_calibre_version = (5, 0, 0)
description = _("Downloads metadata and covers from Kobo")

Expand Down
16 changes: 12 additions & 4 deletions kobo_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,18 @@ def _get_session(self) -> requests.Session:
def _get_webpage(self, url: str, timeout: int, log: Log) -> Tuple[Optional[html.Element], bool]:
session = self._get_session()
try:
resp = session.get(url, timeout=timeout)
page = html.fromstring(resp.text)
is_search = "/search?" in resp.url
return (page, is_search)
attempts = 0
while attempts < 10:
resp = session.get(url, timeout=timeout)
page = html.fromstring(resp.text)
# If we failed to get past the cloudflare protection, we get a page with this class
if not page.xpath("//form[@class='challenge-form']"):
is_search = "/search?" in resp.url
return (page, is_search)
log.info(f"KoboMetadata::get_webpage: Could not defeat cloudflare protection - trying again for {url}")
attempts += 1
log.error(f"KoboMetadata::get_webpage: Could not defeat cloudflare protection - giving up for {url}")
return (None, False)
except Exception as e:
log.error(f"KoboMetadata::get_webpage: Got exception while opening url: {e}")
return (None, False)
Expand Down

0 comments on commit b971ffa

Please sign in to comment.