Skip to content

Commit b93072c

Browse files
committed
feat: fetch a video from oEmbed
1 parent 9608a5d commit b93072c

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

scrapbox/client.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import httpx
77

8-
from .models import GyazoOEmbedResponse, PageDetail, PageListResponse
8+
from .models import GyazoOEmbedResponse, GyazoOEmbedResponsePhoto, PageDetail, PageListResponse
99

1010
if TYPE_CHECKING:
1111
from types import TracebackType
@@ -139,15 +139,26 @@ def get_file(self, file_id: str) -> bytes:
139139

140140
parsed_url = urlparse(url)
141141
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"
151162
response = self.client.get(url)
152163
response.raise_for_status()
153164

scrapbox/models.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,28 @@ class GyazoOEmbedResponsePhoto(BaseModel):
107107
title: str
108108

109109

110-
class GyazoOEmbedResponse(RootModel[GyazoOEmbedResponsePhoto]):
110+
class GyazoOEmbedResponseVideo(BaseModel):
111+
"""Video information in the Gyazo oEmbed response."""
112+
113+
model_config = ConfigDict(alias_generator=to_camel, from_attributes=True, populate_by_name=True)
114+
115+
type: Literal["video"]
116+
version: Literal["1.0"]
117+
provider_name: str
118+
provider_url: str
119+
html: str
120+
thumbnail_url: str
121+
thumbnail_width: int
122+
thumbnail_height: int
123+
has_audio_track: bool
124+
video_length_ms: int
125+
width: int
126+
height: int
127+
scale: float
128+
title: str
129+
130+
131+
class GyazoOEmbedResponse(RootModel[GyazoOEmbedResponsePhoto | GyazoOEmbedResponseVideo]):
111132
"""Response from the Gyazo oEmbed API.
112133
113134
See: https://gyazo.com/api/docs/image#oembed

0 commit comments

Comments
 (0)