Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

retry a few times on connection failure so the whole thing doesn't need to restart #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions titanium_backup_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,19 @@ def _convert_url_to_base64_data(url):
if url is not None:
file_name = 'tmp/' + str(uuid.uuid4())
os.makedirs(os.path.dirname(file_name), exist_ok=True)
with urllib.request.urlopen(url) as file:
data = file.read()
with open(file_name, "wb") as new_file:
new_file.write(data)
new_file.close()
encoded_data = base64.b64encode(open(file_name, "rb").read()).decode('utf-8')
os.remove(file_name)
for trynum in range(0,5):
try:
with urllib.request.urlopen(url) as file:
data = file.read()
with open(file_name, "wb") as new_file:
new_file.write(data)
new_file.close()
encoded_data = base64.b64encode(open(file_name, "rb").read()).decode('utf-8')
os.remove(file_name)
break
except urllib.request.URLError as err:
print("Failed to complete request for: {} on try {} because of: {} Trying again".format(url, trynum, err))

if encoded_data is None or len(encoded_data) <= 0:
print("Error downloading or base64 encoding attachment!")
return encoded_data
Expand Down