fix(twitter_threads): strip trailing slashes from --media-dir and --output

Path objects with trailing separators (e.g. './media/') intermittently
failed to glob on Windows after download_media.py had just finished
writing the directory. Normalize via rstrip('/\\\\') before Path()
re-wrapping so the CLI is forgiving regardless of OS path quirks.

Repro: gallery-dl ran + immediate render with trailing slash on the
just-created media/ dir -> glob returned nothing -> markdown emitted
no ![Media N] lines. Removing the trailing slash fixed it; this makes
both work.
This commit is contained in:
ed
2026-07-26 21:12:20 -04:00
parent 952e164586
commit 0d51c3541d
12 changed files with 846 additions and 1 deletions
+28
View File
@@ -99,3 +99,31 @@ def test_render_cli(tmp_path: Path) -> None:
text = out.read_text(encoding="utf-8")
assert "[Media 1](./media/7_img1.jpg)" in text
assert "body" in text
def test_render_cli_trailing_slash_on_media_dir(tmp_path: Path) -> None:
p1 = _post("8", text="body", media_urls=("https://pbs.twimg.com/media/DDD.jpg",))
t = ThreadData(root_post_id="8", posts=(p1,), source_url="https://x.com/x/x/8")
thread_data_json = tmp_path / "thread_data.json"
thread_data_json.write_text(json.dumps(thread_to_dict(t)), encoding="utf-8")
mediadir = tmp_path / "media"
mediadir.mkdir()
(mediadir / "8_img1.jpg").write_bytes(b"x")
out = tmp_path / "thread.md"
code = render_main(["--input", str(thread_data_json), "--media-dir", "./" + str(mediadir).replace("\\", "/") + "/", "--output", str(out)])
assert code == 0
text = out.read_text(encoding="utf-8")
assert "[Media 1](./media/8_img1.jpg)" in text
assert "body" in text
def test_download_cli_trailing_slash_on_output(tmp_path: Path) -> None:
p1 = _post("9", media_urls=("https://pbs.twimg.com/media/EEE.jpg",))
t = ThreadData(root_post_id="9", posts=(p1,), source_url="https://x.com/x/x/9")
thread_data_json = tmp_path / "thread_data.json"
thread_data_json.write_text(json.dumps(thread_to_dict(t)), encoding="utf-8")
mediadir = tmp_path / "media"
mediadir.mkdir()
(mediadir / "9_1.jpg").write_bytes(b"x")
with patch("scripts.twitter_threads.download_media.subprocess.run", MagicMock(return_value=MagicMock(returncode=0))):
code = download_main(["--input", str(thread_data_json), "--output", "./" + str(mediadir).replace("\\", "/") + "/"])
assert code == 0
assert (mediadir / "9_1.jpg").exists()