-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcivitai-downloadLORASearch-new.py
272 lines (214 loc) · 13.2 KB
/
civitai-downloadLORASearch-new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import requests
import time
import os
import json
import re
from tqdm import tqdm
from itertools import product
import platform
from pathlib import Path
def get_operating_system():
system = platform.system()
return system
def sanitise_folder_name(folder_name):
# Define a regular expression pattern to match invalid characters
invalid_chars_pattern = re.compile(r'[\\/:"*?<>|]')
# Replace invalid characters with an empty string
sanitised_folder_name = re.sub(invalid_chars_pattern, '', folder_name)
return sanitised_folder_name
def sanitise_filepath(filepath,replacewith=''):
# Define the set of invalid characters in Windows and Linux file paths
filepath = filepath.replace('\\','/')
invalid_characters = set(['<', '>', '"', '\\','|', '?', '*',' '])
# Replace or remove invalid characters
sanitised_filepath = ''.join(char if char not in invalid_characters else replacewith for char in filepath)
return sanitised_filepath
def get_script_name():
# Use os.path.basename to get the base name (script name) from the full path
#basename = os.path.basename(path)
return Path(__file__).stem
#return os.path.basename(__file__)
def get_script_path():
return os.path.dirname(os.path.realpath(__file__))
def write_to_log(log_file, message):
print(message)
try:
with open(log_file, 'a', encoding='utf-8') as file:
file.write(message + '\n')
except Exception as e:
print(f"Error writing to the log file: {e}")
def dump_to_json(data, filename):
"""
Dump a Python object to a JSON file.
Parameters:
- data: Python object to be dumped to JSON.
- filename: Name of the JSON file to be created.
"""
with open(filename, 'w') as json_file:
json.dump(data, json_file, indent=2) # indent for pretty formatting (optional)
def get_models():
global search_terms
global download_types
global apikey
for searchterm, download_type, in product(search_terms, download_types):
# Initialize the first page
qty = 10
togglequit = False
# modelsearch = f'https://civitai.com/api/v1/models?tag={DownloadLORASearch}&limit={qty}'
# modeltag = f'https://civitai.com/api/v1/models?limit={qty}&types=LORA&query={DownloadLORASearch}'
modelsearch = f'https://civitai.com/api/v1/models?limit={qty}&types={download_type}&tag={searchterm}'
modeltag = f'https://civitai.com/api/v1/models?limit={qty}&types={download_type}&query={searchterm}'
myarray = []
myarray.append(modelsearch)
myarray.append(modeltag)
for eachsearch in myarray:
i = 0
page = 1
while True:
print(f"URL is {eachsearch}. Page is {page}")
headers = {}
headers['Content-Type'] = 'application/json'
params = {'page': page}
while True:
try:
response = requests.get(eachsearch,headers=headers, params=params)
except Exception as e:
write_to_log(logfile_path, "Error " + str(e))
if response.status_code == 200:
try:
data = response.json()
except Exception as e:
write_to_log(logfile_path, "Error " + str(e))
break
elif response.status_code == 401 and apikey:
# Retry the request with the API key
headers["Authorization"] = f"Bearer {apikey}"
elif "be back in a few minutes" in str(response.content):
print("error. Site down")
exit()
else:
time.sleep(5)
write_to_log(logfile_path, "status code: " + str(response.status_code) + " " + response.reason)
# Check if there are models in the response
if 'items' in data:
# Extract 'id' field from each model and add it to the list
totalcnt = data['metadata'].get('totalItems')
write_to_log(logfile_path, "totalcnt = " + str(totalcnt))
write_to_log(logfile_path, "page = " + str(data['metadata'].get('currentPage')) + " of #" + str(data['metadata'].get('totalPages')))
for eachitem in data['items']:
i += 1
id = eachitem.get('id')
name = eachitem.get('name')
write_to_log(logfile_path, "processing #" + str(i) + " of " + str(totalcnt) + " " + str(id) + f" ({name})")
for submodel in eachitem['modelVersions']:
model = submodel.get('name')
model_id = submodel.get('id')
for file in submodel['files']:
if file.get('type') =="Model":
write_to_log(successfile_path, f"found submodel LORA: {model}")
downloadurl = file.get('downloadUrl')
unused1 = str(file.get('id'))
lorafilename = file.get('name')
destination_folder = sanitise_filepath(os.path.join(Lora_download_to,searchterm))
#downloadfilename = name + '_' + model + '_' + file.get('name')
download_filename = f"{id}_{model_id}_{lorafilename}"
download_fullpath = sanitise_filepath(os.path.join(destination_folder,download_filename))
downloadJSON_filename = f"{id}_{model_id}_{lorafilename}.json"
downloadJSON_fullpath = sanitise_filepath(os.path.join(destination_folder,downloadJSON_filename))
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
filesize_should_be = file.get('sizeKB')
fileexists = False
faileddownload = False
if os.path.exists(download_fullpath):
filesize_is = os.path.getsize(download_fullpath)
fileexists = True
write_to_log(logfile_path, "File already exists")
max_retries = 6
download = True
for retry in range(max_retries):
if download == True:
if retry != 0:
print(f"retrying: {retry}")
with requests.get(downloadurl, headers=headers, stream=True) as response:
if response.status_code == 200:
# The request was successful
file_size = int(response.headers.get("content-length", 0))
if fileexists == True and file_size != filesize_is:
faileddownload = True
write_to_log(logfile_path, f"File is wrong size. {filesize_is} and should be {file_size}. Failed download.")
elif fileexists == True and file_size == filesize_is:
write_to_log(logfile_path, f"File is correct size ({filesize_is}). Next..")
download = False
continue
write_to_log(logfile_path, "downloading " + downloadurl + ". Filename: " + download_filename + ". size: " + str(filesize_should_be))
with open(download_fullpath, "wb") as file, tqdm(
desc="Downloading",
total=file_size,
unit="B",
unit_scale=True,
unit_divisor=1024,
) as bar:
# Iterate over the content in chunks and write to the file
for chunk in response.iter_content(chunk_size=8192): # You can adjust the chunk size
if chunk:
#print("chunk")
file.write(chunk)
bar.update(len(chunk))
print("Download complete.")
dump_to_json(submodel,downloadJSON_fullpath)
write_to_log(logfile_path, f"File downloaded successfully to {download_fullpath}")
write_to_log(successfile_path, f"Model: {model}. {download_type}. {searchterm}. URL: {downloadurl}. Filename: {download_filename}")
download = False
continue
elif retry == max_retries and response.status_code == 401:
print(f"Error {response.status_code}")
elif response.status_code == 401 and apikey:
# Retry the request with the API key
headers["Authorization"] = f"Bearer {apikey}"
print("request denied. Adding auth header")
else:
# The request was not successful, handle the error
write_to_log(logfile_path, f"Failed to download file. Status code: {response.status_code}")
else:
write_to_log(logfile_path, "file type is" + f": {file.get('type')}. Filename: {file.get('name')}")
else:
print("no items returned")
#if togglequit == True:
# break
# Check if there are more pages
if data['metadata'].get('currentPage') >= (data['metadata'].get('totalPages')):
#print("lastpage")
#togglequit = True
break
else:
page += 1
print("Finished")
Lora_download_to = '/folder/to/download/to'
apikey = 'void'
search_terms = []
search_terms += ('Lora to search for')
download_types = []
download_types += ('Checkpoint', 'TextualInversion', 'MotionModule','Hypernetwork', 'AestheticGradient', 'LORA', 'LoCon','Controlnet', 'Upscaler','VAE','Poses','Wildcards','Other')
apifile = os.path.join(get_script_path(), "apikey.py")
try:
from apikey import apikey
print("apikey found" + apikey)
except ImportError:
print("apikey.py not found in the current directory.")
current_os = get_operating_system()
if current_os == "Windows":
print("Running on Windows")
elif current_os == "Linux":
print("Running on Linux")
localoverridesfile = os.path.join(get_script_path(), "localoverridesfile_" + get_script_name() + '_' + current_os + '.py')
if os.path.exists(localoverridesfile):
exec(open(localoverridesfile).read())
#apikey = apikey
#print("API Key:", apikey)
print("local override file is " + localoverridesfile)
else:
print("local override file would be " + localoverridesfile)
logfile_path = os.path.join(Lora_download_to,'logfile.log')
successfile_path = os.path.join(Lora_download_to,'successfile.log')
get_models()