|
| 1 | +import contextlib |
1 | 2 | import os |
2 | 3 | import pathlib |
3 | 4 | import sys |
4 | 5 | from typing import Annotated, Optional |
5 | | -from urllib.parse import unquote, urlparse |
| 6 | +from urllib.parse import parse_qs, unquote, urlparse |
6 | 7 |
|
7 | 8 | import requests |
8 | 9 | import typer |
@@ -74,39 +75,65 @@ def check_huggingface_url(url: str) -> tuple[bool, Optional[str], Optional[str], |
74 | 75 | return True, repo_id, filename, folder_name, branch_name |
75 | 76 |
|
76 | 77 |
|
77 | | -def check_civitai_url(url: str) -> tuple[bool, bool, int, int]: |
| 78 | +def check_civitai_url(url: str) -> tuple[bool, bool, Optional[int], Optional[int]]: |
78 | 79 | """ |
79 | 80 | Returns: |
80 | | - is_civitai_model_url: True if the url is a civitai model url |
81 | | - is_civitai_api_url: True if the url is a civitai api url |
82 | | - model_id: The model id or None if it's api url |
83 | | - version_id: The version id or None if it doesn't have version id info |
| 81 | + is_civitai_model_url: True if the url is a civitai *web* model url (e.g. /models/12345) |
| 82 | + is_civitai_api_url: True if the url is a civitai *api* url useful for resolving downloads |
| 83 | + model_id: The model id (for /models/*), else None |
| 84 | + version_id: The version id (for /api/download/models/* or ?modelVersionId=), else None |
84 | 85 | """ |
85 | | - prefix = "civitai.com" |
86 | 86 | try: |
87 | | - if prefix in url: |
88 | | - # URL is civitai api download url: https://civitai.com/api/download/models/12345 |
89 | | - if "civitai.com/api/download" in url: |
90 | | - # This is a direct download link |
91 | | - version_id = url.strip("/").split("/")[-1] |
92 | | - return False, True, None, int(version_id) |
93 | | - |
94 | | - # URL is civitai web url (e.g. |
95 | | - # - https://civitai.com/models/43331 |
96 | | - # - https://civitai.com/models/43331/majicmix-realistic |
97 | | - subpath = url[url.find(prefix) + len(prefix) :].strip("/") |
98 | | - url_parts = subpath.split("?") |
99 | | - if len(url_parts) > 1: |
100 | | - model_id = url_parts[0].split("/")[1] |
101 | | - version_id = url_parts[1].split("=")[1] |
102 | | - return True, False, int(model_id), int(version_id) |
103 | | - else: |
104 | | - model_id = subpath.split("/")[1] |
105 | | - return True, False, int(model_id), None |
106 | | - except (ValueError, IndexError): |
| 87 | + parsed = urlparse(url) |
| 88 | + host = (parsed.hostname or "").lower() |
| 89 | + if host != "civitai.com" and not host.endswith(".civitai.com"): |
| 90 | + return False, False, None, None |
| 91 | + p_parts = [p for p in parsed.path.split("/") if p] |
| 92 | + query = parse_qs(parsed.query) |
| 93 | + |
| 94 | + if len(p_parts) >= 4 and p_parts[0] == "api": |
| 95 | + # Case 1: /api/download/models/<version_id> |
| 96 | + # e.g. https://civitai.com/api/download/models/1617665?type=Model&format=SafeTensor |
| 97 | + if p_parts[1] == "download" and p_parts[2] == "models": |
| 98 | + try: |
| 99 | + version_id = int(p_parts[3]) |
| 100 | + return False, True, None, version_id |
| 101 | + except ValueError: |
| 102 | + return False, True, None, None |
| 103 | + |
| 104 | + # Case 2: /api/v1/model-versions/<version_id> |
| 105 | + if p_parts[1] == "v1" and p_parts[2] in ("model-versions", "modelVersions"): |
| 106 | + try: |
| 107 | + version_id = int(p_parts[3]) |
| 108 | + return False, True, None, version_id |
| 109 | + except ValueError: |
| 110 | + return False, True, None, None |
| 111 | + |
| 112 | + # Case 3: /models/<model_id>[/*] with optional ?modelVersionId=<id> |
| 113 | + # e.g. https://civitai.com/models/43331 |
| 114 | + # https://civitai.com/models/43331/majicmix-realistic?modelVersionId=485088 |
| 115 | + if len(p_parts) >= 2 and p_parts[0] == "models": |
| 116 | + try: |
| 117 | + model_id = int(p_parts[1]) |
| 118 | + except ValueError: |
| 119 | + return False, False, None, None |
| 120 | + version_id = None |
| 121 | + mv = query.get("modelVersionId") |
| 122 | + if mv and len(mv) > 0: |
| 123 | + with contextlib.suppress(ValueError): |
| 124 | + version_id = int(mv[0]) |
| 125 | + if version_id is None: |
| 126 | + mv = query.get("version") |
| 127 | + if mv and len(mv) > 0: |
| 128 | + with contextlib.suppress(ValueError): |
| 129 | + version_id = int(mv[0]) |
| 130 | + return True, False, model_id, version_id |
| 131 | + |
| 132 | + return False, False, None, None |
| 133 | + |
| 134 | + except Exception: |
107 | 135 | print("Error parsing CivitAI model URL") |
108 | | - |
109 | | - return False, False, None, None |
| 136 | + return False, False, None, None |
110 | 137 |
|
111 | 138 |
|
112 | 139 | def request_civitai_model_version_api(version_id: int, headers: Optional[dict] = None): |
|
0 commit comments