feat: add ouge list and search parsing
This commit is contained in:
@@ -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 = """
|
||||
<div id="main">
|
||||
<div class="module-item">
|
||||
<div class="module-item-pic">
|
||||
<a href="/index.php/vod/detail/id/123.html"></a>
|
||||
<img data-src="/poster.jpg" alt="示例影片" />
|
||||
</div>
|
||||
<div class="module-item-text">HD</div>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
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 = """
|
||||
<div id="main">
|
||||
<div class="module-item">
|
||||
<div class="module-item-pic">
|
||||
<a href="/index.php/vod/detail/id/456.html"></a>
|
||||
<img data-src="/cate.jpg" alt="分类影片" />
|
||||
</div>
|
||||
<div class="module-item-text">更新至10集</div>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
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 = """
|
||||
<div class="module-search-item">
|
||||
<a class="video-serial" href="/index.php/vod/detail/id/789.html" title="搜索影片">抢先版</a>
|
||||
<div class="module-item-pic">
|
||||
<img data-src="/search.jpg" alt="搜索影片" />
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
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()
|
||||
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user