-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcivitai_download.py
243 lines (190 loc) · 8.78 KB
/
civitai_download.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
import subprocess
import os
import sys
import json
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
import requests
from tqdm import tqdm
import logging
import re
def get_filename_from_headers_PRODUCTION(url):
try:
wget_cmd = f"wget --content-disposition --spider --server-response {url}"
result = subprocess.run(wget_cmd, shell=True, text=True, capture_output=True, timeout=10)
content_disposition = ""
for line in result.stdout.splitlines() + result.stderr.splitlines():
if "Content-Disposition" in line:
content_disposition = line.strip()
print(f"CONTENT: {content_disposition}")
break
if content_disposition:
filename = re.search(r'filename="?([^"]+)"?', content_disposition)
if filename:
return filename.group(1)
except Exception as e:
logging.debug(f"Error while getting filename from wget headers for {url}: {e}")
return None
def get_filename_from_headers_DEBUG(url):
try:
wget_cmd = f"wget --content-disposition --spider --server-response {url}"
result = subprocess.run(wget_cmd, shell=True, text=True, capture_output=True, timeout=10)
print("STDOUT:")
print(result.stdout)
print("STDERR:")
print(result.stderr)
content_disposition = ""
for line in result.stdout.splitlines() + result.stderr.splitlines():
if "Content-Disposition" in line:
content_disposition = line.strip()
print(f"CONTENT: {content_disposition}")
break
if content_disposition:
filename = re.search(r'filename="?([^"]+)"?', content_disposition)
if filename:
return filename.group(1)
else:
print("Filename not found in content_disposition")
else:
print("Content-Disposition not found in stdout and stderr")
except Exception as e:
logging.debug(f"Error while getting filename from wget headers for {url}: {e}")
return None
def get_filename_from_headers_POST_FAILED(url):
try:
response = requests.head(url, timeout=10)
content_disposition = response.headers.get("Content-Disposition", "")
if content_disposition:
filename = re.search(r'filename="?([^"]+)"?', content_disposition)
if filename:
return filename.group(1)
else:
print("Content-Disposition not found in headers")
except Exception as e:
print(f"Error while getting filename from headers for {url}: {e}")
return None
def get_filename_from_log(log):
try:
filename = re.search(r'filename="?([^"]+)"?', log)
if filename:
return filename.group(1)
except Exception as e:
print(f"Error while getting filename from log: {e}")
return None
def get_filename_from_headers(url):
try:
with requests.get(url, stream=True, timeout=10) as response:
content_disposition = response.headers.get("Content-Disposition", "")
if content_disposition:
filename = re.search(r'filename="?([^"]+)"?', content_disposition)
if filename:
return filename.group(1)
else:
print("Content-Disposition not found in headers")
except Exception as e:
print(f"Error while getting filename from headers for {url}: {e}")
return None
def call_wget(url, output_path, timeout, retries, range_header):
for attempt in range(retries):
try:
logging.debug(f"Attempt {attempt + 1} for downloading {url}")
# Construct the wget command with timeout, headers, and content-disposition
wget_cmd = f"wget --content-disposition -O {output_path} --timeout={timeout} {range_header} {url}"
# Run the wget command
result = subprocess.run(wget_cmd, shell=True, text=True, capture_output=True)
# Check the exit code to determine if the download was successful
if result.returncode == 0:
return True, ""
else:
error_message = f"Error while downloading: {result.stderr}"
logging.debug(f"Error while downloading {url}: {error_message}")
except Exception as e:
error_message = f"Unexpected error: {e}"
logging.debug(f"Unexpected error while downloading {url}: {error_message}")
return False, error_message
def load_config(config_file):
with open(config_file, "r") as f:
config = json.load(f)
return config
def check_file_size(file_path, min_size_gb):
file_size_gb = os.path.getsize(file_path) / (1024**3)
return file_size_gb >= min_size_gb
def download_file(url, output_directory, timeout, retries, min_file_size_gb):
type_value = re.search(r"type=([^&]+)", url).group(1)
format_value = re.search(r"format=([^&]+)", url).group(1)
file_name = f"{type_value}_{format_value}.bin"
output_path = os.path.join(output_directory, file_name)
# Get the file name from wget headers
wget_filename = get_filename_from_headers(url)
if wget_filename:
file_name = wget_filename
output_path = os.path.join(output_directory, file_name)
print(f"WGET, file_name: {file_name}, output_path: {output_path}.")
# Check if the file with the same name exists before attempting to download
if any(file.startswith(file_name) for file in os.listdir(output_directory)):
logging.info(f"File {file_name} already exists. Skipping download.")
return f"File {file_name} already exists. Skipping download."
# Check if the file exists to resume the download
if os.path.exists(output_path):
file_size = os.path.getsize(output_path)
range_header = f"--header=Range:bytes={file_size}-"
else:
range_header = ""
success, error_message = call_wget(url, output_path, timeout, retries, range_header)
if success:
# Save the link to the model in a txt file
txt_file_name = f"{file_name}.txt"
txt_file_path = os.path.join(output_directory, txt_file_name)
with open(txt_file_path, "w") as txt_file:
txt_file.write(url)
if check_file_size(output_path, min_file_size_gb):
logging.info(f"File {file_name} downloaded successfully and meets the minimum size requirement.")
return f"File {file_name} downloaded successfully and meets the minimum size requirement."
else:
os.remove(output_path)
logging.warning(f"File {file_name} downloaded successfully but does not meet the minimum size requirement.")
return f"File {file_name} downloaded successfully but does not meet the minimum size requirement."
else:
logging.error(f"Failed to download the file {file_name}. {error_message}")
return f"Failed to download the file {file_name}. {error_message}"
def get_content_length(url):
try:
response = requests.head(url)
content_length = response.headers.get('Content-Length')
return content_length
except Exception as e:
return None
def main():
config = load_config("download_config.json")
urls = config["urls"]
output_directory = config["output_directory"]
timeout = config["timeout"] * 60 # Convert minutes to seconds
retries = config["retries"]
min_file_size_gb = config["min_file_size_gb"]
max_workers = config["max_workers"]
log_file = config["log_file"]
if not os.path.exists(output_directory):
os.makedirs(output_directory)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# Add a FileHandler to save logs to a file
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
# Add a StreamHandler to print log messages to the console
#console_handler = logging.StreamHandler(sys.stdout)
#console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
#console_handler.setLevel(logging.DEBUG)
#logger.addHandler(console_handler)
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = {executor.submit(download_file, url, output_directory, timeout, retries, min_file_size_gb): url for url in urls}
results = []
for future in tqdm(as_completed(futures), total=len(urls), desc="Downloading files", ncols=80):
result = future.result()
results.append(result)
for result in results:
print(result)
if __name__ == "__main__":
main()