|
5 | 5 |
|
6 | 6 | import httpx |
7 | 7 |
|
8 | | -from .models import GyazoOEmbedResponse, PageDetail, PageListResponse |
| 8 | +from .models import GyazoOEmbedResponse, GyazoOEmbedResponsePhoto, PageDetail, PageListResponse |
9 | 9 |
|
10 | 10 | if TYPE_CHECKING: |
11 | 11 | from types import TracebackType |
@@ -139,15 +139,26 @@ def get_file(self, file_id: str) -> bytes: |
139 | 139 |
|
140 | 140 | parsed_url = urlparse(url) |
141 | 141 | if parsed_url.hostname and "gyazo.com" in parsed_url.hostname: |
142 | | - oembed_url = f"{self.BASE_URL}/oembed-proxy/gyazo" |
143 | | - response = self.client.get(oembed_url, params={"url": url}) |
144 | | - response.raise_for_status() |
145 | | - json = response.json() |
146 | | - if oembed_type := json.get("type") != "photo": |
147 | | - msg = f"Unsupported Gyazo oEmbed type: {oembed_type}" |
148 | | - raise ValueError(msg) |
149 | | - oembed_data = GyazoOEmbedResponse.model_validate(json) |
150 | | - url = oembed_data.root.url |
| 142 | + # If URL already has a file extension (e.g., .mp4, .jpg), directly convert to i.gyazo.com |
| 143 | + path = parsed_url.path.strip("/") |
| 144 | + if "." in path.split("/")[-1]: # Check if last path segment has extension |
| 145 | + url = f"https://i.gyazo.com/{path}" |
| 146 | + else: |
| 147 | + # Use oEmbed API to get the actual file URL |
| 148 | + oembed_url = f"{self.BASE_URL}/oembed-proxy/gyazo" |
| 149 | + response = self.client.get(oembed_url, params={"url": url}) |
| 150 | + response.raise_for_status() |
| 151 | + json = response.json() |
| 152 | + if (oembed_type := json.get("type")) not in ("photo", "video"): |
| 153 | + msg = f"Unsupported Gyazo oEmbed type: {oembed_type}" |
| 154 | + raise ValueError(msg) |
| 155 | + oembed_data = GyazoOEmbedResponse.model_validate(json) |
| 156 | + if isinstance(oembed_data.root, GyazoOEmbedResponsePhoto): |
| 157 | + url = oembed_data.root.url |
| 158 | + else: # video |
| 159 | + # Extract Gyazo ID from the original URL and construct direct video URL |
| 160 | + gyazo_id = parsed_url.path.strip("/") |
| 161 | + url = f"https://i.gyazo.com/{gyazo_id}.mp4" |
151 | 162 | response = self.client.get(url) |
152 | 163 | response.raise_for_status() |
153 | 164 |
|
|
0 commit comments