From f9c7d877c3ebc584cf31b09baa15b5ac3f721342 Mon Sep 17 00:00:00 2001 From: butaixianran Date: Tue, 14 Mar 2023 02:27:43 +0800 Subject: [PATCH] v1.5.4 --- README.md | 3 +++ scripts/lib/civitai.py | 7 ++++--- scripts/lib/downloader.py | 7 +++++-- scripts/lib/util.py | 5 ++++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5742ab6..7003f32 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,9 @@ From v1.5, v1.x goes into maintenance phase. Enjoy! # Change Log +## v1.5.4 +* set sys.stdout to utf-8 + ## v1.5.3 * When downloading a model by url, check if target model version is already existed in user selected sub-folder. diff --git a/scripts/lib/civitai.py b/scripts/lib/civitai.py index 6411d0c..77f64bc 100644 --- a/scripts/lib/civitai.py +++ b/scripts/lib/civitai.py @@ -26,6 +26,7 @@ model_type_dict = { } + # get image with full size # width is in number, not string # return: url str @@ -42,7 +43,7 @@ def get_model_info_by_hash(hash:str): util.printD("hash is empty") return - r = requests.get(url_dict["hash"]+hash) + r = requests.get(url_dict["hash"]+hash, headers=util.def_headers) if not r.ok: if r.status_code == 404: # this is not a civitai model @@ -79,7 +80,7 @@ def get_model_info_by_id(id:str) -> dict: util.printD("id is empty") return - r = requests.get(url_dict["modelId"]+str(id)) + r = requests.get(url_dict["modelId"]+str(id), headers=util.def_headers) if not r.ok: if r.status_code == 404: # this is not a civitai model @@ -115,7 +116,7 @@ def get_version_info_by_version_id(id:str) -> dict: util.printD("id is empty") return - r = requests.get(url_dict["modelVersionId"]+str(id)) + r = requests.get(url_dict["modelVersionId"]+str(id), headers=util.def_headers) if not r.ok: if r.status_code == 404: # this is not a civitai model diff --git a/scripts/lib/downloader.py b/scripts/lib/downloader.py index 1bc55db..fa24a5d 100644 --- a/scripts/lib/downloader.py +++ b/scripts/lib/downloader.py @@ -31,7 +31,7 @@ def dl(url, folder, filename, filepath): file_path = os.path.join(folder, filename) # first request for header - rh = requests.get(url, stream=True, verify=False) + rh = requests.get(url, stream=True, verify=False, headers=util.def_headers) # get file size total_size = 0 total_size = int(rh.headers['Content-Length']) @@ -76,6 +76,8 @@ def dl(url, folder, filename, filepath): # create header range headers = {'Range': 'bytes=%d-' % downloaded_size} + headers['User-Agent'] = util.def_headers['User-Agent'] + # download with header r = requests.get(url, stream=True, verify=False, headers=headers) @@ -90,7 +92,8 @@ def dl(url, folder, filename, filepath): # progress progress = int(50 * downloaded_size / total_size) - sys.stdout.write("\r[%s%s] %d%%" % ('█' * progress, ' ' * (50 - progress), 100 * downloaded_size / total_size)) + sys.stdout.reconfigure(encoding='utf-8') + sys.stdout.write("\r[%s%s] %d%%" % ('-' * progress, ' ' * (50 - progress), 100 * downloaded_size / total_size)) sys.stdout.flush() print() diff --git a/scripts/lib/util.py b/scripts/lib/util.py index 7c0fbce..63f8f7e 100644 --- a/scripts/lib/util.py +++ b/scripts/lib/util.py @@ -6,6 +6,9 @@ import shutil version = "1.5.3" +def_headers = {'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'} + + # print for debugging def printD(msg): print(f"Civitai Helper: {msg}") @@ -30,7 +33,7 @@ def gen_file_sha256(filname): def download_file(url, path): printD("Downloading file from: " + url) # get file - r = requests.get(url, stream=True) + r = requests.get(url, stream=True, headers=def_headers) if not r.ok: printD("Get error code: " + str(r.status_code)) printD(r.text)