Added some filename fixes (#29)

* Added some filename fixes

* forgot to change how I called the download function
This commit is contained in:
Peter Taylor 2022-03-16 23:20:02 +00:00 committed by GitHub
parent fb10162033
commit ae960bc2ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

@ -65,7 +65,7 @@ class Game:
url = f"https://api.itch.io/uploads/{d['id']}/download?api_key={token}&download_key_id={self.id}&uuid={j['uuid']}"
# response_code = urllib.request.urlopen(url).getcode()
try:
itchiodl.utils.download(url, path, self.name + " - " + file)
itchiodl.utils.download(url, path, self.name, file)
except itchiodl.utils.NoDownloadError as e:
print("Http response is not a download, skipping")

View file

@ -9,7 +9,10 @@ class NoDownloadError(Exception):
pass
def download(url, path, desc):
def download(url, path, name, file):
"""Downloads a file from a url and saves it to a path, skips it if it already exists."""
desc = f"{name} - {file}"
print(f"Downloading {desc}")
rsp = requests.get(url, stream=True)
@ -18,7 +21,13 @@ def download(url, path, desc):
raise NoDownloadError("Http response is not a download, skipping")
cd = rsp.headers.get("Content-Disposition")
filename = re.search(r'filename="(.+)"', cd).group(1)
filename_re = re.search(r'filename="(.+)"', cd)
if filename_re is None:
filename = file
else:
filename = filename_re.group(1)
total_length = int(rsp.headers.get('content-length'))
if os.path.exists(f"{path}/{filename}"):