Skip to content

Bugfix/unicode support #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
55 changes: 27 additions & 28 deletions itchiodl/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import json
import urllib
import datetime
from pathlib import Path
from sys import argv
from pathlib import Path
import requests

from itchiodl import utils
Expand Down Expand Up @@ -42,9 +42,10 @@ def __init__(self, data):
else:
self.publisher_slug = matches.group(1)

# self.destination_path = Path(self.publisher_slug / self.game_slug)
self.files = []
self.downloads = []
self.dir = (
self.destination_path = (
Path(".")
/ utils.clean_path(self.publisher_slug)
/ utils.clean_path(self.game_slug)
Expand Down Expand Up @@ -77,7 +78,7 @@ def download(self, token, platform):

self.load_downloads(token)

self.dir.mkdir(parents=True, exist_ok=True)
self.destination_path.mkdir(parents=True, exist_ok=True)

for d in self.downloads:
if (
Expand All @@ -89,7 +90,7 @@ def download(self, token, platform):
continue
self.do_download(d, token)

with self.dir.with_suffix(".json").open("w") as f:
with self.destination_path.with_suffix(".json").open(mode="w") as f:
json.dump(
{
"name": self.name,
Expand All @@ -107,41 +108,39 @@ def do_download(self, d, token):
"""Download a single file, checking for existing files"""
print(f"Downloading {d['filename']}")

filename = d["filename"] or d["display_name"] or d["id"]

out_file = self.dir / filename
filename = utils.clean_path(d["filename"] or d["display_name"] or d["id"])
filepath = self.destination_path / filename
hashpath = filepath.with_suffix(".md5")

if out_file.exists():
if filepath.exists():
print(f"File Already Exists! {filename}")
md5_file = out_file.with_suffix(".md5")
if md5_file.exists():
with md5_file.open("r") as f:
md5 = f.read().strip()
if hashpath.exists():
with hashpath.open(mode="r") as hashfile:
md5 = hashfile.read().strip()
if md5 == d["md5_hash"]:
print(f"Skipping {self.name} - {filename}")
return
print(f"MD5 Mismatch! {filename}")
else:
md5 = utils.md5sum(str(out_file))
md5 = utils.md5sum(filepath)
if md5 == d["md5_hash"]:
print(f"Skipping {self.name} - {filename}")

# Create checksum file
with md5_file.open("w") as f:
f.write(d["md5_hash"])
with hashpath.open(mode="w") as hashfile:
hashfile.write(d["md5_hash"])
return
# Old Download or corrupted file?
corrupted = False
if corrupted:
out_file.remove()
return

old_dir = self.dir / "old"
# corrupted = False
# if corrupted:
# filename.remove()
# return
old_dir = self.destination_path / "old"
old_dir.mkdir(exist_ok=True)

print(f"Moving {filename} to old/")
timestamp = datetime.datetime.now().strftime("%Y-%m-%d")
out_file.rename(old_dir / f"{timestamp}-{filename}")
filepath.rename(old_dir / f"{timestamp}-{filename}")

# Get UUID
r = requests.post(
Expand All @@ -163,15 +162,15 @@ def do_download(self, d, token):
)
# response_code = urllib.request.urlopen(url).getcode()
try:
utils.download(url, self.dir, self.name, filename)
utils.download(url, self.destination_path, self.name, filename)
except utils.NoDownloadError:
print("Http response is not a download, skipping")

with open("errors.txt", "a") as f:
f.write(
f""" Cannot download game/asset: {self.game_slug}
Publisher Name: {self.publisher_slug}
Path: {out_file}
Path: {self.destination_path}
File: {filename}
Request URL: {url}
This request failed due to a missing response header
Expand All @@ -187,7 +186,7 @@ def do_download(self, d, token):
f.write(
f""" Cannot download game/asset: {self.game_slug}
Publisher Name: {self.publisher_slug}
Path: {out_file}
Path: {self.destination_path}
File: {filename}
Request URL: {url}
Request Response Code: {e.code}
Expand All @@ -199,10 +198,10 @@ def do_download(self, d, token):
return

# Verify
if utils.md5sum(out_file) != d["md5_hash"]:
if utils.md5sum(filepath) != d["md5_hash"]:
print(f"Failed to verify {filename}")
return

# Create checksum file
with out_file.with_suffix(".md5").open("w") as f:
f.write(d["md5_hash"])
with hashpath.open(mode="w") as hashfile:
hashfile.write(d["md5_hash"])
20 changes: 10 additions & 10 deletions itchiodl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -21,20 +21,20 @@ def download(url, path, name, file):
):
raise NoDownloadError("Http response is not a download, skipping")

cd = rsp.headers.get("Content-Disposition")
# cd = rsp.headers.get("Content-Disposition")

filename_re = re.search(r'filename="(.+)"', cd)
if filename_re is None:
filename = file
else:
filename = filename_re.group(1)
# filename_re = re.search(r'filename="(.+)"', cd)
# if filename_re is None:
# filename = file
# else:
# filename = filename_re.group(1)

with open(f"{path}/{filename}", "wb") as f:
with open(f"{pathname}/{filename}", "wb") as f:
for chunk in rsp.iter_content(10240):
f.write(chunk)

print(f"Downloaded {filename}")
return f"{path}/{filename}", True
return f"{pathname}/{filename}", True


def clean_path(path):
Expand Down