玩偶聚合

This commit is contained in:
Harold
2026-04-21 19:52:30 +08:00
parent 552243cccd
commit 7aa6f4f8df
4 changed files with 492 additions and 131 deletions
+61 -6
View File
@@ -33,7 +33,7 @@ class TestErXiaoSpider(unittest.TestCase):
def test_build_url_and_detect_pan_type(self):
self.assertEqual(
self.spider._build_url("/index.php/vod/detail/id/1.html"),
"http://2xiaopan.fun/index.php/vod/detail/id/1.html",
f"{self.spider.host}/index.php/vod/detail/id/1.html",
)
self.assertEqual(self.spider._detect_pan_type("https://pan.baidu.com/s/demo"), ("baidu", "百度资源"))
self.assertEqual(self.spider._detect_pan_type("https://pan.quark.cn/s/demo"), ("quark", "夸克资源"))
@@ -58,13 +58,55 @@ class TestErXiaoSpider(unittest.TestCase):
{
"vod_id": "/index.php/vod/detail/id/123.html",
"vod_name": "示例影片",
"vod_pic": "http://2xiaopan.fun/poster.jpg",
"vod_pic": f"{self.spider.host}/poster.jpg",
"vod_remarks": "HD",
"vod_year": "2025",
}
],
)
def test_parse_cards_ignores_module_items_wrapper_and_keeps_each_card_separate(self):
html = """
<div id="main">
<div class="module-items">
<div class="module-item">
<div class="module-item-pic">
<a href="/index.php/vod/detail/id/111.html"></a>
<img data-src="/poster-1.jpg" alt="影片一" />
</div>
<div class="module-item-text">HD</div>
</div>
<div class="module-item">
<div class="module-item-pic">
<a href="/index.php/vod/detail/id/222.html"></a>
<img data-src="/poster-2.jpg" alt="影片二" />
</div>
<div class="module-item-text">更新至2集</div>
</div>
</div>
</div>
"""
cards = self.spider._parse_cards(html)
self.assertEqual(
cards,
[
{
"vod_id": "/index.php/vod/detail/id/111.html",
"vod_name": "影片一",
"vod_pic": f"{self.spider.host}/poster-1.jpg",
"vod_remarks": "HD",
"vod_year": "",
},
{
"vod_id": "/index.php/vod/detail/id/222.html",
"vod_name": "影片二",
"vod_pic": f"{self.spider.host}/poster-2.jpg",
"vod_remarks": "更新至2集",
"vod_year": "",
},
],
)
@patch.object(Spider, "_request_html")
def test_category_content_builds_reference_url_and_returns_page_payload(self, mock_request_html):
mock_request_html.return_value = """
@@ -82,7 +124,7 @@ class TestErXiaoSpider(unittest.TestCase):
result = self.spider.categoryContent("2", "3", False, {})
self.assertEqual(
mock_request_html.call_args.args[0],
"http://2xiaopan.fun/index.php/vod/show/id/2/page/3.html",
f"{self.spider.host}/index.php/vod/show/id/2/page/3.html",
)
self.assertEqual(result["page"], 3)
self.assertEqual(result["limit"], 1)
@@ -102,14 +144,14 @@ class TestErXiaoSpider(unittest.TestCase):
result = self.spider.searchContent("繁花", False, "2")
self.assertEqual(
mock_request_html.call_args.args[0],
"http://2xiaopan.fun/index.php/vod/search/page/2/wd/%E7%B9%81%E8%8A%B1.html",
f"{self.spider.host}/index.php/vod/search/page/2/wd/%E7%B9%81%E8%8A%B1.html",
)
self.assertEqual(
result["list"][0],
{
"vod_id": "/index.php/vod/detail/id/789.html",
"vod_name": "搜索影片",
"vod_pic": "http://2xiaopan.fun/search.jpg",
"vod_pic": f"{self.spider.host}/search.jpg",
"vod_remarks": "抢先版",
},
)
@@ -149,13 +191,26 @@ class TestErXiaoSpider(unittest.TestCase):
"""
detail = self.spider._parse_detail_page("/index.php/vod/detail/id/123.html", html)
self.assertEqual(detail["vod_name"], "示例剧")
self.assertEqual(detail["vod_pic"], "http://2xiaopan.fun/poster.jpg")
self.assertEqual(detail["vod_pic"], f"{self.spider.host}/poster.jpg")
self.assertEqual(detail["vod_year"], "2024")
self.assertEqual(detail["vod_director"], "导演甲")
self.assertEqual(detail["vod_actor"], "演员甲,演员乙")
self.assertEqual(detail["vod_content"], "一段剧情简介")
self.assertEqual(detail["pan_urls"], ["https://pan.quark.cn/s/q1", "https://pan.baidu.com/s/b1"])
def test_parse_detail_page_uses_only_first_mobile_play_poster(self):
html = """
<div class="page-title">示例剧</div>
<div class="mobile-play"><img class="lazyload" data-src="/poster-main.jpg" /></div>
<div class="mobile-play"><img class="lazyload" data-src="https://img.example.com/side.webp" /></div>
<div class="mobile-play"><img class="lazyload" data-src="https://img.example.com/recommend.jpg" /></div>
<div class="module-row-info">
<p>https://pan.baidu.com/s/b1</p>
</div>
"""
detail = self.spider._parse_detail_page("/index.php/vod/detail/id/123.html", html)
self.assertEqual(detail["vod_pic"], f"{self.spider.host}/poster-main.jpg")
@patch.object(Spider, "_request_html")
def test_detail_content_builds_pan_play_fields(self, mock_request_html):
mock_request_html.return_value = """
+157 -39
View File
@@ -18,15 +18,23 @@ class TestWanouAggregateSpider(unittest.TestCase):
self.spider = Spider()
self.spider.init()
def test_home_content_exposes_site_classes_and_category_filter(self):
def test_home_content_exposes_recommend_and_site_classes_in_priority_order(self):
content = self.spider.homeContent(False)
self.assertEqual(
[item["type_id"] for item in content["class"][:3]],
["site_wanou", "site_zhizhen", "site_shandian"],
[item["type_id"] for item in content["class"][:6]],
[
"site_recommend",
"site_wanou",
"site_muou",
"site_labi",
"site_zhizhen",
"site_erxiao",
],
)
self.assertEqual(content["class"][0]["type_name"], "玩偶")
self.assertEqual(content["class"][0]["type_name"], "推荐")
self.assertEqual(content["filters"]["site_wanou"][0]["key"], "categoryId")
self.assertEqual(content["filters"]["site_wanou"][0]["value"][1], {"n": "电影", "v": "1"})
self.assertEqual(content["filters"]["site_wanou"][0]["value"][1], {"n": "臻彩", "v": "44"})
self.assertEqual(content["filters"]["site_recommend"][0]["value"][0], {"n": "全部", "v": "all"})
def test_home_content_exposes_zhizhen_site_and_categories(self):
content = self.spider.homeContent(False)
@@ -35,40 +43,58 @@ class TestWanouAggregateSpider(unittest.TestCase):
self.assertEqual(
content["filters"]["site_zhizhen"][0]["value"][1:],
[
{"n": "臻彩", "v": "26"},
{"n": "电影", "v": "1"},
{"n": "", "v": "2"},
{"n": "电视", "v": "2"},
{"n": "动漫", "v": "3"},
{"n": "综艺", "v": "4"},
{"n": "短剧", "v": "5"},
{"n": "老剧", "v": "24"},
{"n": "严选", "v": "26"},
],
)
def test_home_content_exposes_shandian_site_and_categories(self):
def test_home_content_exposes_muou_site_and_categories(self):
content = self.spider.homeContent(False)
type_ids = [item["type_id"] for item in content["class"]]
self.assertIn("site_shandian", type_ids)
self.assertIn("site_muou", type_ids)
self.assertEqual(
content["filters"]["site_shandian"][0]["value"][1:],
content["filters"]["site_muou"][0]["value"][1:],
[
{"n": "臻选", "v": "25"},
{"n": "电影", "v": "1"},
{"n": "", "v": "2"},
{"n": "电视", "v": "2"},
{"n": "动漫", "v": "3"},
{"n": "纪录片", "v": "4"},
{"n": "综艺", "v": "29"},
{"n": "原盘", "v": "30"},
],
)
def test_home_content_exposes_kuaiying_and_ouge_site_categories(self):
content = self.spider.homeContent(False)
type_ids = [item["type_id"] for item in content["class"]]
self.assertIn("site_kuaiying", type_ids)
self.assertIn("site_ouge", type_ids)
self.assertEqual(
content["filters"]["site_kuaiying"][0]["value"][1:],
[
{"n": "臻彩", "v": "5"},
{"n": "电影", "v": "1"},
{"n": "电视剧", "v": "2"},
{"n": "综艺", "v": "3"},
{"n": "动漫", "v": "4"},
{"n": "短剧", "v": "30"},
{"n": "短剧", "v": "6"},
{"n": "115", "v": "30"},
{"n": "123", "v": "35"},
{"n": "天移迅", "v": "36"},
],
)
def test_home_content_exposes_ouge_site_and_categories(self):
content = self.spider.homeContent(False)
type_ids = [item["type_id"] for item in content["class"]]
self.assertIn("site_ouge", type_ids)
self.assertEqual(content["class"][-1]["type_name"], "欧哥")
self.assertEqual(
content["filters"]["site_ouge"][0]["value"][1:],
[
{"n": "电影", "v": "1"},
{"n": "", "v": "2"},
{"n": "电视", "v": "2"},
{"n": "动漫", "v": "3"},
{"n": "综艺", "v": "4"},
{"n": "短剧", "v": "5"},
@@ -182,6 +208,53 @@ class TestWanouAggregateSpider(unittest.TestCase):
],
)
def test_parse_cards_ignores_module_items_wrapper(self):
site = {"id": "erxiao", "domains": ["https://www.2xiaopan.top"], "list_xpath": "//*[contains(@class,'module-item')]"}
html = """
<div id="main">
<div class="module-items">
<div class="module-item">
<div class="module-item-pic">
<a href="/index.php/vod/detail/id/111.html"></a>
<img data-src="/poster-1.jpg" alt="影片一" />
</div>
<div class="module-item-text">HD</div>
</div>
<div class="module-item">
<div class="module-item-pic">
<a href="/index.php/vod/detail/id/222.html"></a>
<img data-src="/poster-2.jpg" alt="影片二" />
</div>
<div class="module-item-text">更新至2集</div>
</div>
</div>
</div>
"""
cards = self.spider._parse_cards(site, html)
self.assertEqual(
cards,
[
{
"vod_id": "site:erxiao:/index.php/vod/detail/id/111.html",
"vod_name": "影片一",
"vod_pic": "https://www.2xiaopan.top/poster-1.jpg",
"vod_remarks": "HD",
"vod_year": "",
"_site": "erxiao",
"_detail_path": "/index.php/vod/detail/id/111.html",
},
{
"vod_id": "site:erxiao:/index.php/vod/detail/id/222.html",
"vod_name": "影片二",
"vod_pic": "https://www.2xiaopan.top/poster-2.jpg",
"vod_remarks": "更新至2集",
"vod_year": "",
"_site": "erxiao",
"_detail_path": "/index.php/vod/detail/id/222.html",
},
],
)
@patch.object(Spider, "_request_with_failover")
def test_category_content_uses_default_category_when_extend_missing(self, mock_request_with_failover):
mock_request_with_failover.return_value = """
@@ -197,6 +270,37 @@ class TestWanouAggregateSpider(unittest.TestCase):
self.assertEqual(result["page"], 2)
self.assertEqual(result["list"][0]["vod_name"], "分类影片")
@patch.object(Spider, "_fetch_site_home_recommend")
def test_category_content_returns_recommend_results_with_site_tags(self, mock_fetch_site_home_recommend):
mock_fetch_site_home_recommend.side_effect = lambda site, limit=30: {
"wanou": [
{
"vod_id": "site:wanou:/voddetail/1.html",
"vod_name": "繁花",
"vod_pic": "https://img.example/w.jpg",
"vod_remarks": "玩偶版",
"vod_year": "2024",
"_site": "wanou",
"_detail_path": "/voddetail/1.html",
}
],
"muou": [
{
"vod_id": "site:muou:/voddetail/2.html",
"vod_name": "繁花",
"vod_pic": "https://img.example/m.jpg",
"vod_remarks": "木偶版",
"vod_year": "2024",
"_site": "muou",
"_detail_path": "/voddetail/2.html",
}
],
}.get(site["id"], [])
result = self.spider.categoryContent("site_recommend", "1", False, {"recommendSite": "all"})
self.assertEqual(result["total"], 1)
self.assertEqual(result["list"][0]["vod_name"], "繁花")
self.assertEqual(result["list"][0]["vod_remarks"], "[玩偶] 玩偶版")
def test_aggregate_search_results_merges_same_title_and_keeps_highest_priority_source(self):
raw_results = [
{
@@ -247,8 +351,8 @@ class TestWanouAggregateSpider(unittest.TestCase):
@patch.object(Spider, "_fetch_site_search")
def test_search_content_queries_sites_and_returns_aggregated_items(self, mock_fetch_site_search):
mock_fetch_site_search.side_effect = [
[
results_by_site = {
"wanou": [
{
"vod_id": "site:wanou:/voddetail/1.html",
"vod_name": "繁花",
@@ -259,7 +363,7 @@ class TestWanouAggregateSpider(unittest.TestCase):
"_detail_path": "/voddetail/1.html",
}
],
[
"muou": [
{
"vod_id": "site:muou:/voddetail/2.html",
"vod_name": "繁花",
@@ -270,8 +374,8 @@ class TestWanouAggregateSpider(unittest.TestCase):
"_detail_path": "/voddetail/2.html",
}
],
[],
]
}
mock_fetch_site_search.side_effect = lambda site, keyword, page: results_by_site.get(site["id"], [])
result = self.spider.searchContent("繁花", False, "1")
self.assertEqual(len(result["list"]), 1)
self.assertEqual(result["list"][0]["vod_name"], "繁花")
@@ -345,10 +449,22 @@ class TestWanouAggregateSpider(unittest.TestCase):
self.spider.playerContent("quark#玩偶", "https://pan.quark.cn/s/demo", {}),
{"parse": 0, "playUrl": "", "url": "https://pan.quark.cn/s/demo"},
)
self.assertEqual(
self.spider.playerContent("quark#玩偶", "https://www.quark.cn/s/demo", {}),
{"parse": 0, "playUrl": "", "url": "https://www.quark.cn/s/demo"},
)
self.assertEqual(
self.spider.playerContent("baidu#玩偶", "https://pan.baidu.com/s/demo", {}),
{"parse": 0, "playUrl": "", "url": "https://pan.baidu.com/s/demo"},
)
self.assertEqual(
self.spider.playerContent("a189#玩偶", "https://cloud.189.cn/t/demo", {}),
{"parse": 0, "playUrl": "", "url": "https://cloud.189.cn/t/demo"},
)
self.assertEqual(
self.spider.playerContent("a139#玩偶", "https://yun.139.com/share/demo", {}),
{"parse": 0, "playUrl": "", "url": "https://yun.139.com/share/demo"},
)
def test_player_content_rejects_non_pan_url(self):
self.assertEqual(
@@ -460,22 +576,24 @@ class TestWanouAggregateSpider(unittest.TestCase):
@patch.object(Spider, "_fetch_site_search")
def test_search_content_skips_site_errors(self, mock_fetch_site_search):
mock_fetch_site_search.side_effect = [
RuntimeError("boom"),
[
{
"vod_id": "site:muou:/voddetail/2.html",
"vod_name": "繁花",
"vod_pic": "",
"vod_remarks": "",
"vod_year": "2024",
"_site": "muou",
"_detail_path": "/voddetail/2.html",
}
],
[],
[],
]
def fake_fetch(site, keyword, page):
if site["id"] == "wanou":
raise RuntimeError("boom")
if site["id"] == "muou":
return [
{
"vod_id": "site:muou:/voddetail/2.html",
"vod_name": "繁花",
"vod_pic": "",
"vod_remarks": "",
"vod_year": "2024",
"_site": "muou",
"_detail_path": "/voddetail/2.html",
}
]
return []
mock_fetch_site_search.side_effect = fake_fetch
result = self.spider.searchContent("繁花", False, "1")
self.assertEqual(result["total"], 1)
self.assertEqual(result["list"][0]["vod_name"], "繁花")