Update civitai_api.py
Browse files- civitai_api.py +51 -9
civitai_api.py
CHANGED
@@ -334,6 +334,41 @@ def search_civitai(query, types, base_model=[], sort=SORT[0], period=PERIOD[0],
|
|
334 |
state
|
335 |
|
336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
def get_civitai_json(dl_url: str, is_html: bool=False, image_baseurl: str="", api_key=""):
|
338 |
if not image_baseurl: image_baseurl = dl_url
|
339 |
default = ("", "", "") if is_html else ""
|
@@ -360,15 +395,22 @@ def get_civitai_json(dl_url: str, is_html: bool=False, image_baseurl: str="", ap
|
|
360 |
r = session.get(url, params=params, headers=headers, stream=True, timeout=(5.0, 15))
|
361 |
if not r.ok: return json, html, image
|
362 |
html = r.text
|
363 |
-
if
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
372 |
return json, html, image
|
373 |
except Exception as e:
|
374 |
print(e)
|
|
|
334 |
state
|
335 |
|
336 |
|
337 |
+
def download_media(session, media_info, base_url, headers, is_video=False):
|
338 |
+
media_url = media_info["url"]
|
339 |
+
media_path = get_save_path(base_url, media_info["name"], is_video)
|
340 |
+
|
341 |
+
try:
|
342 |
+
response = session.get(media_url, headers=headers, timeout=(5.0, 15), stream=True)
|
343 |
+
response.raise_for_status()
|
344 |
+
|
345 |
+
with open(media_path, 'wb') as f:
|
346 |
+
for chunk in response.iter_content(chunk_size=8192):
|
347 |
+
f.write(chunk)
|
348 |
+
|
349 |
+
if not is_video:
|
350 |
+
temp_image_path = media_path + ".tmp"
|
351 |
+
with Image.open(temp_image_path) as img:
|
352 |
+
img = img.convert('RGBA')
|
353 |
+
img.save(media_path)
|
354 |
+
os.remove(temp_image_path)
|
355 |
+
|
356 |
+
return media_path
|
357 |
+
|
358 |
+
except requests.RequestException as e:
|
359 |
+
print(f"Media download error: {e}")
|
360 |
+
return ""
|
361 |
+
|
362 |
+
def get_save_path(base_url, file_name, is_video=False):
|
363 |
+
dir_path = Path(TEMP_DIR)
|
364 |
+
os.makedirs(dir_path, exist_ok=True)
|
365 |
+
name = Path(file_name).stem
|
366 |
+
ext = Path(file_name).suffix
|
367 |
+
if is_video:
|
368 |
+
return str(dir_path / f"{name}{ext}")
|
369 |
+
else:
|
370 |
+
return str(dir_path / f"{name}.png")
|
371 |
+
|
372 |
def get_civitai_json(dl_url: str, is_html: bool=False, image_baseurl: str="", api_key=""):
|
373 |
if not image_baseurl: image_baseurl = dl_url
|
374 |
default = ("", "", "") if is_html else ""
|
|
|
395 |
r = session.get(url, params=params, headers=headers, stream=True, timeout=(5.0, 15))
|
396 |
if not r.ok: return json, html, image
|
397 |
html = r.text
|
398 |
+
if "images" in json.keys() and len(json["images"]) != 0:
|
399 |
+
for image_info in json_data["images"]:
|
400 |
+
if image_info["type"] == "video":
|
401 |
+
video_path = download_media(session, image_info, image_baseurl, headers, is_video=True)
|
402 |
+
elif image_info["type"] == "image":
|
403 |
+
# image_path = download_media(session, image_info, image_baseurl, headers, is_video=False)
|
404 |
+
url = json["images"][0]["url"]
|
405 |
+
r = session.get(url, params=params, headers=headers, stream=True, timeout=(5.0, 15))
|
406 |
+
if not r.ok: return json, html, image
|
407 |
+
image_temp = str(Path(TEMP_DIR, "image" + Path(url.split("/")[-1]).suffix))
|
408 |
+
image = str(Path(TEMP_DIR, Path(image_baseurl.split("/")[-1]).stem + ".png"))
|
409 |
+
with open(image_temp, 'wb') as f:
|
410 |
+
f.write(r.content)
|
411 |
+
Image.open(image_temp).convert('RGBA').save(image)
|
412 |
+
if image_path and video_path:
|
413 |
+
break
|
414 |
return json, html, image
|
415 |
except Exception as e:
|
416 |
print(e)
|