From a5a09d10afc6511502c80c2bf0a6fa397d3c5b70 Mon Sep 17 00:00:00 2001 From: 64Core <64core@pm.me> Date: Fri, 14 Apr 2023 22:39:05 -0500 Subject: [PATCH] PR #77 address second comment, potential bug regarding renaming old files --- itchiodl/game.py | 16 ++++++++-------- itchiodl/utils.py | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/itchiodl/game.py b/itchiodl/game.py index 5ec34f1..52350e8 100644 --- a/itchiodl/game.py +++ b/itchiodl/game.py @@ -42,14 +42,14 @@ class Game: else: self.publisher_slug = matches.group(1) - self.destination_path = Path(f"{self.publisher_slug}/{self.game_slug}") + # self.destination_path = Path(self.publisher_slug / self.game_slug) self.files = [] self.downloads = [] - # self.dir = ( - # Path(".") - # / utils.clean_path(self.publisher_slug) - # / utils.clean_path(self.game_slug) - # ) + self.destination_path = ( + Path(".") + / utils.clean_path(self.publisher_slug) + / utils.clean_path(self.game_slug) + ) def load_downloads(self, token): """Load all downloads for this game""" @@ -109,7 +109,7 @@ class Game: print(f"Downloading {d['filename']}") filename = utils.clean_path(d["filename"] or d["display_name"] or d["id"]) - filepath = Path(f"{self.destination_path}/{filename}") + filepath = self.destination_path / filename hashpath = filepath.with_suffix(".md5") if filepath.exists(): @@ -140,7 +140,7 @@ class Game: print(f"Moving {filename} to old/") timestamp = datetime.datetime.now().strftime("%Y-%m-%d") - filename.rename(old_dir / f"{timestamp}-{filename}") + filepath.rename(old_dir / f"{timestamp}-{filename}") # Get UUID r = requests.post( diff --git a/itchiodl/utils.py b/itchiodl/utils.py index b03e0ab..00a5efa 100644 --- a/itchiodl/utils.py +++ b/itchiodl/utils.py @@ -8,10 +8,10 @@ class NoDownloadError(Exception): """No download found exception""" -def download(url, path, name, file): +def download(url, pathname, name, filename): """Downloads a file from a url and saves it to a path, skips it if it already exists.""" - desc = f"{name} - {file}" + desc = f"{name} - {filename}" print(f"Downloading {desc}") rsp = requests.get(url, stream=True) @@ -29,12 +29,12 @@ def download(url, path, name, file): # else: # filename = filename_re.group(1) - with open(f"{path}/{file}", "wb") as f: + with open(f"{pathname}/{filename}", "wb") as f: for chunk in rsp.iter_content(10240): f.write(chunk) - print(f"Downloaded {file}") - return f"{path}/{file}", True + print(f"Downloaded {filename}") + return f"{pathname}/{filename}", True def clean_path(path): @@ -48,10 +48,10 @@ def clean_path(path): return path -def md5sum(pathname): +def md5sum(path): """Returns the md5sum of a file""" md5 = hashlib.md5() - with open(pathname, "rb") as f: + with path.open("rb") as f: for chunk in iter(lambda: f.read(4096), b""): md5.update(chunk) return md5.hexdigest()