Upload files to "/"

This commit is contained in:
Hitmare 2023-11-11 19:17:59 +00:00
parent 6865e06a2e
commit 0c948e72e4
2 changed files with 428 additions and 426 deletions

View File

@ -23,7 +23,7 @@ def scanfolder(path: str) -> list:
os.remove(f"{modelfolder}{modelname}.civitai.info")
sideload = True
else:
with open(f"{modelfolder}{modelname}.civitai.info", "r") as f:
with open(f"{modelfolder}{modelname}.civitai.info", "r", encoding='utf-8') as f:
#fstring = "dd"
print("Reading", modelfolder, modelfile)
modelinfoarray = json.load(f)
@ -63,7 +63,7 @@ def scanfolder(path: str) -> list:
modelid = modelinfoarray.get('modelId')
civitailist.append(modelid)
print(f"Creating {modelfolder}{modelname}.civitai.info for future use")
modelinfofile = open(f"{modelfolder}{modelname}.civitai.info", "w")
modelinfofile = open(f"{modelfolder}{modelname}.civitai.info", "w", encoding='utf-8')
modelinfofile.write(resp.data.decode('utf-8'))
modelinfofile.close()
@ -81,12 +81,12 @@ def scanfolder(path: str) -> list:
def scanall():
erislist = {}
erislist['lora'] = {}
result = scanfolder("./models/lora/")
result = scanfolder("./models/Lora/")
erislist['lora']['civitai'] = result[0]
erislist['lora']['sideload'] = result[1]
erislist['sd'] = {}
result = scanfolder("./models/stable-diffusion/")
result = scanfolder("./models/Stable-diffusion/")
erislist['sd']['civitai'] = result[0]
erislist['sd']['sideload'] = result[1]

View File

@ -1,6 +1,6 @@
import json
import urllib3
import urllib.request
#import urllib.request
import os
import cgi
from pathlib import Path
@ -16,7 +16,6 @@ import erisscan
def filter_str(s: str) -> str:
return "".join(filter(lambda x: x in string.printable, s))
def downloadcivitai(modelid: str, type: str, image: bool) -> bool:
match type:
@ -34,13 +33,14 @@ def downloadcivitai(modelid: str, type: str, image: bool) -> bool:
modelsha256 = modelarray.get('modelVersions')[0].get('files')[0].get('hashes').get("SHA256")
urlhash = f"https://civitai.com/api/v1/model-versions/by-hash/{modelsha256}"
modelimageurl = modelarray.get('modelVersions')[0].get('images')[0].get('url')
modelfilename = modelarray.get('modelVersions')[0].get('files')[0].get('name')
modelfilename = filter_str(modelfilename)
#modelfilesize = int(modelarray.get('modelVersions')[0].get('files')[0].get('sizeKB'))
modelname = Path(modelfilename).stem
modelfileurl = modelarray.get('modelVersions')[0].get('files')[0].get('downloadUrl')
#modeldisplayname = modelarray.get('name')
#trainedwords = modelarray.get('modelVersions')[0].get('trainedWords')
modelimagename = [f"{modelname}.preview.jpg", f"{modelname}.preview.jpeg", f"{modelname}.preview.png"]
#modelimagename = ""
modelinfofile = f"{modelname}.civitai.info"
@ -55,8 +55,7 @@ def downloadcivitai(modelid: str, type: str, image: bool) -> bool:
type, filename = cgi.parse_header(contentdis)
#print("Found file", dict(filename).get('filename'))
filename = dict(filename).get('filename')
#filename = filename.encode('utf-8')
#filename = filter_str(filename)
filename = filter_str(filename)
except Exception as e:
print("Cannot get filename", e)
if filesize:
@ -84,16 +83,19 @@ def downloadcivitai(modelid: str, type: str, image: bool) -> bool:
def downloadimage(url: str, dlpath: str) -> bool:
def downloadimage(url: str, dlpath: str) -> tuple((bool, str)):
print(f"Download Image for {modelfilename}")
imagerequest = http.request('GET', url, preload_content=False)
#path = f"{os.getcwd()}/models/Loras/"
filesize = imagerequest.getheader('content-length')
#contentdis = imagerequest.getheader('Content-Disposition')
#type, filename = cgi.parse_header(contentdis)
filename = f"{Path(modelfilename).stem}.preview{Path(filename).suffix}"
global modelimagename
modelimagename = filename
contentdis = imagerequest.getheader('Content-Disposition')
if contentdis == None:
filename = f"{Path(modelfilename).stem}.preview.png"
else:
#print("Content", contentdis)
type, filename = cgi.parse_header(contentdis)
filename = dict(filename).get('filename')
filename = f"{Path(modelfilename).stem}.preview{Path(filename).suffix}"
#print(filename['filename'])
if filesize:
filesize = int(filesize)
@ -116,11 +118,12 @@ def downloadcivitai(modelid: str, type: str, image: bool) -> bool:
else:
print(f"Download: {Percent}%", end='\r')
imagerequest.release_conn()
return tuple((True, filename))
def downloadinfo(url: str, dlfullpath: str) -> bool:
print(f"Download Infofile for {modelfilename}")
resp = http.request('GET', url)
infofile = open(f"{dlfullpath}", "w")
infofile = open(f"{dlfullpath}", "w", encoding='utf-8')
infofile.write(resp.data.decode('utf-8'))
infofile.close()
@ -140,16 +143,16 @@ def downloadcivitai(modelid: str, type: str, image: bool) -> bool:
if image:
try:
if os.path.exists(f"{dlpath}{modelimagename[0]}") == False and os.path.exists(f"{dlpath}{modelimagename[1]}") == False and os.path.exists(f"{dlpath}{modelimagename[2]}") == False:
downloadimage(modelimageurl, dlpath)
dlimage = downloadimage(modelimageurl, dlpath)
else:
print(f"Preview Image for {modelfilename} already exist, skip download")
except Exception as e:
print(e)
finally:
if isinstance(modelfilename, str):
if not os.path.exists(f"{dlpath}{modelimagename}"):
print(f"{modelfilename} could not be saved, continue.")
if isinstance(dlimage[1], str):
if not os.path.exists(f"{dlpath}{dlimage[1]}"):
print(f"{dlimage[1]} could not be saved, continue.")
# save infofile
try:
if not os.path.exists(f"{dlpath}{modelinfofile}"):
@ -171,9 +174,9 @@ def downloadsideload(name: str, modeltype: str) -> bool:
match modeltype:
case "lora":
dlpath = "./models/lora/"
dlpath = "./models/Lora/"
case "sd":
dlpath = "./models/stable-diffusion/"
dlpath = "./models/Stable-diffusion/"
case "emb":
dlpath = "./embeddings/"
@ -271,9 +274,9 @@ def checksideload(locallist: list, erislist: list) -> tuple:
def deleteFiles(nameid: str, modeltype: str, localfile: bool = False) -> bool:
match modeltype:
case "lora":
dlpath = "./models/lora/"
dlpath = "./models/Lora/"
case "sd":
dlpath = "./models/stable-diffusion/"
dlpath = "./models/Stable-diffusion/"
case "emb":
dlpath = "./embeddings/"
@ -319,8 +322,7 @@ if not os.path.exists("./eris.json"):
# get the eris list from the web and local
http = urllib3.PoolManager()
#erisurl = "https://eris.hitmare.me/eris.json"
erisurl = "https://git.foxo.me/Hitmare/Eris-Update-script/src/branch/main/eris.json"
erisurl = "https://eris.hitmare.me/eris.json"
resp = http.request('GET', erisurl)
erisremote = json.loads(resp.data.decode('utf-8'))
erislocal = json.load(open("./eris.json", "r"))