# 针对 Python 3.12+ 移除 distutils 的兼容性补丁
import sys
try:
    import distutils
except ImportError:
    from types import ModuleType
    d, v = ModuleType("distutils"), ModuleType("distutils.version")
    d.version = v
    sys.modules.update({"distutils": d, "distutils.version": v})
    class LooseVersion:
        def __init__(self, v): self.v = v
        def __lt__(self, o): return True
        def __str__(self): return str(self.v)
    v.LooseVersion = LooseVersion

import time, random, re, os, subprocess, urllib.parse, json, traceback
import undetected_chromedriver as uc 
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium_stealth import stealth

class Scraper1688:
    def __init__(self, headless=True):
        self.headless = headless
        self.user_data_path = os.path.abspath(os.path.join(os.getcwd(), "1688_user_data"))
        self._cleanup()
        options = uc.ChromeOptions()
        options.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36')
        options.add_argument(f"--user-data-dir={self.user_data_path}")
        if headless: options.add_argument('--headless=new')
        options.add_argument('--disable-blink-features=AutomationControlled')
        options.add_argument("--window-size=1920,1080")
        try:
            self.driver = uc.Chrome(options=options, headless=headless, version_main=137)
        except:
            self.driver = uc.Chrome(options=options, headless=headless)
        stealth(self.driver, languages=["zh-CN", "zh"], vendor="Google Inc.", platform="Win32", fix_hairline=True)

    def _cleanup(self):
        if os.name == 'nt': subprocess.call(['taskkill', '/F', '/IM', 'chrome.exe', '/T'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        if os.path.exists(self.user_data_path):
            for root, _, files in os.walk(self.user_data_path):
                for f in files:
                    if "lock" in f.lower() or f == "SingletonLock":
                        try: os.remove(os.path.join(root, f))
                        except: pass

    def clean_url(self, url):
        """极其鲁棒的 1688 URL 清洗逻辑"""
        if not url: return ""
        if url.startswith("//"): url = "https:" + url
        
        # 1. 尝试从路径中匹配 offer ID (标准 PC 链接)
        id_match = re.search(r'offer/(\d+)\.html', url)
        if id_match: return f"https://detail.1688.com/offer/{id_match.group(1)}.html"
        
        # 2. 尝试从查询参数中提取 offerId (移动端或广告链接)
        parsed = urllib.parse.urlparse(url)
        params = urllib.parse.parse_qs(parsed.query)
        oid = params.get('offerId') or params.get('id')
        if oid: return f"https://detail.1688.com/offer/{oid[0]}.html"
        
        # 3. 针对某些特殊加密链接，尝试寻找 data-aplus-report 或类似字符串中的 ID
        id_match_report = re.search(r'object_id@(\d+)', url)
        if id_match_report: return f"https://detail.1688.com/offer/{id_match_report.group(1)}.html"
        
        return url

    def check_for_captcha(self):
        def is_blocked():
            try:
                src, url, title = self.driver.page_source.lower(), self.driver.current_url.lower(), self.driver.title.lower()
                sliders = self.driver.find_elements(By.ID, "nc_1_n1z")
                is_slider = len(sliders) > 0 and sliders[0].is_displayed()
                return is_slider or "punish" in url or "哎哟喂" in src or "验证码" in title
            except: return False
        
        if is_blocked():
            print("\n[!] 触发拦截，请手动完成验证...")
            while is_blocked(): time.sleep(2)
            print("[+] 验证通过！")
            time.sleep(3)
        return True

    def search_products_yield(self, keyword, total_count=200):
        gbk_keyword = urllib.parse.quote(keyword, encoding='gbk')
        base_url = f"https://s.1688.com/selloffer/offer_search.htm?keywords={gbk_keyword}&n=y&netType=1%2C11%2C16"
        
        all_links = set()
        page = 1
        consecutive_empty_pages = 0
        
        while len(all_links) < total_count and consecutive_empty_pages < 3:
            print(f"[*] 正在抓取第 {page} 页...")
            target_url = f"{base_url}&beginPage={page}&page={page}"
            self.driver.get(target_url)
            
            # 关键：首屏强制等待渲染
            time.sleep(10)
            self.check_for_captcha()

            # 深度滚动
            for i in range(1, 6):
                self.driver.execute_script(f"window.scrollTo(0, document.body.scrollHeight * {i/5});")
                time.sleep(1.2)

            page_results = self._extract_all_methods()
            
            new_batch = []
            for it in page_results:
                it["link"] = self.clean_url(it["link"])
                if it["link"] and it["link"] not in all_links:
                    all_links.add(it["link"])
                    new_batch.append(it)
            
            if new_batch:
                consecutive_empty_pages = 0
                yield new_batch
            else:
                print(f"[-] 第 {page} 页未发现新数据，尝试刷新重试...")
                self.driver.refresh()
                time.sleep(8)
                retry_results = self._extract_all_methods()
                new_retry = []
                for it in retry_results:
                    it["link"] = self.clean_url(it["link"])
                    if it["link"] and it["link"] not in all_links:
                        all_links.add(it["link"]); new_retry.append(it)
                
                if new_retry:
                    yield new_retry
                else:
                    consecutive_empty_pages += 1
                    print(f"[!] 连续 {consecutive_empty_pages} 页无数据")

            page += 1
            if len(all_links) < total_count:
                print(f"[*] 累计抓取: {len(all_links)} 条，准备翻页...")
                time.sleep(5)

        return list(all_links)

    def _extract_all_methods(self):
        """三位一体提取法：JSON + DOM + 深度搜索"""
        results = []
        
        # 1. JSON 提取 (window.data 或 window.__INITIAL_DATA__)
        try:
            res = self.driver.execute_script("return JSON.stringify(window.data || window.__INITIAL_DATA__)")
            if res:
                data = json.loads(res)
                def find_list(obj):
                    if isinstance(obj, list) and len(obj) > 0:
                        if 'title' in obj[0] or 'offerId' in obj[0]: return obj
                    if isinstance(obj, dict):
                        for k in obj:
                            found = find_list(obj[k])
                            if found: return found
                    return None
                raw = find_list(data) or []
                for o in raw:
                    title = str(o.get('title', o.get('name', ''))).replace('<em>','').replace('</em>','')
                    link = o.get('itemUrl', o.get('url', ''))
                    price = o.get('priceInfo', {}).get('price', o.get('price', '面议'))
                    if link: results.append({"name": title, "link": link, "price": price})
        except: pass

        # 2. 增强版 DOM 扫描
        if not results:
            # 包含最新的选择器
            selectors = [".search-offer-item", "[class*='offer-card']", ".offer-item", ".major-offer"]
            for s in selectors:
                cards = self.driver.find_elements(By.CSS_SELECTOR, s)
                if len(cards) > 3:
                    for el in cards:
                        try:
                            # 1. 链接提取：自身或子孙节点
                            link = ""
                            if el.tag_name == 'a':
                                link = el.get_attribute("href")
                            else:
                                a_tags = el.find_elements(By.TAG_NAME, "a")
                                for a in a_tags:
                                    h = a.get_attribute("href")
                                    if h and ("offer" in h or "item" in h or "ci_bb" in h):
                                        link = h; break
                            
                            # 2. ID 补丁
                            if not link or "1688.com" not in link:
                                oid = el.get_attribute("data-offer-id") or el.get_attribute("data-id")
                                if oid: link = f"https://detail.1688.com/offer/{oid}.html"
                            
                            if link:
                                # 3. 标题和价格提取
                                title = el.text.split('\n')[0][:50]
                                price = "面议"
                                try:
                                    price_el = el.find_element(By.CSS_SELECTOR, ".text-main, [class*='price'], .amount")
                                    price = price_el.text.strip().replace("¥", "")
                                except: pass
                                results.append({"name": title, "link": link, "price": price})
                        except: continue
                    if results: break # 成功一次就不再尝试其他选择器

        # 3. 最后的保底：正则源码提取 (极其暴力)
        if not results:
            ids = re.findall(r'data-offer-id="(\d+)"', self.driver.page_source)
            for oid in set(ids):
                results.append({"name": f"1688商品-{oid}", "link": f"https://detail.1688.com/offer/{oid}.html", "price": "面议"})
                
        return results

    def quit(self):
        try: self.driver.quit()
        except: pass
