86 lines
3.3 KiB
Python
86 lines
3.3 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import r34vault_downloader as dl
|
|
|
|
|
|
def test_extract_bookmark_items_from_ng_state_preserves_order():
|
|
state = {
|
|
"get:/api/v2/account/user/krosh": {"id": 50311, "userName": "krosh"},
|
|
"post:/api/v2/post/search/bookmarked/50311": {
|
|
"items": [
|
|
{"id": 1075647, "type": 0},
|
|
{"id": 996379, "type": 1},
|
|
{"id": 1263322, "type": 0},
|
|
]
|
|
},
|
|
}
|
|
html = f'<script id="ng-state" type="application/json">{json.dumps(state)}</script>'
|
|
|
|
parsed = dl.parse_ng_state(html)
|
|
user_id = dl.extract_user_id(parsed, "krosh")
|
|
items = dl.extract_bookmark_items_from_state(parsed, user_id)
|
|
|
|
assert user_id == 50311
|
|
assert [item["id"] for item in items] == [1075647, 996379, 1263322]
|
|
|
|
|
|
def test_candidate_image_urls_prefers_original_rule34vault_then_cdn():
|
|
assert dl.post_prefix("1263322") == "1263"
|
|
assert dl.post_prefix("738794") == "738"
|
|
assert list(dl.candidate_image_urls("1263322"))[:4] == [
|
|
"https://rule34vault.com/posts/1263/1263322/1263322.jpg",
|
|
"https://r34xyz.b-cdn.net/posts/1263/1263322/1263322.jpg",
|
|
"https://rule34vault.com/posts/1263/1263322/1263322.png",
|
|
"https://r34xyz.b-cdn.net/posts/1263/1263322/1263322.png",
|
|
]
|
|
|
|
|
|
def test_original_from_derivative_url_removes_small_preview_suffix():
|
|
assert dl.original_from_derivative_url(
|
|
"https://rule34vault.com/posts/1263/1263303/1263303.small.jpg", "1263303"
|
|
) == "https://rule34vault.com/posts/1263/1263303/1263303.jpg"
|
|
assert dl.original_from_derivative_url(
|
|
"/posts/1263/1263303/1263303.preview.webp", "1263303"
|
|
) == "/posts/1263/1263303/1263303.webp"
|
|
|
|
|
|
def test_extract_media_sources_from_post_html_finds_video_and_originalized_images():
|
|
html = """
|
|
<html><body>
|
|
<img class="img" src="/posts/1263/1263303/1263303.small.jpg">
|
|
<video controls>
|
|
<source type="video/mp4; codecs=hvc1" src="https://r34xyz.b-cdn.net/posts/996/996379/996379.720.hevc.mp4">
|
|
<source type="video/mp4" src="https://r34xyz.b-cdn.net/posts/996/996379/996379.480.mp4">
|
|
</video>
|
|
</body></html>
|
|
"""
|
|
|
|
sources = dl.extract_media_sources_from_html(html, "1263303", "https://rule34vault.com/post/1263303")
|
|
|
|
assert "https://rule34vault.com/posts/1263/1263303/1263303.jpg" in sources.images
|
|
assert "https://rule34vault.com/posts/1263/1263303/1263303.small.jpg" in sources.image_fallbacks
|
|
assert sources.videos == [
|
|
"https://r34xyz.b-cdn.net/posts/996/996379/996379.720.hevc.mp4",
|
|
"https://r34xyz.b-cdn.net/posts/996/996379/996379.480.mp4",
|
|
]
|
|
|
|
|
|
def test_choose_video_source_prefers_non_hevc_mp4():
|
|
sources = [
|
|
"https://r34xyz.b-cdn.net/posts/996/996379/996379.720.hevc.mp4",
|
|
"https://r34xyz.b-cdn.net/posts/996/996379/996379.480.mp4",
|
|
]
|
|
|
|
assert dl.choose_video_source(sources) == "https://r34xyz.b-cdn.net/posts/996/996379/996379.480.mp4"
|
|
|
|
|
|
def test_append_download_order_writes_plain_text_manifest(tmp_path):
|
|
manifest = tmp_path / "download_order.txt"
|
|
|
|
dl.append_download_order(manifest, 3, "996379", Path("downloads/996/996379.mp4"), "https://example/file.mp4")
|
|
|
|
assert manifest.read_text() == "000003 996379 downloads/996/996379.mp4 https://example/file.mp4\n"
|