diff --git a/itchiodl/game.py b/itchiodl/game.py index 469b8c4..963505f 100644 --- a/itchiodl/game.py +++ b/itchiodl/game.py @@ -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") diff --git a/itchiodl/utils.py b/itchiodl/utils.py index 6f65703..efe0708 100644 --- a/itchiodl/utils.py +++ b/itchiodl/utils.py @@ -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}"):