PR #77 address second comment, potential bug regarding renaming old files

This commit is contained in:
64Core 2023-04-14 22:39:05 -05:00
parent 6de39b63f5
commit a5a09d10af
2 changed files with 15 additions and 15 deletions

View file

@ -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(

View file

@ -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()