# PPnix Spider Implementation Plan > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. **Goal:** Build a PPnix spider in `py/PPnix.py` with static categories and stable filters, deterministic HTML parsing, short IDs, direct m3u8 playback, and matching unit tests. **Architecture:** Keep the spider in one file following the repository's existing adapter pattern. Use small parsing helpers for URL building, card extraction, detail metadata extraction, and compact play-ID encoding so each behavior can be tested in isolation with embedded HTML fixtures. **Tech Stack:** Python 3.14, `unittest`, `unittest.mock`, `requests` via `base.spider.Spider.fetch`, `beautifulsoup4`, `urllib.parse`, `re` --- ### Task 1: Add failing tests for static metadata, URL builders, and card parsing **Files:** - Create: `py/tests/test_PPnix.py` - Create: `py/PPnix.py` - Test: `py/tests/test_PPnix.py` - [ ] **Step 1: Write the failing tests** ```python import unittest from importlib.machinery import SourceFileLoader from pathlib import Path ROOT = Path(__file__).resolve().parents[1] MODULE = SourceFileLoader("ppnix_spider", str(ROOT / "PPnix.py")).load_module() Spider = MODULE.Spider class TestPPnixSpider(unittest.TestCase): def setUp(self): Spider._instance = None self.spider = Spider() self.spider.init() def test_home_content_exposes_expected_categories_and_filter_keys(self): content = self.spider.homeContent(False) self.assertEqual([item["type_id"] for item in content["class"]], ["1", "2"]) self.assertEqual([item["key"] for item in content["filters"]["1"]], ["class", "by"]) self.assertEqual([item["key"] for item in content["filters"]["2"]], ["class", "by"]) def test_build_category_url_maps_first_page_and_sort_values(self): self.assertEqual( self.spider._build_category_url("1", "1", {}), "https://www.ppnix.com/cn/movie/----newstime.html", ) self.assertEqual( self.spider._build_category_url("2", "3", {"class": "爱情", "by": "hits"}), "https://www.ppnix.com/cn/tv/爱情---2-onclick.html", ) def test_build_search_url_uses_ppnix_pattern(self): self.assertEqual( self.spider._build_search_url("繁花", "1"), "https://www.ppnix.com/cn/search/%E7%B9%81%E8%8A%B1--.html", ) self.assertEqual( self.spider._build_search_url("繁花", "2"), "https://www.ppnix.com/cn/search/%E7%B9%81%E8%8A%B1--.html-page-2", ) def test_parse_cards_extracts_short_vod_id_title_cover_and_remarks(self): html = '''
''' self.assertEqual( self.spider._parse_cards(html), [ { "vod_id": "movie/123.html", "vod_name": "示例影片", "vod_pic": "https://www.ppnix.com/poster.jpg", "vod_remarks": "HD", } ], ) ``` - [ ] **Step 2: Run test to verify it fails** Run: `uv run python -m unittest py/tests/test_PPnix.py -v` Expected: FAIL because `py/PPnix.py` does not exist yet or required methods are missing - [ ] **Step 3: Write minimal implementation** ```python # coding=utf-8 import re import sys from urllib.parse import quote from bs4 import BeautifulSoup from base.spider import Spider as BaseSpider sys.path.append("..") class Spider(BaseSpider): def __init__(self): self.name = "PPnix[采]" self.host = "https://www.ppnix.com" self.lang_path = "/cn" self.headers = { "User-Agent": ( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " "AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/145.0.0.0 Safari/537.36" ), "Referer": self.host + self.lang_path + "/", } self.classes = [ {"type_id": "1", "type_name": "电影"}, {"type_id": "2", "type_name": "电视剧"}, ] self.filters = { "1": [ {"key": "class", "name": "类型", "init": "", "value": [{"n": "全部", "v": ""}, {"n": "动作", "v": "动作"}]}, {"key": "by", "name": "排序", "init": "time", "value": [{"n": "按时间", "v": "time"}, {"n": "按人气", "v": "hits"}, {"n": "按评分", "v": "score"}]}, ], "2": [ {"key": "class", "name": "类型", "init": "", "value": [{"n": "全部", "v": ""}, {"n": "爱情", "v": "爱情"}]}, {"key": "by", "name": "排序", "init": "time", "value": [{"n": "按时间", "v": "time"}, {"n": "按人气", "v": "hits"}, {"n": "按评分", "v": "score"}]}, ], } def init(self, extend=""): return None def getName(self): return self.name def homeContent(self, filter): return {"class": self.classes, "filters": self.filters} def _build_url(self, value): raw = str(value or "").strip() if not raw: return self.host + self.lang_path + "/" if raw.startswith(("http://", "https://")): return raw if raw.startswith("/"): return self.host + raw return self.host + "/" + raw def _map_type_slug(self, tid): return "tv" if str(tid) == "2" else "movie" def _map_sort(self, value): return {"time": "newstime", "hits": "onclick", "score": "rating"}.get(str(value or "time"), "newstime") def _build_category_url(self, tid, pg, extend): extend = extend or {} slug = self._map_type_slug(tid) genre = str(extend.get("class", "") or "") page = max(int(pg), 1) page_part = "" if page <= 1 else str(page - 1) sort = self._map_sort(extend.get("by", "time")) return self._build_url(f"{self.lang_path}/{slug}/{genre}---{page_part}-{sort}.html") def _build_search_url(self, keyword, pg): page = max(int(pg), 1) suffix = "" if page <= 1 else f"-page-{page}" return self._build_url(f"{self.lang_path}/search/{quote(str(keyword or '').strip())}--.html{suffix}") def _parse_cards(self, html): soup = BeautifulSoup(html or "", "html.parser") items = [] for node in soup.select("li"): anchor = node.select_one("a.thumbnail") or node.select_one("h2 a") or node.select_one("a") image = node.select_one("img.thumb") if not anchor: continue href = (anchor.get("href") or "").strip() match = re.search(r"/cn/(movie|tv)/\d+\.html", href) if not match: continue items.append( { "vod_id": href.replace("/cn/", "").lstrip("/"), "vod_name": ((image.get("alt") if image else "") or anchor.get_text(" ", strip=True)).strip(), "vod_pic": self._build_url((image.get("src") if image else "") or ""), "vod_remarks": (node.select_one("footer .rate") or node.select_one("footer") or "").get_text(" ", strip=True), } ) return items ``` - [ ] **Step 4: Run test to verify it passes** Run: `uv run python -m unittest py/tests/test_PPnix.py -v` Expected: PASS for the four tests above - [ ] **Step 5: Commit** ```bash git add py/PPnix.py py/tests/test_PPnix.py git commit -m "feat: add PPnix spider skeleton" ``` ### Task 2: Add failing tests for homepage, category, and search flows **Files:** - Modify: `py/tests/test_PPnix.py` - Modify: `py/PPnix.py` - Test: `py/tests/test_PPnix.py` - [ ] **Step 1: Write the failing tests** ```python from unittest.mock import patch @patch.object(Spider, "_request_html") def test_home_video_content_merges_movie_and_tv_cards(self, mock_request_html): mock_request_html.return_value = ''' ''' result = self.spider.homeVideoContent() self.assertEqual([item["vod_id"] for item in result["list"]], ["movie/101.html", "tv/201.html"]) @patch.object(Spider, "_request_html") def test_category_content_uses_expected_listing_url(self, mock_request_html): mock_request_html.return_value = ''' ''' result = self.spider.categoryContent("1", "2", False, {"class": "动作", "by": "score"}) self.assertEqual( mock_request_html.call_args.args[0], "https://www.ppnix.com/cn/movie/动作---1-rating.html", ) self.assertEqual(result["page"], 2) self.assertEqual(result["list"][0]["vod_id"], "movie/301.html") self.assertNotIn("pagecount", result) @patch.object(Spider, "_request_html") def test_search_content_parses_only_movie_and_tv_ids(self, mock_request_html): mock_request_html.return_value = ''' ''' result = self.spider.searchContent("搜索词", False, "1") self.assertEqual( mock_request_html.call_args.args[0], "https://www.ppnix.com/cn/search/%E6%90%9C%E7%B4%A2%E8%AF%8D--.html", ) self.assertEqual([item["vod_id"] for item in result["list"]], ["movie/401.html"]) self.assertNotIn("pagecount", result) ``` - [ ] **Step 2: Run test to verify it fails** Run: `uv run python -m unittest py/tests/test_PPnix.py -v` Expected: FAIL because `_request_html`, `homeVideoContent`, `categoryContent`, and `searchContent` are incomplete or missing - [ ] **Step 3: Write minimal implementation** ```python def _request_html(self, path_or_url, referer=None, extra_headers=None): target = path_or_url if str(path_or_url).startswith("http") else self._build_url(path_or_url) headers = dict(self.headers) headers["Referer"] = referer or self.headers["Referer"] if isinstance(extra_headers, dict): headers.update(extra_headers) response = self.fetch(target, headers=headers, timeout=10) if response.status_code != 200: return "" return response.text or "" def _dedupe(self, items): seen = set() result = [] for item in items: key = item.get("vod_id") if not key or key in seen: continue seen.add(key) result.append(item) return result def homeVideoContent(self): html = self._request_html(self.lang_path + "/") soup = BeautifulSoup(html or "", "html.parser") blocks = soup.select(".lists-content ul") items = [] if len(blocks) > 0: items.extend(self._parse_cards(str(blocks[0]))) if len(blocks) > 1: items.extend(self._parse_cards(str(blocks[1]))) return {"list": self._dedupe(items)[:20]} def categoryContent(self, tid, pg, filter, extend): page = max(int(pg), 1) items = self._parse_cards(self._request_html(self._build_category_url(tid, pg, extend or {}))) slug = self._map_type_slug(tid) items = [item for item in items if item["vod_id"].startswith(slug + "/")] return {"page": page, "limit": 24, "total": page * 24 + len(items), "list": items} def searchContent(self, key, quick, pg="1"): page = max(int(pg), 1) items = self._parse_cards(self._request_html(self._build_search_url(key, pg))) return {"page": page, "limit": 24, "total": page * 24 + len(items), "list": items[:10] if quick else items} ``` - [ ] **Step 4: Run test to verify it passes** Run: `uv run python -m unittest py/tests/test_PPnix.py -v` Expected: PASS for homepage, category, and search tests - [ ] **Step 5: Commit** ```bash git add py/PPnix.py py/tests/test_PPnix.py git commit -m "feat: add PPnix list and search parsing" ``` ### Task 3: Add failing tests for detail parsing and direct player URLs **Files:** - Modify: `py/tests/test_PPnix.py` - Modify: `py/PPnix.py` - Test: `py/tests/test_PPnix.py` - [ ] **Step 1: Write the failing tests** ```python def test_extract_m3u8_items_reads_infoid_and_episode_names(self): html = """ """ self.assertEqual( self.spider._extract_m3u8_items(html), {"info_id": "7788", "items": ["第1集", "第2集"]}, ) @patch.object(Spider, "_request_html") def test_detail_content_builds_ppnix_play_group(self, mock_request_html): mock_request_html.return_value = '''