File size: 10,635 Bytes
18b6a95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
Core scraping engine with async downloading, retry logic, and chapter management.
"""
from __future__ import annotations

import asyncio
import hashlib
import json
import logging
import random
import re
import time
from dataclasses import dataclass, field, asdict
from enum import Enum
from pathlib import Path
from typing import AsyncIterator, Callable, Optional
from urllib.parse import urljoin, urlparse

import aiohttp
import aiofiles

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Data models
# ---------------------------------------------------------------------------

class ChapterStatus(str, Enum):
    PENDING = "pending"
    DOWNLOADING = "downloading"
    DONE = "done"
    FAILED = "failed"
    SKIPPED = "skipped"
    LOCKED = "locked"


@dataclass
class Chapter:
    index: int
    title: str
    url: str
    status: ChapterStatus = ChapterStatus.PENDING
    retries: int = 0
    error: str = ""
    content: str = ""
    word_count: int = 0

    def to_dict(self) -> dict:
        d = asdict(self)
        d["status"] = self.status.value
        return d


@dataclass
class NovelJob:
    url: str
    title: str = ""
    chapters: list[Chapter] = field(default_factory=list)
    status: str = "queued"          # queued | running | paused | done | cancelled
    start_chapter: int = 1
    end_chapter: int = 0            # 0 = all
    specific_chapters: list[int] = field(default_factory=list)
    output_dir: Path = field(default_factory=lambda: Path("output"))
    created_at: float = field(default_factory=time.time)
    started_at: float = 0.0
    finished_at: float = 0.0
    bytes_downloaded: int = 0

    # Progress helpers
    @property
    def total(self) -> int:
        return len(self.chapters)

    @property
    def done_count(self) -> int:
        return sum(1 for c in self.chapters if c.status == ChapterStatus.DONE)

    @property
    def failed_count(self) -> int:
        return sum(1 for c in self.chapters if c.status == ChapterStatus.FAILED)

    @property
    def progress(self) -> float:
        if not self.total:
            return 0.0
        return self.done_count / self.total

    def chapter_in_range(self, ch: Chapter) -> bool:
        if self.specific_chapters:
            return ch.index in self.specific_chapters
        end = self.end_chapter if self.end_chapter else float("inf")
        return self.start_chapter <= ch.index <= end


# ---------------------------------------------------------------------------
# User-agent pool
# ---------------------------------------------------------------------------

_USER_AGENTS = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 14.4; rv:124.0) Gecko/20100101 Firefox/124.0",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
]


def random_ua() -> str:
    return random.choice(_USER_AGENTS)


def build_headers(extra: dict | None = None) -> dict:
    h = {
        "User-Agent": random_ua(),
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language": "en-US,en;q=0.9",
        "Accept-Encoding": "gzip, deflate, br",
        "Connection": "keep-alive",
        "Upgrade-Insecure-Requests": "1",
    }
    if extra:
        h.update(extra)
    return h


# ---------------------------------------------------------------------------
# Session factory
# ---------------------------------------------------------------------------

async def create_session(
    cookies: dict | None = None,
    timeout_seconds: int = 30,
) -> aiohttp.ClientSession:
    connector = aiohttp.TCPConnector(
        limit=20,
        limit_per_host=5,
        ssl=False,
        force_close=False,
        enable_cleanup_closed=True,
    )
    timeout = aiohttp.ClientTimeout(total=timeout_seconds, connect=10)
    session = aiohttp.ClientSession(
        connector=connector,
        timeout=timeout,
        headers=build_headers(),
        cookies=cookies or {},
    )
    return session


# ---------------------------------------------------------------------------
# Fetch helpers
# ---------------------------------------------------------------------------

_CF_SIGNALS = [
    "checking your browser",
    "cloudflare",
    "ddos-guard",
    "ray id",
    "enable javascript",
    "__cf_bm",
    "cf-challenge",
    "403 forbidden",
    "access denied",
    "bot detection",
    "please wait",
    "browser check",
]

def _is_cloudflare(html: str, status: int) -> bool:
    if status in (403, 503, 429):
        return True
    low = html.lower()
    return any(sig in low for sig in _CF_SIGNALS)

def _is_login_page(html: str) -> bool:
    low = html.lower()
    return any(k in low for k in ["please log in", "sign in to continue", "login required", "members only"])

def _is_locked_chapter(html: str) -> bool:
    low = html.lower()
    return any(k in low for k in ["this chapter is locked", "premium chapter", "unlock chapter", "coins required", "vip chapter"])


async def fetch_html(
    session: aiohttp.ClientSession,
    url: str,
    retries: int = 3,
    base_delay: float = 2.0,
    extra_headers: dict | None = None,
) -> tuple[str, int]:
    """Fetch URL, return (html, status_code). Raises on permanent failure."""
    headers = build_headers(extra_headers)
    for attempt in range(retries):
        try:
            async with session.get(url, headers=headers, allow_redirects=True) as resp:
                # Try utf-8 first, fallback to detected encoding
                raw = await resp.read()
                try:
                    html = raw.decode("utf-8")
                except UnicodeDecodeError:
                    html = raw.decode("latin-1", errors="replace")
                return html, resp.status
        except (aiohttp.ClientError, asyncio.TimeoutError) as exc:
            if attempt == retries - 1:
                raise
            delay = base_delay * (2 ** attempt) + random.uniform(0, 1)
            logger.debug(f"Fetch attempt {attempt+1} failed for {url}: {exc}. Retry in {delay:.1f}s")
            await asyncio.sleep(delay)
    return "", 0


# ---------------------------------------------------------------------------
# Cloudscraper fallback
# ---------------------------------------------------------------------------

def _cloudscraper_fetch(url: str, cookies: dict | None = None) -> tuple[str, int]:
    try:
        import cloudscraper
        scraper = cloudscraper.create_scraper(browser={"browser": "chrome", "platform": "windows", "mobile": False})
        if cookies:
            scraper.cookies.update(cookies)
        resp = scraper.get(url, timeout=30)
        return resp.text, resp.status_code
    except ImportError:
        raise RuntimeError("cloudscraper not installed")
    except Exception as exc:
        raise RuntimeError(f"cloudscraper failed: {exc}")


# ---------------------------------------------------------------------------
# Playwright fallback
# ---------------------------------------------------------------------------

async def _playwright_fetch(url: str, wait_seconds: float = 3.0) -> tuple[str, int]:
    try:
        from playwright.async_api import async_playwright
        async with async_playwright() as pw:
            browser = await pw.chromium.launch(
                headless=True,
                args=["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage",
                      "--disable-gpu", "--no-first-run", "--no-zygote", "--single-process"],
            )
            ctx = await browser.new_context(user_agent=random_ua())
            page = await ctx.new_page()
            response = await page.goto(url, wait_until="networkidle", timeout=30000)
            await asyncio.sleep(wait_seconds)
            html = await page.content()
            status = response.status if response else 200
            await browser.close()
            return html, status
    except ImportError:
        raise RuntimeError("playwright not installed")
    except Exception as exc:
        raise RuntimeError(f"playwright failed: {exc}")


# ---------------------------------------------------------------------------
# Smart fetch with fallback chain
# ---------------------------------------------------------------------------

class FetchMode(str, Enum):
    FAST = "fast"
    BALANCED = "balanced"
    COMPAT = "compat"


async def smart_fetch(
    session: aiohttp.ClientSession,
    url: str,
    mode: FetchMode = FetchMode.BALANCED,
    cookies: dict | None = None,
    log_cb: Callable[[str], None] | None = None,
) -> tuple[str, int]:
    """Try aiohttp → cloudscraper → playwright depending on mode and CF detection."""

    def log(msg: str):
        if log_cb:
            log_cb(msg)
        logger.info(msg)

    # 1. Normal aiohttp
    try:
        html, status = await fetch_html(session, url)
        if _is_cloudflare(html, status):
            log(f"⚠️  Cloudflare detected on {url}")
            if mode == FetchMode.FAST:
                return html, status
        else:
            return html, status
    except Exception as exc:
        log(f"⚠️  aiohttp failed for {url}: {exc}")
        if mode == FetchMode.FAST:
            raise

    # 2. Cloudscraper
    if mode in (FetchMode.BALANCED, FetchMode.COMPAT):
        try:
            log(f"🔄 Trying cloudscraper for {url}")
            loop = asyncio.get_event_loop()
            html, status = await loop.run_in_executor(None, _cloudscraper_fetch, url, cookies)
            if not _is_cloudflare(html, status):
                return html, status
            log(f"⚠️  Cloudscraper still blocked on {url}")
        except Exception as exc:
            log(f"⚠️  Cloudscraper failed: {exc}")

    # 3. Playwright
    if mode == FetchMode.COMPAT:
        log(f"🌐 Activating Playwright browser mode for {url}")
        try:
            html, status = await _playwright_fetch(url)
            return html, status
        except Exception as exc:
            log(f"❌ Playwright failed: {exc}")
            raise

    raise RuntimeError(f"All fetch methods failed for {url}")