110 lines
4.5 KiB
Python
110 lines
4.5 KiB
Python
from __future__ import annotations
|
|||
|
|
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
from unittest.mock import Mock, patch
|
||
|
|
|
||
|
|
from pixiv_app.core.plugin.manager import PluginManager
|
||
|
|
from plugins.douyin.plugin import DouyinPlugin
|
||
|
|
|
||
|
|
|
||
|
|
VIDEO_HTML = """
|
||
|
|
<html><body>
|
||
|
|
<script id="RENDER_DATA" type="application/json">
|
||
|
|
%7B%22aweme%22%3A%7B%22aweme_id%22%3A%227321234567890123456%22%2C%22desc%22%3A%22test%20video%22%2C%22author%22%3A%7B%22nickname%22%3A%22sakura%22%2C%22uid%22%3A%22u1%22%7D%2C%22video%22%3A%7B%22play_addr%22%3A%7B%22url_list%22%3A%5B%22https%3A%5C%2F%5C%2Fv3.douyinvod.com%5C%2Fvideo.mp4%22%5D%7D%7D%7D%7D
|
||
|
|
</script>
|
||
|
|
</body></html>
|
||
|
|
"""
|
||
|
|
|
||
|
|
IMAGE_HTML = """
|
||
|
|
<html><body>
|
||
|
|
<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" type="application/json">
|
||
|
|
{"aweme":{"aweme_id":"7321234567890123457","desc":"test images","author":{"nickname":"hana","uid":"u2"},"images":[{"url_list":["https:\\/\\/p3-pc.douyinpic.com\\/a.jpg"]},{"url_list":["https:\\/\\/p3-pc.douyinpic.com\\/b.webp"]}]}}
|
||
|
|
</script>
|
||
|
|
</body></html>
|
||
|
|
"""
|
||
|
|
|
||
|
|
|
||
|
|
class FakeResponse:
|
||
|
|
def __init__(self, url: str, text: str = "", content: bytes = b"video-bytes" * 80, content_type: str = "video/mp4") -> None:
|
||
|
|
self.url = url
|
||
|
|
self.text = text
|
||
|
|
self.content = content
|
||
|
|
self.headers = {"content-type": content_type}
|
||
|
|
|
||
|
|
def raise_for_status(self) -> None:
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
class FakeClient:
|
||
|
|
def __init__(self, *args, **kwargs) -> None:
|
||
|
|
self.requests: list[str] = []
|
||
|
|
|
||
|
|
def __enter__(self) -> "FakeClient":
|
||
|
|
return self
|
||
|
|
|
||
|
|
def __exit__(self, *args) -> None:
|
||
|
|
return None
|
||
|
|
|
||
|
|
def get(self, url: str, *args, **kwargs) -> FakeResponse:
|
||
|
|
self.requests.append(url)
|
||
|
|
if "note" in url:
|
||
|
|
return FakeResponse("https://www.douyin.com/note/7321234567890123457", IMAGE_HTML)
|
||
|
|
if "v.douyin.com" in url:
|
||
|
|
return FakeResponse("https://www.douyin.com/video/7321234567890123456", VIDEO_HTML)
|
||
|
|
if url.endswith(".jpg") or url.endswith(".webp"):
|
||
|
|
return FakeResponse(url, content=b"image-bytes" * 80, content_type="image/jpeg")
|
||
|
|
return FakeResponse(url, VIDEO_HTML)
|
||
|
|
|
||
|
|
|
||
|
|
class DouyinPluginTests(unittest.TestCase):
|
||
|
|
def test_can_handle_douyin_urls(self) -> None:
|
||
|
|
plugin = DouyinPlugin()
|
||
|
|
self.assertTrue(plugin.can_handle("https://v.douyin.com/abc123/"))
|
||
|
|
self.assertTrue(plugin.can_handle("https://www.douyin.com/video/7321234567890123456"))
|
||
|
|
self.assertTrue(plugin.can_handle("https://www.douyin.com/note/7321234567890123457"))
|
||
|
|
self.assertFalse(plugin.can_handle("https://x.com/test/status/1"))
|
||
|
|
|
||
|
|
@patch("plugins.douyin.plugin.httpx.Client", FakeClient)
|
||
|
|
def test_parse_video_from_render_data(self) -> None:
|
||
|
|
plugin = DouyinPlugin()
|
||
|
|
resource = plugin.parse("https://v.douyin.com/mock/")[0]
|
||
|
|
self.assertEqual(resource.id, "7321234567890123456")
|
||
|
|
self.assertEqual(resource.title, "test video")
|
||
|
|
self.assertEqual(resource.author, "sakura")
|
||
|
|
self.assertEqual(resource.files[0]["type"], "video")
|
||
|
|
self.assertIn("douyinvod.com/video.mp4", resource.files[0]["url"])
|
||
|
|
|
||
|
|
@patch("plugins.douyin.plugin.httpx.Client", FakeClient)
|
||
|
|
def test_parse_image_note(self) -> None:
|
||
|
|
plugin = DouyinPlugin()
|
||
|
|
resource = plugin.parse("https://www.douyin.com/note/7321234567890123457")[0]
|
||
|
|
self.assertEqual(resource.id, "7321234567890123457")
|
||
|
|
self.assertEqual(resource.title, "test images")
|
||
|
|
self.assertEqual([item["type"] for item in resource.files], ["image", "image"])
|
||
|
|
|
||
|
|
@patch("plugins.douyin.plugin.httpx.Client", FakeClient)
|
||
|
|
def test_download_video_entry(self) -> None:
|
||
|
|
plugin = DouyinPlugin()
|
||
|
|
resource = plugin.parse("https://v.douyin.com/mock/")[0]
|
||
|
|
with tempfile.TemporaryDirectory() as tmp:
|
||
|
|
paths = plugin.download(resource, Path(tmp))
|
||
|
|
self.assertEqual(len(paths), 1)
|
||
|
|
self.assertTrue(paths[0].is_file())
|
||
|
|
self.assertEqual(paths[0].suffix, ".mp4")
|
||
|
|
|
||
|
|
def test_plugin_registration(self) -> None:
|
||
|
|
root = Path(__file__).resolve().parents[1]
|
||
|
|
if str(root) not in sys.path:
|
||
|
|
sys.path.insert(0, str(root))
|
||
|
|
manager = PluginManager([root / "plugins"])
|
||
|
|
plugin = manager.load_plugin(root / "plugins" / "douyin" / "plugin.py")
|
||
|
|
self.assertIsNotNone(plugin)
|
||
|
|
assert plugin is not None
|
||
|
|
self.assertIs(manager.get_plugin_for_url("https://www.douyin.com/video/7321234567890123456"), plugin)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|