pull/63/head
butaixianran 2023-03-14 02:27:43 +08:00
parent 6d5a7d2b74
commit f9c7d877c3
4 changed files with 16 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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