# -*- coding: utf-8 -*-
import sys
import re
import json
import urllib.request
import urllib.parse
import gzip
import io

sys.path.append('..')
from base.spider import Spider


class Spider(Spider):
    """污漫天堂漫画爬虫 - 皮卡丘标准格式"""

    def getName(self):
        return "污漫漫画"

    def init(self, extend=""):
        pass

    def isVideoFormat(self, url):
        return False

    def manualVideoCheck(self):
        return False

    def destroy(self):
        pass

    def getHeader(self):
        return {
            "User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36",
            "Referer": "https://wmtt5.com/",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Accept-Encoding": "gzip, deflate",  # 关键：移除br，只接受gzip
            "Connection": "keep-alive",
            "Upgrade-Insecure-Requests": "1",
            "Cookie": "age_verify=1; popup_agreement=1"
        }

    def fetch(self, url, method='GET', data=None, headers=None):
        """统一请求方法 - 仅使用标准库"""
        try:
            h = headers if headers else self.getHeader()
            
            # 处理URL编码（保留已编码的 %XX，避免双重编码导致 404）
            parsed = urllib.parse.urlparse(url)
            encoded_path = urllib.parse.quote(parsed.path, safe='/%')
            url = urllib.parse.urlunparse((
                parsed.scheme,
                parsed.netloc,
                encoded_path,
                parsed.params,
                parsed.query,
                parsed.fragment
            ))
            
            # 构建请求
            req = urllib.request.Request(url, method=method.upper())
            
            # 添加headers
            for key, value in h.items():
                req.add_header(key, value)
            
            # POST数据
            if method.upper() == 'POST' and data:
                if isinstance(data, dict):
                    data = urllib.parse.urlencode(data).encode('utf-8')
                req.data = data
            
            # 发送请求
            with urllib.request.urlopen(req, timeout=15) as response:
                content = response.read()
                content_encoding = response.info().get('Content-Encoding', '').lower()
                
                # 处理gzip压缩
                if 'gzip' in content_encoding:
                    try:
                        content = gzip.decompress(content)
                    except Exception as e:
                        try:
                            buf = io.BytesIO(content)
                            with gzip.GzipFile(fileobj=buf) as f:
                                content = f.read()
                        except Exception as e2:
                            pass
                
                # 解码
                result = content.decode('utf-8', errors='ignore')
                return result
        except Exception as e:
            print(f"[ERROR] 请求失败: {url}, 错误: {e}")
            return None

    def homeContent(self, filter):
        """首页分类"""
        result = {
            "class": [
                {"type_id": "newmanga", "type_name": "最近更新"},
                {"type_id": "mangacata/all/ob/hit/st/all", "type_name": "全部漫画"},
                {"type_id": "mangacata/%E9%9F%A9%E6%BC%AB/ob/time/st/all", "type_name": "韩漫"},
                {"type_id": "mangacata/%E6%97%A5%E6%BC%AB/ob/time/st/all", "type_name": "日漫"},
                {"type_id": "mangacata/%E7%9C%9F%E4%BA%BA%E6%BC%AB%E7%94%BB/ob/time/st/all", "type_name": "真人漫画"},
                {"type_id": "mangacata/%E7%9F%AD%E7%AF%87/ob/time/st/all", "type_name": "短篇"},
                {"type_id": "mangarank/daily", "type_name": "日榜"},
                {"type_id": "mangarank", "type_name": "周榜"},
                {"type_id": "mangarank/monthly", "type_name": "月榜"},
                {"type_id": "mangarank/all", "type_name": "总榜"}
            ]
        }
        try:
            result["list"] = self.categoryContent("newmanga", "1", None, None)["list"]
        except Exception:
            result["list"] = []
        return result

    def homeVideoContent(self):
        """首页推荐内容"""
        return self.categoryContent("newmanga", "1", False, None)

    def categoryContent(self, tid, pg, filter, extend):
        """分类内容"""
        try:
            # 构建URL
            if tid == "newmanga":
                url = f"https://wmtt5.com/newmanga"
                if int(pg) > 1:
                    url += f"?page={pg}"
            elif "mangarank" in tid:
                url = f"https://wmtt5.com/{tid}"
                if int(pg) > 1:
                    url += f"?page={pg}"
            else:
                url = f"https://wmtt5.com/{tid}/page/{pg}"
            
            html = self.fetch(url)
            if not html:
                return {"list": []}
            
            vlist = self.parseList(html)
            
            return {
                "list": vlist,
                "page": pg,
                "pagecount": 9999,
                "limit": 20,
                "total": 999999
            }
        except Exception as e:
            print(f"[ERROR] categoryContent error: {e}")
            return {"list": []}

    def searchContent(self, key, quick, pg="1"):
        """搜索内容"""
        try:
            search_key = urllib.parse.quote(key)
            url = f"https://wmtt5.com/cata.php?key={search_key}"
            if int(pg) > 1:
                url += f"&page={pg}"
            
            html = self.fetch(url)
            if not html:
                return {"list": []}
            
            vlist = self.parseList(html)
            
            return {
                "list": vlist,
                "page": pg,
                "pagecount": 9999,
                "limit": 20,
                "total": 999999
            }
        except Exception as e:
            print(f"[ERROR] searchContent error: {e}")
            return {"list": []}

    def detailContent(self, ids):
        """详情内容"""
        try:
            vid = ids[0]
            html = self.fetch(vid)
            if not html:
                return {"list": []}
            
            # 提取标题
            name = "未知漫画"
            name_match = re.search(r'<h1[^>]*class=["\'][^"\']*module-info-title[^"\']*["\'][^>]*>(.*?)</h1>', html, re.S)
            if name_match:
                name = self.cleanHtml(name_match.group(1))
            
            if name == "未知漫画":
                name_match = re.search(r'<h1[^>]*>(.*?)</h1>', html, re.S)
                if name_match:
                    name = self.cleanHtml(name_match.group(1))
            name = name.strip().strip('《》').strip()
            
            # 提取作者
            author = ""
            author_match = re.search(r'<div[^>]*class=["\'][^"\']*module-info-item-content[^"\']*["\'][^>]*>(.*?)</div>', html, re.S)
            if author_match:
                author = self.cleanHtml(author_match.group(1))
                author = re.sub(r'\s+', ' ', author).strip(' /').strip()
            
            # 提取标签
            tags = re.findall(r'<div[^>]*class=["\'][^"\']*module-info-tag-link[^"\']*["\'][^>]*>(.*?)</div>', html, re.S)
            tag_list = [self.cleanHtml(t).strip() for t in tags]
            tag_list = [t for t in tag_list if t and t not in ('/', '|', ',')]
            tag = " / ".join(tag_list)
            tag = re.sub(r'\s+', ' ', tag).strip(' /').strip()
            
            # 提取简介
            desc = ""
            desc_match = re.search(r'<div[^>]*class=["\'][^"\']*module-info-introduction-content[^"\']*show-desc[^"\']*["\'][^>]*>(.*?)</div>', html, re.S)
            if not desc_match:
                desc_match = re.search(r'<div[^>]*class=["\'][^"\']*module-info-introduction-content[^"\']*["\'][^>]*>(.*?)</div>', html, re.S)
            if desc_match:
                desc = self.cleanHtml(desc_match.group(1))
                desc = re.sub(r'\s+', ' ', desc).strip()
            
            # 提取封面（优先取 thumb.niaopic.com 的封面图，避开 new.niaopic.com 等坏链）
            pic = ""
            for u in re.findall(r'data-original=["\']([^"\']+)["\']', html):
                if 'thumb.niaopic.com' in u and 'error.png' not in u and 'logo' not in u:
                    pic = self.cover(u)
                    break
            if not pic:
                pic_match = re.search(r'<meta[^>]*property=["\']og:image["\'][^>]*content=["\']([^"\']+)["\']', html)
                if pic_match:
                    pic = self.cover(pic_match.group(1))
            
            # 提取章节列表
            chapters = []
            chapter_items = re.findall(r'<a[^>]*class=["\'][^"\']*module-play-list-link[^"\']*["\'][^>]*href=["\']([^"\']+)["\'][^>]*title=["\']([^"\']+)["\']', html, re.S)
            if not chapter_items:
                chapter_items = re.findall(r'<a[^>]*href=["\'](/mangaread/\d+\.html)["\'][^>]*title=["\']([^"\']+)["\']', html)
            
            for ch_url, ch_name in chapter_items:
                ch_url = self.fixUrl(ch_url)
                chapters.append(f"{ch_name}${ch_url}")
            
            play_url = "#".join(chapters) if chapters else ""
            
            return {
                "list": [{
                    "vod_id": vid,
                    "vod_name": name,
                    "vod_pic": pic,
                    "type_name": tag,
                    "vod_actor": author,
                    "vod_content": desc,
                    "vod_play_from": "污漫天堂",
                    "vod_play_url": play_url
                }]
            }
        except Exception as e:
            print(f"[ERROR] detailContent error: {e}")
            import traceback
            traceback.print_exc()
            return {"list": []}

    def playerContent(self, flag, id, vipFlags):
        """播放内容 - 提取图片"""
        try:
            url = id if id.startswith("http") else f"https://wmtt5.com{id}"
            
            html = self.fetch(url)
            if not html:
                return {"parse": 1, "url": url, "header": self.getHeader()}
            
            img_list = []
            
            # 匹配data-original
            imgs = re.findall(r'<img[^>]*data-original=["\']([^"\']+)["\']', html)
            if imgs:
                for src in imgs:
                    if any(ext in src.lower() for ext in ['.jpg', '.jpeg', '.png', '.webp', '.gif']):
                        img_list.append(self.fixUrl(src))
            
            # 备用：匹配src
            if not img_list:
                imgs = re.findall(r'<img[^>]*src=["\']([^"\']+)["\']', html)
                for src in imgs:
                    if any(ext in src.lower() for ext in ['.jpg', '.jpeg', '.png', '.webp', '.gif']):
                        if 'error.png' not in src and 'logo' not in src:
                            img_list.append(self.fixUrl(src))
            
            # 去重
            seen = set()
            unique_imgs = []
            for img in img_list:
                if img not in seen:
                    seen.add(img)
                    unique_imgs.append(img)
            
            if unique_imgs:
                return {
                    "parse": 0,
                    "playUrl": "",
                    "url": f"pics://{'&&'.join(unique_imgs)}",
                    "header": ""
                }
            else:
                return {"parse": 1, "url": url, "header": self.getHeader()}
        except Exception as e:
            print(f"[ERROR] playerContent error: {e}")
            return {"parse": 1, "url": id, "header": self.getHeader()}

    def localProxy(self, param):
        pass

    # ============ 工具方法 ============

    def parseList(self, html):
        """解析列表页"""
        vlist = []
        try:
            if not html or len(html) < 100:
                return vlist
            
            # 顺序无关解析：先定位每张卡片起始 <a> 标签，卡片内容取到下一张卡片开始处，
            # href/title 直接取自闭包开标签，封面取卡片块内首个 data-original，
            # 避免卡片内嵌 <a> 导致 </a> 提前闭合、截断封面
            starts = [(m.start(), m.group(0))
                      for m in re.finditer(r'<a[^>]*class=["\'][^"\']*module-poster-item[^"\']*["\'][^>]*>', html, re.I)]
            for i, (pos, tag) in enumerate(starts):
                try:
                    end = starts[i + 1][0] if i + 1 < len(starts) else len(html)
                    chunk = html[pos:end]

                    href_m = re.search(r'href=["\']([^"\']+)["\']', tag)
                    if not href_m:
                        continue
                    title_m = re.search(r'title=["\']([^"\']+)["\']', tag)
                    name = title_m.group(1).strip() if title_m else ""
                    link = self.fixUrl(href_m.group(1))

                    pic = ""
                    pic_m = re.search(r'data-original=["\']([^"\']+)["\']', chunk)
                    if not pic_m:
                        pic_m = re.search(r'<img[^>]*src=["\']([^"\']+)["\']', chunk)
                    if pic_m:
                        pic = self.cover(pic_m.group(1))

                    note = ""
                    note_m = re.search(r'class=["\'][^"\']*module-item-note[^"\']*["\'][^>]*>(.*?)</div>', chunk, re.S)
                    if note_m:
                        note = self.cleanHtml(note_m.group(1))

                    vlist.append({
                        "vod_id": link,
                        "vod_name": name,
                        "vod_pic": pic,
                        "vod_remarks": note
                    })
                except Exception:
                    continue

            # 兜底：直接匹配 /mangaread/ 链接
            if not vlist:
                links = re.findall(r'<a[^>]*href=["\'](/mangaread/[^"\']+\.html)["\'][^>]*title=["\']([^"\']+)["\']', html, re.S)
                for href, name in links:
                    vlist.append({
                        "vod_id": self.fixUrl(href),
                        "vod_name": name.strip(),
                        "vod_pic": "",
                        "vod_remarks": ""
                    })

        except Exception as e:
            print(f"[ERROR] parseList error: {e}")

        return vlist

    def cleanHtml(self, text):
        """清除HTML标签"""
        if not text:
            return ""
        text = re.sub(r'<[^>]+>', '', text)
        text = text.replace('&nbsp;', ' ').replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>')
        text = text.replace('&quot;', '"').replace('&#39;', "'")
        return text.strip()

    def fixUrl(self, url):
        """补全URL"""
        if not url:
            return ""
        if url.startswith("http"):
            return url
        if url.startswith("//"):
            return "https:" + url
        return "https://wmtt5.com" + url

    def cover(self, url):
        """封面图统一转 jpg：原站封面为 webp，部分播放器封面加载器不支持 webp，
        同一图片路径服务器同时提供 .jpg 版本，转 jpg 可保证各分支正常显示"""
        if not url:
            return ""
        u = self.fixUrl(url)
        if u.lower().endswith(".webp"):
            u = u[:-5] + ".jpg"
        return u


# ==================== 测试代码 ====================
if __name__ == '__main__':
    spider = Spider()
    
    print("=== 测试首页分类 ===")
    home = spider.homeContent(filter=True)
    print(f"分类数量: {len(home['class'])}")
    
    print("\n=== 测试首页内容 ===")
    home_video = spider.homeVideoContent()
    print(f"获取到 {len(home_video['list'])} 条数据")
    if home_video['list']:
        print("第一条:", home_video['list'][0])
        print("最后一条:", home_video['list'][-1])
    
    print("\n=== 测试分类内容 ===")
    cat = spider.categoryContent("mangacata/%E9%9F%A9%E6%BC%AB/ob/time/st/all", "1", False, None)
    print(f"获取到 {len(cat['list'])} 条数据")
    
    print("\n=== 测试搜索 ===")
    search = spider.searchContent("测试", False, "1")
    print(f"搜索结果: {len(search['list'])} 条")
