feat(twitter_threads): gallery-dl media download + inline media embeds in Markdown
download_media now shells out to gallery-dl (handles X.com auth/403/video->mp4) instead of urllib, returns the downloaded files, + media_files_for_post glob helper + --cookies CLI. render_markdown embeds images with  and videos with an HTML <video controls> tag (were plain links). extract_corpus uses the package download_media (one download path). Tests rewritten for the gallery-dl API. Corpus: 4 threads, 33 media (32 img embeds + 1 video), 0 missing.
This commit is contained in:
@@ -43,6 +43,14 @@ def _render_yaml(thread: ThreadData, title: str) -> list[str]:
|
||||
"---",
|
||||
]
|
||||
|
||||
def _media_line(index: int, name: str) -> str:
|
||||
ext = name.rsplit(".", 1)[-1].lower() if "." in name else ""
|
||||
if ext in ("mp4", "mov", "webm", "m4v"):
|
||||
return f'<video controls src="./media/{name}"></video>'
|
||||
if ext in ("jpg", "jpeg", "png", "gif", "webp", "bmp"):
|
||||
return f""
|
||||
return f"[Media {index}](./media/{name})"
|
||||
|
||||
def _render_post(post: PostData, n: int, id_to_index: dict[str, int], media_names: dict[str, list[str]]) -> list[str]:
|
||||
lines: list[str] = []
|
||||
header = f"## Post {n} ({post.timestamp})"
|
||||
@@ -58,7 +66,7 @@ def _render_post(post: PostData, n: int, id_to_index: dict[str, int], media_name
|
||||
names = media_names.get(post.post_id, [])
|
||||
if names:
|
||||
for k, name in enumerate(names, start=1):
|
||||
lines.append(f"[Media {k}](./media/{name})")
|
||||
lines.append(_media_line(k, name))
|
||||
lines.append("")
|
||||
return lines
|
||||
|
||||
|
||||
Reference in New Issue
Block a user