File size: 47,471 Bytes
d23966c
 
 
 
 
 
652f463
d23966c
fd61b28
e4035e1
d23966c
e4035e1
d23966c
582b7af
adbf374
fd61b28
 
 
167f7e3
 
 
fd61b28
 
 
d23966c
e4035e1
9978d0c
d23966c
 
 
 
 
652f463
 
 
fd61b28
d23966c
 
 
 
 
fd61b28
d23966c
fd61b28
652f463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d23966c
 
 
 
 
 
 
652f463
 
 
 
d23966c
 
 
 
 
652f463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d23966c
 
e4035e1
fd61b28
 
 
d23966c
 
 
652f463
d23966c
 
 
 
652f463
d23966c
fd61b28
e4035e1
fd61b28
e4035e1
 
fd61b28
 
064365d
 
 
 
fd61b28
064365d
 
 
 
 
 
 
 
 
 
 
fd61b28
 
 
064365d
e4035e1
 
167f7e3
e4035e1
 
fd61b28
e4035e1
167f7e3
6c68921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4035e1
 
fd61b28
 
 
e4035e1
fd61b28
 
 
e4035e1
fd61b28
 
 
 
 
 
 
e4035e1
b1c0895
fd61b28
 
064365d
 
fd61b28
064365d
 
 
 
 
 
 
 
 
 
 
 
fd61b28
e4035e1
fd61b28
e4035e1
fd61b28
9978d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adbf374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd61b28
 
167f7e3
fd61b28
 
167f7e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4035e1
167f7e3
 
 
fd61b28
e4035e1
fd61b28
 
582b7af
 
 
fd61b28
 
582b7af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e09c0bd
 
 
 
 
 
 
 
 
582b7af
 
 
 
 
 
 
 
e4035e1
e09c0bd
fd61b28
e09c0bd
 
 
 
 
 
 
 
 
 
fd61b28
e09c0bd
fd61b28
 
 
582b7af
fd61b28
e4035e1
d23966c
 
e4035e1
d23966c
 
 
 
 
 
 
 
652f463
 
 
fd61b28
 
 
 
 
 
e4035e1
fd61b28
e4035e1
fd61b28
 
 
 
d23966c
fd61b28
d23966c
 
 
 
 
 
652f463
 
 
fd61b28
e4035e1
 
 
d23966c
fd61b28
 
d23966c
 
e4035e1
 
 
fd61b28
 
d23966c
 
 
fd61b28
e4035e1
 
d23966c
fd61b28
 
d23966c
fd61b28
 
 
 
d23966c
 
652f463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd61b28
2782b0e
 
 
fd61b28
 
e4035e1
 
 
fd61b28
e4035e1
652f463
 
 
e4035e1
fd61b28
167f7e3
652f463
e4035e1
167f7e3
 
e4035e1
 
167f7e3
fd61b28
e4035e1
 
fd61b28
e4035e1
 
fd61b28
e4035e1
fd61b28
e4035e1
fd61b28
e4035e1
 
fd61b28
 
 
 
 
652f463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4035e1
652f463
 
 
2782b0e
652f463
 
 
 
 
 
 
 
e4035e1
fd61b28
2782b0e
fd61b28
2782b0e
fd61b28
e4035e1
fd61b28
 
e4035e1
fd61b28
 
e4035e1
 
 
652f463
 
167f7e3
d23966c
9978d0c
 
 
064365d
 
 
 
 
9978d0c
 
 
652f463
 
9978d0c
 
 
 
 
 
 
 
 
 
 
 
 
064365d
9978d0c
064365d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
652f463
 
 
 
 
 
064365d
652f463
064365d
 
652f463
 
 
 
 
 
 
 
 
 
 
064365d
 
fd61b28
 
 
666f2fb
2782b0e
fd61b28
 
 
666f2fb
2782b0e
fd61b28
 
 
 
 
d23966c
 
fd61b28
d23966c
 
fd61b28
d23966c
 
fd61b28
 
d23966c
fd61b28
 
d23966c
fd61b28
 
d23966c
 
fd61b28
d23966c
e4035e1
 
fd61b28
 
e4035e1
fd61b28
d23966c
fd61b28
 
 
d23966c
 
652f463
 
d23966c
 
fd61b28
 
d23966c
 
e4035e1
 
fd61b28
d23966c
fd61b28
e4035e1
 
 
 
fd61b28
 
e4035e1
fd61b28
 
 
e4035e1
652f463
 
 
 
 
 
 
 
 
 
d23966c
e4035e1
652f463
 
 
 
 
 
 
fd61b28
652f463
e4035e1
652f463
 
e4035e1
652f463
 
 
 
 
e4035e1
fd61b28
e4035e1
fd61b28
 
 
 
 
 
652f463
 
e4035e1
fd61b28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d23966c
064365d
 
 
 
652f463
064365d
652f463
 
064365d
 
 
 
9978d0c
 
 
 
064365d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9978d0c
064365d
 
 
 
 
 
 
 
 
 
 
 
 
adbf374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d23966c
 
fd61b28
d23966c
 
652f463
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
import gradio as gr
import numpy as np
import cv2
from pathlib import Path
import zipfile
import tempfile
import shutil
import os
import re
import time
import requests
from PIL import Image
from io import BytesIO
import base64
import urllib.parse
try:
    import undetected_chromedriver as uc
    from selenium.webdriver.common.by import By
    from pyvirtualdisplay import Display
    import ssl
    ssl._create_default_https_context = ssl._create_unverified_context
    SCRAPER_AVAILABLE = True
except ImportError:
    SCRAPER_AVAILABLE = False

from manhwa_extractor import ManhwaProcessor
from bs4 import BeautifulSoup

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

IMAGE_SUFFIXES = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif"}
NATURAL_TOKEN_RE = re.compile(r"(\d+)")

def _common_prefix_name(names: list) -> str:
    if not names:
        return "manhwa_batch"
    if len(names) == 1:
        return names[0]
    prefix = os.path.commonprefix(names).rstrip("_- ")
    return prefix if len(prefix) >= 3 else f"{names[0]}_{names[-1]}_batch"


def _natural_key(value) -> list:
    text = str(value).lower()
    return [int(token) if token.isdigit() else token for token in NATURAL_TOKEN_RE.split(text)]


def _is_image_path(path: Path) -> bool:
    return path.suffix.lower() in IMAGE_SUFFIXES and not any(part.startswith(".") for part in path.parts)


def _resolve_gradio_path(file_obj):
    if file_obj is None:
        return None
    return Path(file_obj if isinstance(file_obj, str) else getattr(file_obj, "name", str(file_obj)))


def _sanitize_folder_name(name: str, fallback: str) -> str:
    cleaned = re.sub(r"[^\w.\- /]+", "_", (name or "").strip()).strip(" ./")
    return cleaned or fallback


def _build_settings(min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels):
    return dict(
        min_panel_area=min_panel_area,
        edge_sigma=edge_sigma,
        low_threshold=low_threshold,
        high_threshold=high_threshold,
        reading_order=reading_order,
        refine_panels=refine_panels,
    )


def _extract_panels_for_image(image_path: Path, settings: dict):
    processor = ManhwaProcessor(
        min_panel_area_ratio=settings["min_panel_area"] / 100,
        edge_detection_sigma=settings["edge_sigma"],
        low_threshold=settings["low_threshold"],
        high_threshold=settings["high_threshold"],
        reading_order=settings["reading_order"],
    )
    loaded = processor.load_image(str(image_path))
    if loaded is None:
        raise ValueError(f"Could not load {image_path.name}")

    segmentation = processor.detect_edges()
    processor.identify_panels(segmentation)
    processor.extract_panels()
    processor.order_panels()

    panels = []
    for panel_data in processor.ordered_panels:
        panel = panel_data["panel"]
        if settings["refine_panels"]:
            panel = processor.refine_panel(panel)[0]
        panels.append(panel)
    return panels


def _zip_directory_to_file(root_dir: Path, zip_name: str) -> str:
    root_dir.mkdir(parents=True, exist_ok=True)
    zip_path = Path(tempfile.gettempdir()) / f"{zip_name}.zip"
    with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as zf:
        for file_path in sorted(root_dir.rglob("*"), key=lambda p: _natural_key(p.relative_to(root_dir).as_posix())):
            if file_path.is_file():
                zf.write(file_path, arcname=str(file_path.relative_to(root_dir)))
    return str(zip_path)


def _process_chapter_sequence(image_paths: list[Path], chapter_rel_path: Path, settings: dict, output_root: Path):
    chapter_dir = output_root / chapter_rel_path
    chapter_dir.mkdir(parents=True, exist_ok=True)

    saved_paths = []
    issues = []
    panel_index = 1

    for image_path in sorted(image_paths, key=lambda p: _natural_key(p.name)):
        try:
            panels = _extract_panels_for_image(image_path, settings)
        except Exception as exc:
            issues.append(f"{chapter_rel_path.as_posix()}/{image_path.name} (error: {exc})")
            continue

        if not panels:
            issues.append(f"{chapter_rel_path.as_posix()}/{image_path.name} (no panels)")
            continue

        for panel in panels:
            out_path = chapter_dir / f"panel_{panel_index:04d}.png"
            cv2.imwrite(str(out_path), cv2.cvtColor(panel, cv2.COLOR_RGB2BGR))
            saved_paths.append(out_path)
            panel_index += 1

    return saved_paths, issues


def _extract_zip_groups(zip_path: Path, extract_root: Path):
    groups = {}
    with zipfile.ZipFile(zip_path) as zf:
        for info in sorted(zf.infolist(), key=lambda item: _natural_key(item.filename)):
            if info.is_dir():
                continue

            rel_name = info.filename.replace("\\", "/")
            rel_path = Path(*[part for part in Path(rel_name).parts if part not in {"", "."}])
            if not rel_path.parts or ".." in rel_path.parts or not _is_image_path(rel_path):
                continue

            dest = extract_root / rel_path
            dest.parent.mkdir(parents=True, exist_ok=True)
            with zf.open(info) as src, open(dest, "wb") as dst:
                shutil.copyfileobj(src, dst)

            chapter_key = rel_path.parent if rel_path.parent != Path(".") else Path(zip_path.stem)
            groups.setdefault(chapter_key, []).append(dest)

    return sorted(groups.items(), key=lambda item: _natural_key(item[0].as_posix()))


def _process_single(image_path: Path, settings: dict, temp_dir: Path):
    panels = _extract_panels_for_image(image_path, settings)
    if not panels:
        return 0, []

    parent_name = image_path.parent.name
    out_dir = (temp_dir / parent_name / image_path.stem
               if parent_name.lower().startswith("chapter")
               else temp_dir / image_path.stem)
    out_dir.mkdir(parents=True, exist_ok=True)

    panel_paths = []
    for panel_id, panel_to_save in enumerate(panels, start=1):
        out_path = out_dir / f"panel_{panel_id:02d}.png"
        cv2.imwrite(str(out_path), cv2.cvtColor(panel_to_save, cv2.COLOR_RGB2BGR))
        panel_paths.append(out_path)

    return len(panels), panel_paths


# ---------------------------------------------------------------------------
# Scraper Logic — site-agnostic
# ---------------------------------------------------------------------------

def _chap_num(url: str) -> float:
    """Extract a sortable chapter number from any URL."""
    url_l = url.lower().rstrip('/')
    
    # 1. Look for explicit chapter keywords 
    m = re.search(r'(?:chapter|ch|episode|ep|scan)[-_]?(\d+(?:\.\d+)?)', url_l)
    if m:
        return float(m.group(1))
        
    # 2. Look backwards for the last number block
    parts = url_l.split('/')
    for part in reversed(parts):
        nums = re.findall(r'\b(\d+(?:\.\d+)?)\b', part)
        if nums:
            return float(nums[-1])
            
    # 3. Fallback: last contiguous string of digits
    digits = re.findall(r'\d+', url_l)
    return float(digits[-1]) if digits else 0.0



def setup_uc_driver():
    options = uc.ChromeOptions()
    # No options.headless! We want it to be fully headed within the Virtual Display
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-gpu")
    options.add_argument("--window-size=1920,1080")
    options.add_argument("--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
    
    # In Hugging Face Spaces, chromium is usually installed via packages.txt
    # and ends up in /usr/bin/chromium or similar.
    possible_bins = ["/usr/bin/chromium", "/usr/bin/google-chrome", "/usr/bin/chromium-browser"]
    for bin_path in possible_bins:
        if os.path.exists(bin_path):
            options.binary_location = bin_path
            break
            
    kwargs = {"options": options}
    
    # Also specify the chromedriver path if it exists
    possible_drivers = ["/usr/bin/chromedriver", "/usr/local/bin/chromedriver"]
    for driver_path in possible_drivers:
        if os.path.exists(driver_path):
            kwargs["driver_executable_path"] = driver_path
            break
            
    # Force version 146 as fallback if auto-download triggers
    # since Debian currently has chromium 146.
    kwargs["version_main"] = 146

    return uc.Chrome(**kwargs)


def _scroll_to_bottom(driver, pause=1.5, max_scrolls=15):
    last = driver.execute_script("return document.body.scrollHeight")
    for _ in range(max_scrolls):
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(pause)
        new = driver.execute_script("return document.body.scrollHeight")
        if new == last:
            break
        last = new


def get_chapter_links(driver, series_url: str) -> list:
    driver.get(series_url)
    time.sleep(6)  # Let Cloudflare clear
    _scroll_to_bottom(driver)

    chapter_links = []
    for link in driver.find_elements(By.TAG_NAME, "a"):
        href = link.get_attribute("href") or ""
        href_l = href.lower()
        if not href or "#" in href or "login" in href_l or "register" in href_l:
            continue
            
        href_clean = href_l.rstrip('/')
        is_chapter = False
        
        if re.search(r'[-/](chapter|ch|episode|ep|scan)[-_]?\d+', href_clean):
            is_chapter = True
        elif re.search(r'/[a-z0-9-]+-chapter-\d+', href_clean):
            is_chapter = True
        elif re.search(r'/\d+(?:\.\d+)?$', href_clean):
            is_chapter = True
            
        if is_chapter:
            chapter_links.append(href)

    return sorted(set(chapter_links), key=_chap_num, reverse=True)


def parse_images_from_html(html_source: str, output_dir: str) -> list:
    soup = BeautifulSoup(html_source, 'html.parser')
    badKeywords = ['logo', 'avatar', 'icon', 'banner', 'button', 'ads', 'sprite', 'thumbnail', 'discord']
    
    img_urls = []
    for img in soup.find_all('img'):
        src = ""
        for name, val in img.attrs.items():
            name_l = name.lower()
            if isinstance(val, list):
                val = val[0]
            val_str = str(val).strip()
            if ('src' in name_l or 'url' in name_l) and val_str.startswith('http'):
                if name_l.startswith('data-'):
                    src = val_str
                    break
                if not src:
                    src = val_str
                    
        if src:
            src_l = src.lower()
            if len(src_l) > 5 and not src_l.startswith("data:") and not any(bad in src_l for bad in badKeywords):
                if src not in img_urls:
                    img_urls.append(src)
                    
    os.makedirs(output_dir, exist_ok=True)
    paths = []
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
    for i, url in enumerate(img_urls):
        try:
            r = requests.get(url, headers=headers, stream=True, timeout=10)
            if r.status_code == 200:
                ext = url.split("?")[0].split(".")[-1]
                if len(ext) > 5 or not ext: ext = "jpg"
                p = Path(output_dir) / f"{i:03d}.{ext}"
                with open(p, 'wb') as f:
                    for chunk in r.iter_content(1024):
                        f.write(chunk)
                paths.append(p)
        except Exception:
            pass
    return paths


# ---------------------------------------------------------------------------
# API Hub Integrations (MangaDex & Comick)
# ---------------------------------------------------------------------------

def search_manga_api(source, query):
    if not query.strip(): return []
    results = []
    if source == "MangaDex":
        url = f"https://api.mangadex.org/manga?title={urllib.parse.quote(query)}&limit=15&includes[]=cover_art"
        try:
            r = requests.get(url, timeout=10).json()
            for manga in r.get("data", []):
                title = manga["attributes"]["title"].get("en", "Unknown Title")
                m_id = manga["id"]
                results.append((title, m_id))
        except: pass
    elif source == "Comick":
        url = f"https://api.comick.fun/v1.0/search?q={urllib.parse.quote(query)}&limit=15"
        try:
            r = requests.get(url, timeout=10).json()
            for manga in r:
                title = manga.get("title", "Unknown")
                hid = manga.get("hid")
                if hid: results.append((title, hid))
        except: pass
    return results

def get_chapters_api(source, manga_id):
    if not manga_id: return []
    chapters = []
    if source == "MangaDex":
        url = f"https://api.mangadex.org/manga/{manga_id}/feed?limit=500&translatedLanguage[]=en&order[chapter]=desc"
        try:
            r = requests.get(url, timeout=10).json()
            for ch in r.get("data", []):
                num = ch["attributes"].get("chapter")
                tit = ch["attributes"].get("title")
                cid = ch["id"]
                label = f"Chapter {num}" if num else "Oneshot"
                if tit: label += f" - {tit}"
                chapters.append((label, cid))
        except: pass
    elif source == "Comick":
        url = f"https://api.comick.fun/comic/{manga_id}/chapters?lang=en&limit=500"
        try:
            r = requests.get(url, timeout=10).json()
            for ch in r.get("chapters", []):
                num = ch.get("chap")
                tit = ch.get("title")
                cid = ch.get("hid")
                label = f"Chapter {num}" if num else "Oneshot"
                if tit: label += f" - {tit}"
                chapters.append((label, cid))
        except: pass
    return chapters

def download_api_chapter(source, chapter_id, output_dir):
    os.makedirs(output_dir, exist_ok=True)
    paths = []
    if source == "MangaDex":
        url = f"https://api.mangadex.org/at-home/server/{chapter_id}"
        try:
            r = requests.get(url, timeout=10).json()
            base = r.get("baseUrl")
            h = r.get("chapter", {}).get("hash")
            data = r.get("chapter", {}).get("data", [])
            for i, filename in enumerate(data):
                img_url = f"{base}/data/{h}/{filename}"
                p = Path(output_dir) / f"{i:03d}.jpg"
                img_r = requests.get(img_url, stream=True, timeout=10)
                with open(p, 'wb') as f:
                    for chunk in img_r.iter_content(1024): f.write(chunk)
                paths.append(p)
        except: pass
    elif source == "Comick":
        url = f"https://api.comick.fun/chapter/{chapter_id}"
        try:
            r = requests.get(url, timeout=10).json()
            images = r.get("chapter", {}).get("images", [])
            for i, img in enumerate(images):
                img_url = img.get("url")
                if img_url:
                    p = Path(output_dir) / f"{i:03d}.jpg"
                    img_r = requests.get(img_url, stream=True, timeout=10)
                    with open(p, 'wb') as f:
                        for chunk in img_r.iter_content(1024): f.write(chunk)
                    paths.append(p)
        except: pass
    return paths

def hub_search(query, source):
    res = search_manga_api(source, query)
    choices = [f"{t} ({i})" for t, i in res]
    return gr.update(choices=choices, value=choices[0] if choices else None), res

def hub_get_chapters(selected_str, res_state, source):
    if not selected_str: return gr.update(choices=[]), []
    manga_id = None
    for t, i in res_state:
        if f"{t} ({i})" == selected_str:
            manga_id = i
            break
    chaps = get_chapters_api(source, manga_id)
    choices = [f"{t} [{i}]" for t, i in chaps]
    return gr.update(choices=choices, value=choices[0] if choices else None), chaps

def hub_download_chapter(selected_chap_str, chaps_state, source):
    if not selected_chap_str: raise gr.Error("Select a chapter first")
    chap_id = None
    for t, i in chaps_state:
        if f"{t} [{i}]" == selected_chap_str:
            chap_id = i
            break
            
    td = Path(tempfile.mkdtemp()) / "hub_chapter"
    td.mkdir(parents=True, exist_ok=True)
    imgs = download_api_chapter(source, chap_id, str(td))
    if not imgs: raise gr.Error("Failed to fetch chapter images from API.")
    
    pil_images = [Image.open(p) for p in imgs]
    choices = [p.name for p in imgs]
    state_paths = {p.name: str(p) for p in imgs}
    return pil_images, gr.update(choices=choices, value=choices, visible=True), state_paths, f"✅ Fetched {len(imgs)} pages via {source} API."

def download_chapter_images(driver, chapter_url: str, output_dir: str) -> list:
    driver.get(chapter_url)
    time.sleep(14)
    _scroll_to_bottom(driver)

    js_images = driver.execute_script("""
        const imgs = Array.from(document.querySelectorAll('img'));
        const badKeywords = ['logo', 'avatar', 'icon', 'banner', 'button', 'ads', 'sprite', 'thumbnail', 'discord'];
        return imgs.map(img => {
            let src = img.src || "";
            for (let attr of img.attributes) {
                let name = attr.name.toLowerCase();
                let val = attr.value;
                if ((name.includes('src') || name.includes('url')) && val.startsWith('http')) {
                    if (name.startsWith('data-')) return val;
                    src = val;
                }
            }
            return src;
        }).filter(s => {
            if (!s) return false;
            let s_lower = s.toLowerCase();
            if (s_lower.length < 5 || s_lower.startsWith("data:")) return false;
            for (let bad of badKeywords) {
                if (s_lower.includes(bad)) return false;
            }
            return true;
        });
    """)

    img_urls = []
    for url in (js_images or []):
        if url not in img_urls:
            img_urls.append(url)

    os.makedirs(output_dir, exist_ok=True)
    paths = []
    headers = {"Referer": chapter_url, "User-Agent": "Mozilla/5.0"}
    
    driver.set_script_timeout(15)
    
    for i, url in enumerate(img_urls):
        out = os.path.join(output_dir, f"{i+1:03d}.jpg")
        
        # 1. Try fetching via the authenticated browser context to bypass Cloudflare
        try:
            script = """
                var url = arguments[0];
                var done = arguments[arguments.length - 1];
                fetch(url)
                    .then(response => response.blob())
                    .then(blob => {
                        var reader = new FileReader();
                        reader.onloadend = function() { done(reader.result); }
                        reader.readAsDataURL(blob);
                    })
                    .catch(err => done(null));
            """
            b64_data = driver.execute_async_script(script, url)
            if b64_data and "," in b64_data:
                img_data = base64.b64decode(b64_data.split(',')[1])
                
                # Check dimensions to filter out thumbnails/icons
                try:
                    img_pil = Image.open(BytesIO(img_data))
                    if img_pil.width < 400 and img_pil.height < 400:
                        continue
                except Exception:
                    pass
                    
                with open(out, "wb") as f:
                    f.write(img_data)
                paths.append(out)
                continue
        except Exception:
            pass
            
        # 2. Fallback to requests
        try:
            r = requests.get(url, timeout=15, headers=headers)
            r.raise_for_status()
            img_data = r.content
            
            # Check dimensions to filter out thumbnails/icons
            try:
                img_pil = Image.open(BytesIO(img_data))
                if img_pil.width < 400 and img_pil.height < 400:
                    continue
            except Exception:
                pass
                
            with open(out, "wb") as f:
                f.write(img_data)
            paths.append(out)
        except Exception:
            pass
            
    return [Path(p) for p in paths]


# ---------------------------------------------------------------------------
# Processor Wrappers
# ---------------------------------------------------------------------------

def process_manhwa_image(input_image, min_panel_area, edge_sigma, low_threshold,
                         high_threshold, reading_order, refine_panels):
    if input_image is None:
        return None, "Please upload an image first", []
    try:
        img_array = np.array(input_image)
        settings = _build_settings(
            min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels
        )
        with tempfile.TemporaryDirectory() as tmp:
            td = Path(tmp)
            inp = td / "input.png"
            cv2.imwrite(str(inp), cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR))
            n, paths = _process_single(inp, settings, td)
            if n == 0:
                return None, "No panels detected.", []
            imgs = [Image.open(p) for p in paths]
            pzip = Path(tempfile.gettempdir()) / f"manhwa_{id(input_image)}.zip"
            with zipfile.ZipFile(pzip, "w") as zf:
                for p in paths:
                    zf.write(p, p.name)
            return str(pzip), f"✅ Extracted {n} panel(s)" + (" (refined)" if refine_panels else ""), imgs
    except Exception as e:
        return None, f"❌ Error: {e}", []


def batch_process_images(files, min_panel_area, edge_sigma, low_threshold,
                         high_threshold, reading_order, refine_panels):
    if not files:
        return None, "⚠️ Please upload at least one image."
    settings = _build_settings(
        min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels
    )
    file_paths = sorted([Path(f if isinstance(f, str) else getattr(f, "name", str(f))) for f in files],
                        key=lambda p: p.stem.lower())
    zip_name = _common_prefix_name([p.stem for p in file_paths])
    tot_panels, tot_imgs, failed = 0, 0, []

    with tempfile.TemporaryDirectory() as tmp:
        td = Path(tmp)
        for img_path in file_paths:
            try:
                n, _ = _process_single(img_path, settings, td)
                tot_panels += n
                tot_imgs += 1
                if n == 0:
                    failed.append(f"{img_path.name} (no panels)")
            except Exception as e:
                failed.append(f"{img_path.name} (error: {e})")

        pzip = Path(tempfile.gettempdir()) / f"{zip_name}.zip"
        with zipfile.ZipFile(pzip, "w", compression=zipfile.ZIP_DEFLATED) as zf:
            for folder in td.iterdir():
                if folder.is_dir():
                    for pf in sorted(folder.iterdir()):
                        zf.write(pf, f"{folder.name}/{pf.name}")

    msgs = [f"✅ Processed **{tot_imgs}** images", f"📦 Extracted **{tot_panels}** panels"]
    if failed:
        msgs.append("⚠️ Issues:\n" + "\n".join(f"  • {f}" for f in failed))
    return str(pzip), "\n".join(msgs)


def process_chapter_batch(folder_files, folder_name, zip_file, min_panel_area, edge_sigma, low_threshold,
                          high_threshold, reading_order, refine_panels):
    if not folder_files and not zip_file:
        return None, "⚠️ Upload a chapter folder or a ZIP file first."

    settings = _build_settings(
        min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels
    )

    with tempfile.TemporaryDirectory() as tmp:
        td = Path(tmp)
        output_root = td / "processed"
        output_root.mkdir(parents=True, exist_ok=True)

        chapter_groups = []
        if folder_files:
            file_paths = [
                path for path in (_resolve_gradio_path(f) for f in folder_files)
                if path is not None and _is_image_path(path)
            ]
            if file_paths:
                chapter_name = _sanitize_folder_name(
                    folder_name,
                    _common_prefix_name([path.stem for path in file_paths]) or "uploaded_chapter",
                )
                chapter_groups.append((Path(chapter_name), sorted(file_paths, key=lambda p: _natural_key(p.name))))

        if zip_file:
            zip_path = _resolve_gradio_path(zip_file)
            if zip_path is not None:
                chapter_groups.extend(_extract_zip_groups(zip_path, td / "unzipped"))

        if not chapter_groups:
            return None, "⚠️ No valid chapter images were found in the uploaded folder/ZIP."

        total_pages = 0
        total_panels = 0
        failed = []
        processed_chapters = []

        for chapter_rel_path, image_paths in sorted(chapter_groups, key=lambda item: _natural_key(item[0].as_posix())):
            if not image_paths:
                failed.append(f"{chapter_rel_path.as_posix()} (empty chapter)")
                continue

            saved_paths, issues = _process_chapter_sequence(image_paths, chapter_rel_path, settings, output_root)
            total_pages += len(image_paths)
            total_panels += len(saved_paths)
            processed_chapters.append(chapter_rel_path.as_posix())
            failed.extend(issues)

        if not list(output_root.rglob("*.png")):
            return None, "⚠️ No panels were extracted. Try lowering sensitivity or checking the source images."

        zip_sources = []
        if zip_file:
            zip_path = _resolve_gradio_path(zip_file)
            if zip_path is not None:
                zip_sources.append(zip_path.stem)
        if folder_files:
            zip_sources.append(_sanitize_folder_name(folder_name, "folder_upload"))
        zip_base = _common_prefix_name(zip_sources or ["processed_chapters"]) + "_processed"
        pzip = _zip_directory_to_file(output_root, zip_base)

    msgs = [
        f"✅ Processed **{len(processed_chapters)}** chapter folder(s)",
        f"📄 Read **{total_pages}** page(s)",
        f"📦 Extracted **{total_panels}** panel(s)",
        "📁 Output keeps the chapter folder layout and saves panels directly inside each chapter folder.",
    ]
    if failed:
        msgs.append("⚠️ Issues:\n" + "\n".join(f"  • {issue}" for issue in failed))
    return str(pzip), "\n".join(msgs)


def process_series_flow(series_url, start_chap, end_chap, do_extract,
                        min_panel_area, edge_sigma, low_threshold,
                        high_threshold, reading_order, refine_panels,
                        progress=gr.Progress(track_tqdm=True)):
    if not SCRAPER_AVAILABLE:
        raise gr.Error("Scraper dependencies not installed (undetected-chromedriver).")
    if not series_url.strip():
        raise gr.Error("Please provide a Comic Series URL.")
    if start_chap > end_chap:
        raise gr.Error("Start chapter must be ≤ End chapter.")

    settings = _build_settings(
        min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels
    )

    progress(0, desc="Booting browser (Cloudflare bypass)…")
    disp = None
    driver = None
    try:
        disp = Display(visible=0, size=(1920, 1080))
        disp.start()
        driver = setup_uc_driver()
    except Exception as e:
        if disp: disp.stop()
        raise gr.Error(f"Failed to start browser: {e}")

    try:
        progress(0.05, desc="Fetching chapter list…")
        links = get_chapter_links(driver, series_url)
        if not links:
            raise gr.Error("No chapter links found — check the URL and try again.")

        filtered = [l for l in links if start_chap <= _chap_num(l) <= end_chap]
        if not filtered:
            raise gr.Error(f"No chapters found between {int(start_chap)} and {int(end_chap)}.")

        tot_panels, tot_imgs, failed = 0, 0, []
        label = "extracted_panels" if do_extract else "raw_chapters"
        pzip = Path(tempfile.gettempdir()) / f"manga_{label}.zip"

        with tempfile.TemporaryDirectory() as tmp:
            td = Path(tmp)
            raw_root = td / "raw"
            out_root = td / "output"
            raw_root.mkdir(parents=True, exist_ok=True)
            out_root.mkdir(parents=True, exist_ok=True)

            for idx, chap_url in enumerate(reversed(filtered)):
                chap_name = f"Chapter-{_chap_num(chap_url):g}"
                progress(0.1 + 0.7 * (idx / len(filtered)),
                         desc=f"{'Extracting' if do_extract else 'Downloading'} {chap_name}…")

                raw_dir = raw_root / chap_name
                imgs = download_chapter_images(driver, chap_url, str(raw_dir))
                if not imgs:
                    failed.append(f"{chap_name}: nothing downloaded")
                    continue

                tot_imgs += len(imgs)
                if not do_extract:
                    continue

                saved_paths, issues = _process_chapter_sequence(imgs, Path(chap_name), settings, out_root)
                tot_panels += len(saved_paths)
                failed.extend(issues)

            if do_extract:
                pzip = _zip_directory_to_file(out_root, f"manga_{label}")
            else:
                pzip = _zip_directory_to_file(raw_root, f"manga_{label}")

        msgs = [f"✅ {len(filtered)} chapters downloaded."]
        if do_extract:
            msgs.append(f"✅ {tot_imgs} pages → {tot_panels} panels extracted.")
        else:
            msgs.append(f"✅ {tot_imgs} raw pages saved.")
        if failed:
            msgs.append("⚠️ Issues:\n" + "\n".join(f"  • {f}" for f in failed))
        return str(pzip), "\n".join(msgs)

    except gr.Error:
        raise
    except Exception as e:
        raise gr.Error(str(e))
    finally:
        if driver is not None:
            driver.quit()
        if disp: disp.stop()

def fetch_chapter_images_only(chapter_url, html_source=""):
    if not str(chapter_url).strip() and not str(html_source).strip():
        raise gr.Error("Please provide a Chapter URL or HTML Source.")
        
    td = Path(tempfile.mkdtemp()) / "single_chapter"
    td.mkdir(parents=True, exist_ok=True)
    
    try:
        if str(html_source).strip():
            imgs = parse_images_from_html(html_source, str(td))
        else:
            if not SCRAPER_AVAILABLE:
                raise gr.Error("Browser scraping is disabled in this Space build. Paste HTML source instead, or use the API Hub tab.")
            disp = None
            try:
                disp = Display(visible=0, size=(1920, 1080))
                disp.start()
                driver = setup_uc_driver()
            except Exception as e:
                if disp: disp.stop()
                raise gr.Error(f"Failed to start browser: {e}")
                
            imgs = download_chapter_images(driver, chapter_url, str(td))
            driver.quit()
            if disp: disp.stop()
            
        if not imgs:
            raise gr.Error("No images found on this page. Check the URL or HTML contents.")
            
        pil_images = [Image.open(p) for p in imgs]
        choices = [p.name for p in imgs]
        state_paths = {p.name: str(p) for p in imgs}
        
        return pil_images, gr.update(choices=choices, value=choices, visible=True), state_paths, f"✅ Fetched {len(imgs)} pages successfully."
    except gr.Error:
        raise
    except Exception as e:
        raise gr.Error(str(e))
def _create_zip_from_selected(state_paths, selected_names, zip_name):
    paths = [Path(state_paths[name]) for name in selected_names if name in state_paths]
    pzip = Path(tempfile.gettempdir()) / f"{zip_name}.zip"
    with zipfile.ZipFile(pzip, "w", compression=zipfile.ZIP_DEFLATED) as zf:
        for p in paths:
            zf.write(p, p.name)
    return str(pzip)

def download_selected_single(state_paths, selected_names):
    if not selected_names:
        return None, "⚠️ No images selected."
    try:
        pzip = _create_zip_from_selected(state_paths, selected_names, "manga_chapter_raw")
        return str(pzip), f"✅ Prepared zip with {len(selected_names)} raw images."
    except Exception as e:
        return None, f"❌ Error: {e}"

def extract_selected_single(state_paths, selected_names, min_panel_area, edge_sigma, low_threshold,
                            high_threshold, reading_order, refine_panels):
    if not selected_names:
        return None, "⚠️ No images selected."

    settings = _build_settings(
        min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels
    )

    paths = [Path(state_paths[name]) for name in selected_names if name in state_paths]

    with tempfile.TemporaryDirectory() as tmp:
        td = Path(tmp)
        output_root = td / "chapter_panels"
        saved_paths, issues = _process_chapter_sequence(paths, Path("selected_chapter"), settings, output_root)
        pzip = _zip_directory_to_file(output_root, "manga_chapter_panels")

    msgs = [
        f"✅ Processed **{len(paths)}** images",
        f"📦 Extracted **{len(saved_paths)}** panels",
        "📁 Panels are saved directly inside the chapter folder with continuous numbering.",
    ]
    if issues:
        msgs.append("⚠️ Issues:\n" + "\n".join(f"  • {issue}" for issue in issues))
    return str(pzip), "\n".join(msgs)


def download_only_series(url, start, end, ma, es, lt, ht, ro, rp,
                         progress=gr.Progress(track_tqdm=True)):
    return process_series_flow(url, start, end, False, ma, es, lt, ht, ro, rp, progress)


def download_and_extract_series(url, start, end, ma, es, lt, ht, ro, rp,
                                progress=gr.Progress(track_tqdm=True)):
    return process_series_flow(url, start, end, True, ma, es, lt, ht, ro, rp, progress)


def load_sample():
    url = "https://raw.githubusercontent.com/kushalchoksi/manhwa-panel-extractor/refs/heads/main/jinwoo.jpg"
    return Image.open(BytesIO(requests.get(url).content))


# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------

def _settings_block():
    with gr.Accordion("⚙️ Advanced Settings", open=False):
        with gr.Row():
            min_panel_area = gr.Slider(0.1, 10.0, value=1.0, step=0.1, label="Min Panel Area (%)")
            edge_sigma = gr.Slider(0.1, 3.0, value=1.0, step=0.1, label="Edge Sensitivity")
        with gr.Row():
            low_threshold = gr.Slider(0.01, 0.5, value=0.1, step=0.01, label="Low Threshold")
            high_threshold = gr.Slider(0.05, 0.8, value=0.2, step=0.01, label="High Threshold")
        with gr.Row():
            reading_order = gr.Radio(["ltr", "rtl"], value="ltr", label="Reading Order")
            refine_panels = gr.Checkbox(value=False, label="Refine Panels (Trim Bubbles)")
    return min_panel_area, edge_sigma, low_threshold, high_threshold, reading_order, refine_panels


def create_demo():
    CSS = """
    .gradio-container { font-family: 'Segoe UI', Tahoma, sans-serif; }
    .hdr { text-align:center; background:linear-gradient(135deg,#667eea,#764ba2);
           color:#fff; padding:1.8rem; border-radius:10px; margin-bottom:1.2rem; }
    """
    with gr.Blocks(css=CSS, title="Manhwa Panel Extractor") as demo:
        gr.HTML("""
        <div class="hdr">
          <h1 style="margin:0;font-size:2rem;">🖼️ Manhwa Batch Extractor</h1>
          <p style="margin:.4rem 0 0;opacity:.9;">Extract panels from any manhwa/manga page — upload images or scrape directly from the web.</p>
        </div>
        """)
        if not SCRAPER_AVAILABLE:
            gr.Markdown("> Browser-based scraping is disabled in this Space build so the app stays lightweight and starts reliably. Batch uploads, HTML source parsing, and the API Hub still work.")

        with gr.Tabs():

            # ── Tab 1: Single Image ──────────────────────────────────────────
            with gr.TabItem("🖼️ Single Image"):
                with gr.Row():
                    with gr.Column():
                        s_img = gr.Image(label="Manhwa Page", type="pil", height=420)
                        s_sets = _settings_block()
                        with gr.Row():
                            s_samp = gr.Button("Load Sample", size="sm")
                            s_btn = gr.Button("Extract Panels ▶", variant="primary", size="lg")
                    with gr.Column():
                        s_stat = gr.Textbox(label="Status", interactive=False)
                        s_down = gr.File(label="⬇️ Download ZIP", interactive=False)
                        s_gal = gr.Gallery(label="Extracted Panels", columns=2, rows=3,
                                           height=550, object_fit="contain")

                s_btn.click(process_manhwa_image, inputs=[s_img] + list(s_sets),
                            outputs=[s_down, s_stat, s_gal])
                s_samp.click(load_sample, outputs=[s_img])

            # ── Tab 2: Batch Chapters ───────────────────────────────────────
            with gr.TabItem("📂 Batch Chapters"):
                gr.Markdown("""
### 📂 Chapter Batch Processor
Upload either:
- a single **chapter folder** of page images, or
- one **ZIP** that contains many chapter folders.

The output ZIP keeps the chapter folder layout and saves processed panels directly inside each chapter folder with continuous numbering. No page subfolders are created.
                """)
                with gr.Row():
                    with gr.Column():
                        b_folder = gr.File(file_count="directory", label="📁 Upload One Chapter Folder")
                        b_folder_name = gr.Textbox(
                            label="Chapter Folder Name",
                            value="chapter_001",
                            info="Used for folder uploads. ZIP uploads keep their own folder names.",
                        )
                        b_zip = gr.File(file_count="single", label="🗜️ Or Upload ZIP of Chapter Folders", file_types=[".zip"])
                        b_sets = _settings_block()
                        b_btn = gr.Button("Process Chapters ▶", variant="primary", size="lg")
                    with gr.Column():
                        b_stat = gr.Textbox(label="Status", interactive=False, lines=8)
                        b_down = gr.File(label="⬇️ Download Processed ZIP", interactive=False)

                b_btn.click(
                    process_chapter_batch,
                    inputs=[b_folder, b_folder_name, b_zip] + list(b_sets),
                    outputs=[b_down, b_stat],
                )

            # ── Tab 3: Web Downloader ────────────────────────────────────────
            with gr.TabItem("🌐 Download & Extract Series"):
                gr.Markdown("""
### 🌐 Scrape any Manga / Manhwa site
Paste the **series homepage** URL. Works with **TCB Scans, Kaynscan, OmegaScans, AsuraScans, MangaDex-style readers**, and most sites that scroll to reveal all images on one page.

> ⚠️ Sites that require you to click "Next Page" per image are not yet supported.
                """)
                if not SCRAPER_AVAILABLE:
                    gr.Markdown("> Browser scraping is unavailable in this build. Use the API Hub tab, or duplicate the Space with scraper dependencies if you want full site scraping.")
                with gr.Row():
                    c_url = gr.Textbox(label="Series URL",
                                       placeholder="https://tcbscanschapters.net/manga/one-piece/",
                                       scale=3)
                    c_strt = gr.Number(label="Start Chapter", value=1, precision=0, scale=1)
                    c_end = gr.Number(label="End Chapter", value=3, precision=0, scale=1)

                c_sets = _settings_block()

                with gr.Row():
                    c_btn_dl = gr.Button("⬇️ Just Download Raw Pages", variant="secondary", size="lg")
                    c_btn_ext = gr.Button("🚀 Download & Extract Panels", variant="primary", size="lg")

                c_stat = gr.Textbox(label="Log", interactive=False, lines=5)
                c_down = gr.File(label="⬇️ Download ZIP", interactive=False)

                c_btn_dl.click(download_only_series,
                               inputs=[c_url, c_strt, c_end] + list(c_sets),
                               outputs=[c_down, c_stat])
                c_btn_ext.click(download_and_extract_series,
                                inputs=[c_url, c_strt, c_end] + list(c_sets),
                                outputs=[c_down, c_stat])

            # ── Tab 4: Single Chapter Downloader ─────────────────────────────
            with gr.TabItem("📖 Single Chapter Scraper"):
                gr.Markdown("""
### 📖 Download or Extract a Single Chapter
Paste a direct **chapter URL** to fetch all images, or paste the page HTML source if the site blocks automation. You can then preview them, uncheck any unwanted pages (like sponsor pages or credits), and download or extract the final selection.
                """)
                if not SCRAPER_AVAILABLE:
                    gr.Markdown("> URL scraping is disabled here, but HTML source parsing still works. Open the chapter in your browser, choose `View Page Source`, and paste it below.")
                with gr.Row():
                    sc_url = gr.Textbox(label="Chapter URL", placeholder="https://site.com/manga/title/chapter-1/", scale=4)
                    sc_fetch = gr.Button("🔍 Fetch Images", variant="primary", scale=1)
                    
                with gr.Accordion("🛡️ Anti-Bot Bypass (For protected sites like Webtoonscan)", open=False):
                    gr.Markdown("If Cloudflare blocks the URL, open the chapter in your browser, **right-click -> View Page Source**, copy all text, and paste it here.")
                    sc_html = gr.Textbox(label="HTML Page Source", lines=4, placeholder="Paste entire HTML code here...")
                    
                sc_state = gr.State({})
                
                with gr.Row():
                    with gr.Column(scale=2):
                        sc_gal = gr.Gallery(label="Chapter Pages Preview", columns=3, rows=4, height=600, object_fit="contain", allow_preview=True)
                    with gr.Column(scale=1):
                        sc_stat = gr.Textbox(label="Status", interactive=False, lines=3)
                        sc_checks = gr.CheckboxGroup(label="Select Pages to Process", choices=[], visible=False)
                        
                sc_sets = _settings_block()
                
                with gr.Row():
                    sc_dl = gr.Button("⬇️ Download Selected Only", variant="secondary", size="lg")
                    sc_ext = gr.Button("🚀 Extract Panels from Selected", variant="primary", size="lg")
                    
                sc_down = gr.File(label="⬇️ Download ZIP", interactive=False)
                
                sc_fetch.click(
                    fn=fetch_chapter_images_only, 
                    inputs=[sc_url, sc_html], 
                    outputs=[sc_gal, sc_checks, sc_state, sc_stat]
                )
                sc_dl.click(
                    fn=download_selected_single, 
                    inputs=[sc_state, sc_checks], 
                    outputs=[sc_down, sc_stat]
                )
                sc_ext.click(
                    fn=extract_selected_single, 
                    inputs=[sc_state, sc_checks] + list(sc_sets), 
                    outputs=[sc_down, sc_stat]
                )

            # ── Tab 5: 📚 Built-in Manga Search Hub ──────────────────────────
            with gr.TabItem("📚 Built-in Manga Search Hub"):
                gr.Markdown("### 🔍 Search & Download API (100% Free & Cloudflare-Immune)\nSearch directly via MangaDex or Comick APIs. This bypasses all Cloudflare blocks natively.")
                with gr.Row():
                    hub_source = gr.Dropdown(["MangaDex", "Comick"], value="MangaDex", label="API Source", scale=1)
                    hub_query = gr.Textbox(label="Search Manga Title", placeholder="e.g. Solo Leveling", scale=3)
                    hub_btn_search = gr.Button("Search", variant="primary", scale=1)
                
                hub_manga_state = gr.State([])
                with gr.Row():
                    hub_manga_drop = gr.Dropdown(label="Select Series", choices=[], scale=4)
                    hub_btn_chaps = gr.Button("Load Chapters", scale=1)
                
                hub_chap_state = gr.State([])
                with gr.Row():
                    hub_chap_drop = gr.Dropdown(label="Select Chapter", choices=[], scale=4)
                    hub_btn_fetch = gr.Button("Fetch Images", variant="primary", scale=1)
                
                hub_state = gr.State({})
                
                with gr.Row():
                    with gr.Column(scale=2):
                        hub_gal = gr.Gallery(label="Chapter Pages Preview", columns=3, rows=4, height=600, object_fit="contain", allow_preview=True)
                    with gr.Column(scale=1):
                        hub_stat = gr.Textbox(label="Status", interactive=False, lines=3)
                        hub_checks = gr.CheckboxGroup(label="Select Pages to Process", choices=[], visible=False)
                        
                hub_sets = _settings_block()
                
                with gr.Row():
                    hub_dl = gr.Button("⬇️ Download Selected Only", variant="secondary", size="lg")
                    hub_ext = gr.Button("🚀 Extract Panels from Selected", variant="primary", size="lg")
                    
                hub_down = gr.File(label="⬇️ Download ZIP", interactive=False)
                
                hub_btn_search.click(hub_search, inputs=[hub_query, hub_source], outputs=[hub_manga_drop, hub_manga_state])
                hub_btn_chaps.click(hub_get_chapters, inputs=[hub_manga_drop, hub_manga_state, hub_source], outputs=[hub_chap_drop, hub_chap_state])
                hub_btn_fetch.click(hub_download_chapter, inputs=[hub_chap_drop, hub_chap_state, hub_source], outputs=[hub_gal, hub_checks, hub_state, hub_stat])
                hub_dl.click(download_selected_single, inputs=[hub_state, hub_checks], outputs=[hub_down, hub_stat])
                hub_ext.click(extract_selected_single, inputs=[hub_state, hub_checks] + list(hub_sets), outputs=[hub_down, hub_stat])

    return demo


if __name__ == "__main__":
    demo = create_demo()
    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)