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 13 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: 28 additions & 27 deletions itchiodl/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import json
import urllib
import datetime
from pathlib import Path
from os import path
from os import mkdir
from os import makedirs
from sys import argv
import requests

Expand Down Expand Up @@ -42,13 +44,14 @@ def __init__(self, data):
else:
self.publisher_slug = matches.group(1)

self.destination_path = path.normpath(f"{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.dir = (
# Path(".")
# / utils.clean_path(self.publisher_slug)
# / utils.clean_path(self.game_slug)
#)

def load_downloads(self, token):
"""Load all downloads for this game"""
Expand Down Expand Up @@ -77,7 +80,8 @@ def download(self, token, platform):

self.load_downloads(token)

self.dir.mkdir(parents=True, exist_ok=True)
if not path.exists(self.destination_path):
makedirs(self.destination_path)

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

with self.dir.with_suffix(".json").open("w") as f:
with open(f"{self.destination_path}.json", "w") as f:
json.dump(
{
"name": self.name,
Expand All @@ -107,41 +111,38 @@ 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

if out_file.exists():
filename = utils.clean_path(d["filename"] or d["display_name"] or d["id"])
pathname = self.destination_path
if path.exists(f"{pathname}/{filename}"):
print(f"File Already Exists! {filename}")
md5_file = out_file.with_suffix(".md5")
if md5_file.exists():
with md5_file.open("r") as f:
if path.exists(f"{pathname}/{filename}.md5"):
with open(f"{pathname}/{filename}.md5", "r") as f:
md5 = f.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(f"{pathname}/{filename}")
if md5 == d["md5_hash"]:
print(f"Skipping {self.name} - {filename}")

# Create checksum file
with md5_file.open("w") as f:
with open(f"{pathname}/{filename}.md5", "w") as f:
f.write(d["md5_hash"])
return
# Old Download or corrupted file?
corrupted = False
if corrupted:
out_file.remove()
filename.remove()
return

old_dir = self.dir / "old"
old_dir.mkdir(exist_ok=True)
old_dir = f"{pathname}/old"
mkdir(old_dir)

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

# Get UUID
r = requests.post(
Expand All @@ -163,15 +164,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: {pathname}
File: {filename}
Request URL: {url}
This request failed due to a missing response header
Expand All @@ -187,7 +188,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: {pathname}
File: {filename}
Request URL: {url}
Request Response Code: {e.code}
Expand All @@ -199,10 +200,10 @@ def do_download(self, d, token):
return

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

# Create checksum file
with out_file.with_suffix(".md5").open("w") as f:
with open(f"{pathname}/{filename}.md5", "w") as f:
f.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 @@ -23,18 +23,18 @@ def download(url, path, name, file):

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"{path}/{file}", "wb") as f:
for chunk in rsp.iter_content(10240):
f.write(chunk)

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


def clean_path(path):
Expand All @@ -48,10 +48,10 @@ def clean_path(path):
return path


def md5sum(path):
def md5sum(pathname):
"""Returns the md5sum of a file"""
md5 = hashlib.md5()
with path.open("rb") as f:
with open(pathname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
md5.update(chunk)
return md5.hexdigest()