feat: add AAZ音乐 search parsing
This commit is contained in:
+29
-1
@@ -1,7 +1,7 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import quote, urljoin
|
||||||
|
|
||||||
from base.spider import Spider as BaseSpider
|
from base.spider import Spider as BaseSpider
|
||||||
|
|
||||||
@@ -119,6 +119,26 @@ class Spider(BaseSpider):
|
|||||||
)
|
)
|
||||||
return items
|
return items
|
||||||
|
|
||||||
|
def _parse_search_cards(self, html):
|
||||||
|
root = self._load_html(html)
|
||||||
|
items = []
|
||||||
|
seen = set()
|
||||||
|
for node in root.xpath("//li"):
|
||||||
|
href = "".join(node.xpath(".//div[contains(@class,'name')]//a[1]/@href")).strip()
|
||||||
|
vod_id = self._encode_vod_id(href)
|
||||||
|
if not vod_id or vod_id in seen:
|
||||||
|
continue
|
||||||
|
seen.add(vod_id)
|
||||||
|
items.append(
|
||||||
|
{
|
||||||
|
"vod_id": vod_id,
|
||||||
|
"vod_name": self._clean_text("".join(node.xpath(".//div[contains(@class,'name')]//a[1]/@title"))),
|
||||||
|
"vod_pic": self._build_url("".join(node.xpath(".//img[1]/@src"))),
|
||||||
|
"vod_remarks": "",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return items
|
||||||
|
|
||||||
def homeContent(self, filter):
|
def homeContent(self, filter):
|
||||||
items = self._parse_song_cards(self._fetch_html(self.category_paths["new"]))
|
items = self._parse_song_cards(self._fetch_html(self.category_paths["new"]))
|
||||||
return {"class": list(self.classes), "list": items}
|
return {"class": list(self.classes), "list": items}
|
||||||
@@ -141,3 +161,11 @@ class Spider(BaseSpider):
|
|||||||
else:
|
else:
|
||||||
items = self._parse_folder_cards(html, "mv")
|
items = self._parse_folder_cards(html, "mv")
|
||||||
return {"page": int(pg), "limit": len(items), "total": len(items), "list": items}
|
return {"page": int(pg), "limit": len(items), "total": len(items), "list": items}
|
||||||
|
|
||||||
|
def searchContent(self, key, quick, pg="1"):
|
||||||
|
keyword = self._clean_text(key)
|
||||||
|
if not keyword:
|
||||||
|
return self._empty_result(1)
|
||||||
|
html = self._fetch_html("/so/%s.html" % quote(keyword))
|
||||||
|
items = self._parse_search_cards(html)
|
||||||
|
return {"page": int(pg), "limit": len(items), "total": len(items), "list": items}
|
||||||
|
|||||||
@@ -60,6 +60,18 @@ MV_HTML = """
|
|||||||
</body></html>
|
</body></html>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
SEARCH_HTML = """
|
||||||
|
<html><body>
|
||||||
|
<ul>
|
||||||
|
<li><div class="name"><a href="/m/2001.html" title="夜曲">夜曲</a></div></li>
|
||||||
|
<li><div class="name"><a href="/s/jay" title="周杰伦">周杰伦</a></div></li>
|
||||||
|
<li><div class="name"><a href="/p/jaybest" title="周董精选">周董精选</a></div></li>
|
||||||
|
<li><div class="name"><a href="/a/fantasy" title="范特西">范特西</a></div></li>
|
||||||
|
<li><div class="name"><a href="/v/nocturnemv" title="夜曲MV">夜曲MV</a></div></li>
|
||||||
|
</ul>
|
||||||
|
</body></html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class TestAAZMusicSpider(unittest.TestCase):
|
class TestAAZMusicSpider(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -108,6 +120,21 @@ class TestAAZMusicSpider(unittest.TestCase):
|
|||||||
{"page": 1, "limit": 0, "total": 0, "list": []},
|
{"page": 1, "limit": 0, "total": 0, "list": []},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@patch.object(Spider, "fetch")
|
||||||
|
def test_search_content_maps_mixed_result_types(self, mock_fetch):
|
||||||
|
mock_fetch.return_value = SimpleNamespace(status_code=200, text=SEARCH_HTML)
|
||||||
|
result = self.spider.searchContent("周杰伦", False, "1")
|
||||||
|
self.assertEqual(
|
||||||
|
[item["vod_id"] for item in result["list"]],
|
||||||
|
["song:2001", "singer:jay", "playlist:jaybest", "album:fantasy", "mv:nocturnemv"],
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_search_content_returns_empty_for_blank_keyword(self):
|
||||||
|
self.assertEqual(
|
||||||
|
self.spider.searchContent("", False, "1"),
|
||||||
|
{"page": 1, "limit": 0, "total": 0, "list": []},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user