Skip to content

Commit

Permalink
Merge pull request #124 from eggplants/fix_save_retry
Browse files Browse the repository at this point in the history
Fix save retry mechanism
  • Loading branch information
akamhy authored Feb 3, 2022
2 parents 5cbdfc0 + bc1dc4d commit 947647f
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions waybackpy/save_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def __init__(self, url, user_agent=DEFAULT_USER_AGENT, max_tries=8):
self.request_url = "https://web.archive.org/save/" + self.url
self.user_agent = user_agent
self.request_headers = {"User-Agent": self.user_agent}
if max_tries < 1:
raise ValueError("max_tries should be positive")
self.max_tries = max_tries
self.total_save_retries = 5
self.backoff_factor = 0.5
Expand Down Expand Up @@ -160,27 +162,22 @@ def save(self):
tries = 0

while True:

tries += 1

if tries >= self.max_tries:
raise MaximumSaveRetriesExceeded(
"Tried %s times but failed to save and retrieve the" % str(tries)
+ " archive for %s.\nResponse URL:\n%s \nResponse Header:\n%s\n"
% (self.url, self.response_url, str(self.headers)),
)

if not self.saved_archive:

if tries > 1:
if tries >= 1:
self.sleep(tries)

self.get_save_request_headers()
self.saved_archive = self.archive_url_parser()

if not self.saved_archive:
continue
else:
if self.saved_archive is not None:
self._archive_url = self.saved_archive
self.timestamp()
return self.saved_archive

tries += 1
if tries >= self.max_tries:
raise MaximumSaveRetriesExceeded(
"Tried %s times but failed to save and retrieve the" % str(tries)
+ " archive for %s.\nResponse URL:\n%s \nResponse Header:\n%s\n"
% (self.url, self.response_url, str(self.headers)),
)

0 comments on commit 947647f

Please sign in to comment.