From 7b1449c7ab81e77e02528b1288b2412c30cdd63f Mon Sep 17 00:00:00 2001 From: Harold <8866033@gmail.com> Date: Mon, 20 Apr 2026 14:57:18 +0800 Subject: [PATCH] feat: add ouge list and search parsing --- py/tests/test_欧歌.py | 80 +++++++++++++++++++++++++++++++++++++++ py/欧歌.py | 87 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) diff --git a/py/tests/test_欧歌.py b/py/tests/test_欧歌.py index 8b5c674..85b940a 100644 --- a/py/tests/test_欧歌.py +++ b/py/tests/test_欧歌.py @@ -1,6 +1,7 @@ import unittest from importlib.machinery import SourceFileLoader from pathlib import Path +from unittest.mock import patch ROOT = Path(__file__).resolve().parents[1] @@ -55,6 +56,85 @@ class TestOuGeSpider(unittest.TestCase): ("", ""), ) + def test_parse_cards_extracts_short_path_id_and_cover(self): + html = """ +
+
+
+ + 示例影片 +
+
HD
+
+
+ """ + self.assertEqual( + self.spider._parse_cards(html), + [ + { + "vod_id": "/index.php/vod/detail/id/123.html", + "vod_name": "示例影片", + "vod_pic": "https://woog.nxog.eu.org/poster.jpg", + "vod_remarks": "HD", + } + ], + ) + + @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 = """ +
+
+
+ + 分类影片 +
+
更新至10集
+
+
+ """ + result = self.spider.categoryContent("2", "3", False, {}) + self.assertEqual( + mock_request_html.call_args.args[0], + "https://woog.nxog.eu.org/index.php/vod/show/id/2/page/3.html", + ) + self.assertEqual(result["page"], 3) + self.assertEqual(result["limit"], 1) + self.assertEqual(result["list"][0]["vod_name"], "分类影片") + self.assertNotIn("pagecount", result) + + @patch.object(Spider, "_request_html") + def test_search_content_builds_reference_url_and_parses_results(self, mock_request_html): + mock_request_html.return_value = """ +
+ 抢先版 +
+ 搜索影片 +
+
+ """ + result = self.spider.searchContent("繁花", False, "2") + self.assertEqual( + mock_request_html.call_args.args[0], + "https://woog.nxog.eu.org/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": "https://woog.nxog.eu.org/search.jpg", + "vod_remarks": "抢先版", + }, + ) + self.assertNotIn("pagecount", result) + + @patch.object(Spider, "_request_html") + def test_search_content_short_circuits_blank_keyword(self, mock_request_html): + result = self.spider.searchContent("", False, "1") + self.assertEqual(result, {"page": 1, "total": 0, "list": []}) + mock_request_html.assert_not_called() + if __name__ == "__main__": unittest.main() diff --git a/py/欧歌.py b/py/欧歌.py index 24021af..965f9b6 100644 --- a/py/欧歌.py +++ b/py/欧歌.py @@ -1,6 +1,7 @@ # coding=utf-8 import re import sys +from urllib.parse import quote from urllib.parse import urljoin from base.spider import Spider as BaseSpider @@ -92,3 +93,89 @@ class Spider(BaseSpider): if re.search(pattern, raw, re.I): return pan_type, title return "", "" + + def _fix_img_url(self, img_url): + raw = self._stringify(img_url).strip() + if not raw: + return "" + if raw.startswith(("http://", "https://")): + return raw + return self._build_url(raw) + + def _build_category_url(self, tid, pg): + return self._build_url(f"/index.php/vod/show/id/{self._stringify(tid).strip()}/page/{int(pg)}.html") + + def _build_search_url(self, key, pg): + return self._build_url(f"/index.php/vod/search/page/{int(pg)}/wd/{quote(self._stringify(key).strip())}.html") + + def _parse_cards(self, html): + root = self.html(html) + if root is None: + return [] + items = [] + seen = set() + for card in root.xpath("//*[@id='main']//*[contains(@class,'module-item')]"): + href = ((card.xpath(".//*[contains(@class,'module-item-pic')]//a[@href][1]/@href") or [""])[0]).strip() + title = ((card.xpath(".//*[contains(@class,'module-item-pic')]//img[@alt][1]/@alt") or [""])[0]).strip() + pic = ( + ((card.xpath(".//*[contains(@class,'module-item-pic')]//img[@data-src][1]/@data-src") or [""])[0]).strip() + or ((card.xpath(".//*[contains(@class,'module-item-pic')]//img[@src][1]/@src") or [""])[0]).strip() + ) + remarks = self._clean_text("".join(card.xpath(".//*[contains(@class,'module-item-text')][1]//text()"))) + if not href or not title or href in seen: + continue + seen.add(href) + items.append( + { + "vod_id": href, + "vod_name": title, + "vod_pic": self._fix_img_url(pic), + "vod_remarks": remarks, + } + ) + return items + + def _parse_search_cards(self, html): + root = self.html(html) + if root is None: + return [] + items = [] + seen = set() + for card in root.xpath("//*[contains(@class,'module-search-item')]"): + href = ((card.xpath(".//*[contains(@class,'video-serial')][1]/@href") or [""])[0]).strip() + title = ( + ((card.xpath(".//*[contains(@class,'video-serial')][1]/@title") or [""])[0]).strip() + or ((card.xpath(".//*[contains(@class,'module-item-pic')]//img[@alt][1]/@alt") or [""])[0]).strip() + ) + pic = ( + ((card.xpath(".//*[contains(@class,'module-item-pic')]//img[@data-src][1]/@data-src") or [""])[0]).strip() + or ((card.xpath(".//*[contains(@class,'module-item-pic')]//img[@src][1]/@src") or [""])[0]).strip() + ) + remarks = self._clean_text("".join(card.xpath(".//*[contains(@class,'video-serial')][1]//text()"))) + if not remarks: + remarks = self._clean_text("".join(card.xpath(".//*[contains(@class,'module-item-text')][1]//text()"))) + if not href or not title or href in seen: + continue + seen.add(href) + items.append( + { + "vod_id": href, + "vod_name": title, + "vod_pic": self._fix_img_url(pic), + "vod_remarks": remarks, + } + ) + return items + + def categoryContent(self, tid, pg, filter, extend): + page = int(pg) + items = self._parse_cards(self._request_html(self._build_category_url(tid, pg))) + return {"list": items, "page": page, "limit": len(items), "total": page * 20 + len(items)} + + def searchContent(self, key, quick, pg="1"): + page = int(pg) + keyword = self._stringify(key).strip() + if not keyword: + return {"page": page, "total": 0, "list": []} + items = self._parse_search_cards(self._request_html(self._build_search_url(keyword, pg))) + return {"page": page, "total": len(items), "list": items}