2c680e23e4
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.
63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
|
|
"""Tests for scripts.twitter_threads.download_media gallery-dl subprocess contract."""
|
|
from __future__ import annotations
|
|
from pathlib import Path
|
|
from unittest.mock import patch, MagicMock
|
|
import pytest
|
|
from scripts.twitter_threads.error_types import PostMetrics, PostData, ThreadData
|
|
from scripts.twitter_threads.download_media import download_media, media_files_for_post
|
|
|
|
|
|
def _post(post_id: str) -> PostData:
|
|
metrics = PostMetrics(reply_count=0, repost_count=0, like_count=0, view_count=None)
|
|
return PostData(post_id=post_id, author="Tim", handle="NOTimothyLottes", text="t", timestamp="2024-02-01T00:00:00Z", media_urls=(), reply_to_id=None, quote_of_id=None, metrics=metrics)
|
|
|
|
|
|
def _thread(post_id: str) -> ThreadData:
|
|
return ThreadData(root_post_id=post_id, posts=(_post(post_id),), source_url=f"https://x.com/x/status/{post_id}")
|
|
|
|
|
|
def test_download_collects_matching_files(tmp_path: Path) -> None:
|
|
mediadir = tmp_path / "m"
|
|
mediadir.mkdir()
|
|
(mediadir / "123_1.jpg").write_bytes(b"x")
|
|
(mediadir / "123_2.png").write_bytes(b"x")
|
|
(mediadir / "999_1.jpg").write_bytes(b"x")
|
|
with patch("scripts.twitter_threads.download_media.subprocess.run", MagicMock(return_value=MagicMock(returncode=0))):
|
|
r = download_media(_thread("123"), mediadir)
|
|
assert r.is_ok
|
|
names = [p.name for p in r.data]
|
|
assert "123_1.jpg" in names
|
|
assert "123_2.png" in names
|
|
assert "999_1.jpg" not in names
|
|
|
|
|
|
def test_download_invokes_gallery_dl_with_url_and_cookies(tmp_path: Path) -> None:
|
|
mediadir = tmp_path / "m"
|
|
ck = tmp_path / "cookies.txt"
|
|
ck.write_text("x", encoding="utf-8")
|
|
with patch("scripts.twitter_threads.download_media.subprocess.run", MagicMock(return_value=MagicMock(returncode=0))) as mock:
|
|
download_media(_thread("55"), mediadir, ck)
|
|
assert mock.called
|
|
args = mock.call_args.args[0]
|
|
assert "gallery-dl" in args[0] or args[0] == "gallery-dl"
|
|
assert "https://x.com/x/status/55" in args
|
|
assert "--cookies" in args
|
|
assert str(ck) in args
|
|
|
|
|
|
def test_download_gallery_dl_error(tmp_path: Path) -> None:
|
|
with patch("scripts.twitter_threads.download_media.subprocess.run", MagicMock(return_value=MagicMock(returncode=1, stderr="boom"))):
|
|
r = download_media(_thread("123"), tmp_path / "m")
|
|
assert r.is_ok is False
|
|
assert r.error.kind == "GalleryDlError"
|
|
|
|
|
|
def test_media_files_for_post(tmp_path: Path) -> None:
|
|
d = tmp_path / "m"
|
|
d.mkdir()
|
|
(d / "123_1.jpg").write_bytes(b"x")
|
|
(d / "123_2.png").write_bytes(b"x")
|
|
(d / "999_1.jpg").write_bytes(b"x")
|
|
assert media_files_for_post(d, "123") == ["123_1.jpg", "123_2.png"]
|