File size: 25,996 Bytes
46a36c9
 
 
 
 
 
 
 
 
 
 
59755fa
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
1909027
46a36c9
 
 
59755fa
 
1909027
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59755fa
 
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
 
 
46a36c9
1909027
 
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
46a36c9
 
 
 
1909027
46a36c9
1909027
 
46a36c9
 
 
ec152af
46a36c9
 
 
 
 
 
1909027
 
 
ec152af
 
46a36c9
 
1909027
 
 
 
 
 
46a36c9
 
 
 
59755fa
46a36c9
 
 
1909027
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
 
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
59755fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59755fa
 
 
 
1909027
 
46a36c9
 
1909027
 
 
 
 
46a36c9
 
 
 
 
 
 
 
 
1909027
 
46a36c9
 
 
 
 
 
1909027
46a36c9
 
 
 
 
1909027
 
46a36c9
1909027
46a36c9
 
 
 
1909027
46a36c9
1909027
46a36c9
 
 
1909027
46a36c9
1909027
46a36c9
 
 
 
1909027
 
 
 
 
46a36c9
 
 
1909027
46a36c9
1909027
46a36c9
59755fa
1909027
46a36c9
1909027
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
 
 
46a36c9
 
 
 
 
 
 
 
 
 
 
 
59755fa
1909027
 
46a36c9
1909027
46a36c9
1909027
59755fa
 
46a36c9
 
 
 
 
 
1909027
46a36c9
1909027
59755fa
 
 
 
 
 
46a36c9
 
 
59755fa
46a36c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
59755fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1909027
 
 
 
 
 
46a36c9
 
 
 
 
59755fa
46a36c9
 
 
 
 
59755fa
46a36c9
1909027
46a36c9
 
59755fa
46a36c9
 
 
 
 
 
 
59755fa
46a36c9
 
 
59755fa
46a36c9
 
59755fa
46a36c9
 
59755fa
46a36c9
 
ec152af
59755fa
46a36c9
 
 
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#!/usr/bin/env python3
"""
Anna's Archives API - Hugging Face Space Edition
Optimized for HF Free Tier (CPU-only, minimal resources)
"""

import os
import re
import logging
from datetime import datetime, timedelta
from dataclasses import dataclass, asdict
from typing import Optional, Any, Literal
import html

from flask import Flask, jsonify, request, Response
from curl_cffi import requests
from bs4 import BeautifulSoup
import csv
import io

# ============================================================================
# CONFIGURATION
# ============================================================================

class Config:
    PORT = int(os.getenv("PORT", 7860))
    HOST = "0.0.0.0"
    MIRRORS_URL = "https://shadowlibraries.github.io/DirectDownloads/AnnasArchive/"
    DEFAULT_BASE_URL = "https://annas-archive.gs"
    # Welib est un miroir stable avec l'endpoint /popular — on le cible directement.
    WELIB_BASE_URL = "https://fr.welib.org"
    BROWSER_IMPERSONATE = "chrome110"
    CACHE_TTL_MINUTES = 10
    REQUEST_TIMEOUT = 20

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(levelname)s] %(message)s'
)
logger = logging.getLogger(__name__)

# ============================================================================
# SIMPLE CACHE
# ============================================================================

class SimpleCache:
    def __init__(self, ttl_minutes: int):
        self._cache = {}
        self._ttl = timedelta(minutes=ttl_minutes)
    
    def get(self, key: str) -> Optional[Any]:
        if key in self._cache:
            value, timestamp = self._cache[key]
            if datetime.now() - timestamp < self._ttl:
                return value
            del self._cache[key]
        return None
    
    def set(self, key: str, value: Any):
        if len(self._cache) > 100:
            oldest = min(self._cache.items(), key=lambda x: x[1][1])[0]
            del self._cache[oldest]
        self._cache[key] = (value, datetime.now())
    
    def clear(self):
        self._cache.clear()
    
    def size(self):
        return len(self._cache)

cache = SimpleCache(Config.CACHE_TTL_MINUTES)

# ============================================================================
# DATA MODELS
# ============================================================================

@dataclass
class Book:
    md5: Optional[str]
    title: str
    author: str
    publisher: str
    year: Optional[int]
    format: str
    language: str
    size_mb: float
    url: str
    cover_url: Optional[str] = None
    description: Optional[str] = None

    def to_dict(self):
        return asdict(self)

# ============================================================================
# UTILITIES
# ============================================================================

def clean_url(url: str) -> str:
    if not url:
        return ""
    from urllib.parse import urlparse, urlunparse
    parsed = urlparse(url)
    return urlunparse((parsed.scheme, parsed.netloc, parsed.path.rstrip('/'), '', '', ''))

def clean_text(text: str) -> str:
    if not text:
        return ""
    text = html.unescape(text)
    text = re.sub(r'[👤🏢📘🚀✅❌⭐]', '', text)
    text = re.sub(r'\s+', ' ', text)
    return text.strip()

def parse_size(size_str: str) -> float:
    if not size_str:
        return 0.0
    match = re.search(r'([\d.]+)\s*([KMGT]?B)', size_str, re.I)
    if not match:
        return 0.0
    num = float(match.group(1))
    unit = match.group(2).upper()
    multipliers = {'B': 1/1024/1024, 'KB': 1/1024, 'MB': 1, 'GB': 1024, 'TB': 1024*1024}
    return round(num * multipliers.get(unit, 1), 2)

# ============================================================================
# MIRROR MANAGER
# ============================================================================

class MirrorManager:
    _mirrors_cache: Optional[list] = None
    _current_mirror: Optional[str] = None

    def get_mirrors(self) -> list[dict]:
        if MirrorManager._mirrors_cache is not None:
            return MirrorManager._mirrors_cache
        logger.info("Fetching mirrors...")
        try:
            resp = requests.get(
                Config.MIRRORS_URL,
                impersonate=Config.BROWSER_IMPERSONATE,
                timeout=Config.REQUEST_TIMEOUT
            )
            soup = BeautifulSoup(resp.text, "html.parser")
            article = soup.find("article", class_="book-article")
            if not article:
                return []
            heading = article.find("h3", id="links")
            if not heading:
                return []
            ul = heading.find_next_sibling("ul")
            if not ul:
                return []
            mirrors = []
            for li in ul.find_all("li"):
                a = li.find("a", href=True)
                if a:
                    mirrors.append({
                        "label": a.get_text(strip=True),
                        "url": clean_url(a["href"])
                    })
            logger.info(f"Found {len(mirrors)} mirrors")
            MirrorManager._mirrors_cache = mirrors
            return mirrors
        except Exception as e:
            logger.error(f"Failed to fetch mirrors: {e}")
            return []

    def get_active_mirror(self) -> str:
        if MirrorManager._current_mirror:
            return MirrorManager._current_mirror
        mirrors = self.get_mirrors()
        for mirror in mirrors:
            try:
                logger.info(f"Testing mirror: {mirror['url']}")
                resp = requests.get(
                    mirror['url'],
                    impersonate=Config.BROWSER_IMPERSONATE,
                    timeout=10
                )
                if resp.status_code == 200:
                    MirrorManager._current_mirror = mirror['url']
                    logger.info(f"✅ Active mirror: {MirrorManager._current_mirror}")
                    return MirrorManager._current_mirror
            except Exception as e:
                logger.warning(f"Mirror {mirror['url']} failed: {e}")
                continue
        logger.warning("No active mirror, using default")
        MirrorManager._current_mirror = Config.DEFAULT_BASE_URL
        return MirrorManager._current_mirror

    def reset(self):
        MirrorManager._mirrors_cache = None
        MirrorManager._current_mirror = None

mirror_manager = MirrorManager()

# ============================================================================
# SCRAPER — SEARCH (Anna's Archive)
# ============================================================================

def scrape_search(query: str, page: int = 1, **filters) -> dict:
    cache_key = f"search_{query}_{page}_{sorted(filters.items())}"
    cached = cache.get(cache_key)
    if cached:
        logger.info(f"Cache HIT: {query} (page {page})")
        return cached
    
    logger.info(f"Scraping: {query} (page {page})")
    base_url = mirror_manager.get_active_mirror()
    search_url = f"{base_url}/search"
    params = {"q": query}
    if page > 1:
        params["page"] = page
    for key in ['lang', 'content', 'ext', 'sort']:
        if key in filters and filters[key]:
            params[key] = filters[key]
    
    try:
        resp = requests.get(
            search_url,
            params=params,
            impersonate=Config.BROWSER_IMPERSONATE,
            timeout=Config.REQUEST_TIMEOUT,
            headers={"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}
        )
        resp.raise_for_status()
        books = parse_books(resp.text, base_url)
        has_more = check_next_page(resp.text)
        result = {
            "books": [b.to_dict() for b in books],
            "total": len(books),
            "has_more": has_more,
            "timestamp": datetime.now().isoformat()
        }
        cache.set(cache_key, result)
        logger.info(f"Found {len(books)} books")
        return result
    except Exception as e:
        logger.error(f"Scraping error: {e}")
        return {"books": [], "total": 0, "has_more": False, "error": str(e)}


# ============================================================================
# SCRAPER — POPULAR (Welib /popular endpoint)
# ============================================================================

# Intervalles valides côté serveur welib
PopularInterval = Literal["24h", "week", "month", "random"]

def scrape_popular(interval: PopularInterval, offset: int = 0, limit: int = 10) -> dict:
    """
    Scrape GET /popular?interval={interval}&offset={offset}&limit={limit} sur fr.welib.org.
    Le HTML retourné est un fragment de liste de livres (pas une page complète).
    interval : "24h" | "week" | "month" | "random"
    """
    cache_key = f"popular_{interval}_{offset}_{limit}"
    # Pour "random" (surprenez-moi), le cache est volontairement court (1 min).
    ttl = 1 if interval == "random" else Config.CACHE_TTL_MINUTES
    cached = cache.get(cache_key)
    if cached and interval != "random":
        logger.info(f"Cache HIT: popular/{interval}")
        return cached

    logger.info(f"Fetching popular books: interval={interval}, offset={offset}, limit={limit}")
    url = f"{Config.WELIB_BASE_URL}/popular"
    params = {"interval": interval, "offset": offset, "limit": limit}

    try:
        resp = requests.get(
            url,
            params=params,
            impersonate=Config.BROWSER_IMPERSONATE,
            timeout=Config.REQUEST_TIMEOUT,
            headers={
                "Accept": "*/*",
                "Referer": f"{Config.WELIB_BASE_URL}/",
                "Accept-Language": "fr,fr-FR;q=0.9,en-US;q=0.8,en;q=0.7",
            }
        )
        resp.raise_for_status()
        books = parse_welib_books(resp.text)
        result = {
            "interval": interval,
            "offset": offset,
            "limit": limit,
            "books": [b.to_dict() for b in books],
            "total": len(books),
            "timestamp": datetime.now().isoformat()
        }
        cache.set(cache_key, result)
        logger.info(f"Got {len(books)} popular books ({interval})")
        return result
    except Exception as e:
        logger.error(f"Popular scraping error: {e}")
        return {"interval": interval, "books": [], "total": 0, "error": str(e)}


def parse_welib_books(html_text: str) -> list[Book]:
    """
    Parse le fragment HTML retourné par /popular sur fr.welib.org.
    Structure des cartes :
      .book-card
        img[data-author][data-title][src]   → cover, author, title
        a[href=/md5/...]                     → md5, url
        h2.font-semibold                     → title (fallback)
        a[href=/search?q=...]               → author
        p.text-gray-600                     → description
        div.mb-1 > span (×4)               → format · langue · année · taille
    """
    soup = BeautifulSoup(html_text, "html.parser")
    books = []
    seen_md5s: set[str] = set()

    for card in soup.find_all("div", class_="book-card"):
        try:
            # — MD5 & URL —
            md5 = None
            url = ""
            anchor = card.find("a", href=re.compile(r'/md5/'))
            if anchor:
                href = anchor.get("href", "")
                md5_match = re.search(r'/md5/([a-f0-9]{32})', href)
                if md5_match:
                    md5 = md5_match.group(1)
                url = f"{Config.WELIB_BASE_URL}{href}" if href.startswith('/') else href

            if md5:
                if md5 in seen_md5s:
                    continue
                seen_md5s.add(md5)

            # — Titre —
            title = ""
            img = card.find("img", attrs={"data-title": True})
            if img:
                title = clean_text(img["data-title"])
            if not title:
                h2 = card.find("h2", class_=lambda x: x and "font-semibold" in x)
                if h2:
                    title = clean_text(h2.get_text())
            if not title:
                continue  # carte invalide

            # — Auteur —
            author = "Unknown"
            if img and img.get("data-author"):
                author = clean_text(img["data-author"])
            else:
                author_link = card.find("a", href=re.compile(r'search\?q='))
                if author_link:
                    author = clean_text(author_link.get_text())

            # — Cover URL —
            cover_url = None
            if img:
                src = img.get("src", "")
                cover_url = src if src else None

            # — Description —
            description = None
            desc_p = card.find("p", class_=lambda x: x and "text-gray-600" in x)
            if desc_p:
                # Exclure le bouton "Lire plus…"
                for btn in desc_p.find_all("button"):
                    btn.decompose()
                description = clean_text(desc_p.get_text()) or None

            # — Métadonnées (format · langue · année · taille) —
            # Dans le HTML welib, ces 4 infos sont dans des <span> inside div.mb-1
            fmt = "UNKNOWN"
            language = "xx"
            year = None
            size_mb = 0.0

            meta_div = card.find("div", class_="mb-1")
            if meta_div:
                spans = [clean_text(s.get_text()) for s in meta_div.find_all("span")]
                # spans typiques : ["PDF", "· français", "· 2017", "· 13.6 MB"]
                # On nettoie les "· " en tête et on parse chaque span
                for span in spans:
                    span = re.sub(r'^[·\s]+', '', span).strip()
                    if not span:
                        continue
                    if re.match(r'^\d{4}$', span):
                        year = int(span)
                    elif re.search(r'[\d.]+\s*[KMGT]?B', span, re.I):
                        size_mb = parse_size(span)
                    elif re.match(r'^[A-Z0-9]{2,6}$', span):
                        fmt = span
                    else:
                        # langue : peut être "français", "english", "deutsch", etc.
                        language = span

            books.append(Book(
                md5=md5,
                title=title,
                author=author,
                publisher="Unknown",   # pas exposé dans ce fragment HTML
                year=year,
                format=fmt,
                language=language,
                size_mb=size_mb,
                url=url,
                cover_url=cover_url,
                description=description,
            ))
        except Exception as e:
            logger.warning(f"Error parsing welib book card: {e}")
            continue

    return books


# ============================================================================
# SCRAPER — RECENT DOWNLOADS (Anna's Archive /dyn/recent_downloads/)
# ============================================================================

def scrape_recent_downloads() -> dict:
    """
    Endpoint /dyn/recent_downloads/ — retourne les 50 derniers téléchargements globaux.
    Requiert Accept: text/css (trick anti-DDoS Guard, même pattern que /dyn/search_counts).
    Réponse : JSON array de {path, title}, cachée 60s côté serveur.
    """
    cache_key = "recent_downloads"
    cached = cache.get(cache_key)
    if cached:
        logger.info("Cache HIT: recent_downloads")
        return cached

    logger.info("Fetching recent downloads...")
    base_url = mirror_manager.get_active_mirror()
    url = f"{base_url}/dyn/recent_downloads/"

    try:
        resp = requests.get(
            url,
            impersonate=Config.BROWSER_IMPERSONATE,
            timeout=Config.REQUEST_TIMEOUT,
            headers={"Accept": "text/css"}  # Requis — bypass DDoS Guard cache
        )
        resp.raise_for_status()
        items = resp.json()
        enriched = []
        for item in items:
            md5_match = re.search(r'/md5/([a-f0-9]{32})', item.get("path", ""))
            enriched.append({
                "md5": md5_match.group(1) if md5_match else None,
                "title": item.get("title", ""),
                "path": item.get("path", ""),
                "url": f"{base_url}{item['path']}" if item.get("path") else None
            })
        result = {
            "items": enriched,
            "total": len(enriched),
            "timestamp": datetime.now().isoformat()
        }
        cache.set(cache_key, result)
        logger.info(f"Got {len(enriched)} recent downloads")
        return result
    except Exception as e:
        logger.error(f"Recent downloads error: {e}")
        return {"items": [], "total": 0, "error": str(e)}


# ============================================================================
# SCRAPER — SEARCH PARSER (Anna's Archive)
# ============================================================================

def parse_books(html_text: str, base_url: str) -> list[Book]:
    soup = BeautifulSoup(html_text, 'html.parser')
    books = []
    seen_md5s = set()

    blocks = soup.find_all(
        'div',
        class_=lambda x: x and 'flex' in x and 'pt-3' in x and 'pb-3' in x
    )
    
    for block in blocks:
        try:
            md5 = None
            md5_div = block.find('div', class_='hidden')
            if md5_div:
                match = re.search(r'md5:([a-f0-9]{32})', md5_div.text)
                if match:
                    md5 = match.group(1)

            title_link = block.find('a', class_=lambda x: x and 'js-vim-focus' in x and 'font-semibold' in x)
            if not title_link:
                continue
            title = clean_text(title_link.text)
            url = title_link.get('href', '')
            if url.startswith('/'):
                url = f"{base_url}{url}"

            if not md5:
                match = re.search(r'/md5/([a-f0-9]{32})', url)
                if match:
                    md5 = match.group(1)
            if md5:
                if md5 in seen_md5s:
                    continue
                seen_md5s.add(md5)

            cover_img = block.find('img')
            cover_url = None
            if cover_img:
                cover_url = cover_img.get('src', '')
                if cover_url and cover_url.startswith('/'):
                    cover_url = f"{base_url}{cover_url}"

            author = "Unknown"
            for link in block.find_all('a', href=re.compile(r'search\?q=')):
                if 'user-edit' in str(link):
                    author = clean_text(link.get_text())
                    break

            publisher = "Unknown"
            year = None
            for link in block.find_all('a', href=re.compile(r'search\?q=')):
                if 'company' in str(link):
                    pub_text = clean_text(link.get_text())
                    year_match_pub = re.search(r'(\d{4})$', pub_text)
                    if year_match_pub:
                        year = int(year_match_pub.group(1))
                        publisher = re.sub(r',\s*(?:\w+\s+\d+,\s*)?\d{4}$', '', pub_text).strip()
                    else:
                        publisher = pub_text
                    break

            info_div = block.find('div', class_=re.compile(r'text-gray-800'))
            info_text = info_div.get_text() if info_div else ""
            format_match = re.search(r'·\s*([A-Z0-9]+)\s*·', info_text)
            lang_match = re.search(r'\[([a-z]{2,4})\]', info_text)
            size_match = re.search(r'([\d.]+\s*[KMGT]?B)', info_text)
            year_match = re.search(r'·\s*(\d{4})\s*·', info_text)

            book = Book(
                md5=md5,
                title=title,
                author=author,
                publisher=publisher,
                year=year or (int(year_match.group(1)) if year_match else None),
                format=format_match.group(1) if format_match else "UNKNOWN",
                language=lang_match.group(1) if lang_match else "xx",
                size_mb=parse_size(size_match.group(1)) if size_match else 0.0,
                url=url,
                cover_url=cover_url
            )
            books.append(book)
        except Exception as e:
            logger.warning(f"Error parsing book: {e}")
            continue
    return books


def check_next_page(html_text: str) -> bool:
    soup = BeautifulSoup(html_text, 'html.parser')
    return soup.find('a', string=re.compile(r'Next|→|»')) is not None

# ============================================================================
# FLASK APP
# ============================================================================

app = Flask(__name__)

@app.route('/')
def index():
    return jsonify({
        "name": "Anna's Archives API",
        "version": "1.2.0",
        "description": "HF Space Edition - Free Tier Optimized",
        "browser": Config.BROWSER_IMPERSONATE,
        "endpoints": {
            "GET /": "Documentation",
            "GET /search": "Search books",
            "GET /recent": "Recent global downloads (live feed)",
            "GET /popular": "Popular books by interval (24h / week / month)",
            "GET /surprise": "Random book selection (surprenez-moi)",
            "GET /health": "Health check",
            "GET /mirrors": "List mirrors",
            "POST /cache/clear": "Clear cache"
        },
        "examples": {
            "search": "/search?q=python",
            "filters": "/search?q=machine+learning&ext=pdf&lang=en",
            "pagination": "/search?q=python&page=2",
            "csv": "/search?q=python&format=csv",
            "recent": "/recent",
            "popular_day": "/popular?interval=24h",
            "popular_week": "/popular?interval=week",
            "popular_month": "/popular?interval=month",
            "popular_paged": "/popular?interval=week&offset=10&limit=10",
            "surprise": "/surprise"
        }
    })


@app.route('/search')
def search():
    query = request.args.get('q', '').strip()
    if not query:
        return jsonify({"error": "Parameter 'q' is required"}), 400
    try:
        page = max(1, int(request.args.get('page', 1)))
    except ValueError:
        return jsonify({"error": "Invalid page number"}), 400
    
    filters = {
        'lang': request.args.get('lang'),
        'ext': request.args.get('ext'),
        'content': request.args.get('content'),
        'sort': request.args.get('sort')
    }
    result = scrape_search(query, page, **filters)
    
    if request.args.get('format') == 'csv':
        output = io.StringIO()
        if result['books']:
            writer = csv.DictWriter(output, fieldnames=result['books'][0].keys())
            writer.writeheader()
            writer.writerows(result['books'])
        return Response(
            output.getvalue(),
            mimetype='text/csv',
            headers={'Content-Disposition': f'attachment; filename=search_{query}.csv'}
        )
    
    return jsonify({
        "query": query,
        "page": page,
        **result,
        "filters": filters
    })


@app.route('/popular')
def popular():
    """
    Livres populaires par période.
    Paramètres :
      interval : "24h" | "week" | "month"  (défaut : "week")
      offset   : int  (défaut : 0)
      limit    : int  (défaut : 10, max : 50)
    Source : fr.welib.org/popular
    """
    interval = request.args.get('interval', 'week').lower()
    valid_intervals = {"24h", "week", "month"}
    if interval not in valid_intervals:
        return jsonify({
            "error": f"Invalid interval '{interval}'. Must be one of: {', '.join(sorted(valid_intervals))}"
        }), 400

    try:
        offset = max(0, int(request.args.get('offset', 0)))
        limit = min(50, max(1, int(request.args.get('limit', 10))))
    except ValueError:
        return jsonify({"error": "Invalid offset or limit"}), 400

    result = scrape_popular(interval, offset, limit)
    return jsonify(result)


@app.route('/surprise')
def surprise():
    """
    Sélection aléatoire de livres — "Surprenez-moi".
    Paramètres :
      limit : int  (défaut : 10, max : 50)
    Source : fr.welib.org/popular?interval=random
    Le résultat N'EST PAS mis en cache (chaque appel retourne une sélection fraîche).
    """
    try:
        limit = min(50, max(1, int(request.args.get('limit', 10))))
    except ValueError:
        return jsonify({"error": "Invalid limit"}), 400

    result = scrape_popular("random", offset=0, limit=limit)
    # On rebaptise l'interval pour l'utilisateur
    result["interval"] = "random"
    result["description"] = "Random book selection — surprenez-moi!"
    return jsonify(result)


@app.route('/recent')
def recent_downloads():
    result = scrape_recent_downloads()
    return jsonify(result)


@app.route('/health')
def health():
    try:
        mirror = mirror_manager.get_active_mirror()
        status = "healthy"
    except Exception:
        mirror = "unavailable"
        status = "degraded"
    return jsonify({
        "status": status,
        "mirror": mirror,
        "welib": Config.WELIB_BASE_URL,
        "cache_size": cache.size(),
        "browser": Config.BROWSER_IMPERSONATE
    })


@app.route('/mirrors')
def mirrors():
    return jsonify({
        "mirrors": mirror_manager.get_mirrors(),
        "current": mirror_manager.get_active_mirror()
    })


@app.route('/cache/clear', methods=['POST'])
def clear_cache():
    cache.clear()
    mirror_manager.reset()
    return jsonify({"message": "Cache cleared", "size": 0})


if __name__ == "__main__":
    logger.info("=" * 70)
    logger.info("🚀 Anna's Archives API - HF Space Edition v1.2.0")
    logger.info("=" * 70)
    logger.info(f"Port: {Config.PORT}")
    logger.info(f"Browser: {Config.BROWSER_IMPERSONATE}")
    logger.info(f"Popular source: {Config.WELIB_BASE_URL}")
    logger.info("=" * 70)
    mirror_manager.get_active_mirror()
    app.run(host=Config.HOST, port=Config.PORT)