summaryrefslogblamecommitdiffstatshomepage
path: root/tests/plugins/scrapers/test_handlers.py
blob: 4e71041502d1f8df742da5de2cffdb472f770bd2 (plain) (tree)
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





















































































































































































































































































































































































































                                                                                                                                                                                                           

                                                    



                                                                                                      






                                                                                                              
                                                                         



































































































                                                                   
                                                            







                                                                     
                                                            


































































                                                                                   
import json
from datetime import date

import pytest

import hircine.enums as enums
from hircine.plugins.scrapers.handlers.dynastyscans import DynastyScansHandler
from hircine.plugins.scrapers.handlers.e621 import E621Handler
from hircine.plugins.scrapers.handlers.exhentai import (
    ExHentaiHandler,
)
from hircine.plugins.scrapers.handlers.exhentai import (
    sanitize as exhentai_sanitize,
)
from hircine.plugins.scrapers.handlers.mangadex import MangadexHandler
from hircine.scraper import Scraper
from hircine.scraper.types import (
    URL,
    Artist,
    Category,
    Censorship,
    Character,
    Circle,
    Date,
    Direction,
    Language,
    OriginalTitle,
    Rating,
    Tag,
    Title,
    World,
)


class Scraper(Scraper):
    def __init__(self, handler, json):
        self.handler = handler
        self.json = json
        super().__init__(None)

    def scrape(self):
        yield from self.handler.scrape(json.loads(self.json))


def test_dynastyscans():
    scraper = Scraper(
        DynastyScansHandler(),
        """
        {
            "manga": "Hoshiiro GirlDrop Comic Anthology",
            "chapter": 1,
            "chapter_minor": "",
            "title": "Hop, Step, Drop!",
            "author": "Fujisawa Kamiya",
            "group": "Cyan Steam (Stan Miller)",
            "date": "2018-02-05 00:00:00",
            "lang": "en",
            "language": "English",
            "count": 15,
            "category": "dynastyscans",
            "subcategory": "manga"
        }
        """,
    )

    assert set(scraper.collect()) == set(
        [
            Artist(name="Fujisawa Kamiya"),
            Circle(name="Cyan Steam (Stan Miller)"),
            Date(value=date(2018, 2, 5)),
            Language(value=enums.Language.EN),
            Title(value="Hoshiiro GirlDrop Comic Anthology Ch. 1: Hop, Step, Drop!"),
        ]
    )


def test_mangadex():
    scraper = Scraper(
        MangadexHandler(),
        """
        {
            "manga": "Shimeji Simulation",
            "manga_id": "28b5d037-175d-4119-96f8-e860e408ebe9",
            "title": "Danchi",
            "volume": 1,
            "chapter": 8,
            "chapter_minor": "",
            "chapter_id": "2a115ccb-de52-4b84-9166-cebd152d9396",
            "date": "2019-09-22 04:19:15",
            "lang": "en",
            "language": "English",
            "count": 12,
            "artist": [
                "Tsukumizu"
            ],
            "author": [
                "Tsukumizu"
            ],
            "group": [
                "Orchesc/a/ns"
            ],
            "status": "completed",
            "tags": [
                "Sci-Fi",
                "Comedy",
                "Girls' Love",
                "4-Koma",
                "Philosophical",
                "School Life",
                "Slice of Life"
            ],
            "category": "mangadex",
            "subcategory": "chapter"
        }
        """,
    )

    assert set(scraper.collect()) == set(
        [
            Artist(name="Tsukumizu"),
            Circle(name="Orchesc/a/ns"),
            Date(value=date(2019, 9, 22)),
            Language(value=enums.Language.EN),
            Tag(namespace="none", tag="4-Koma"),
            Tag(namespace="none", tag="Comedy"),
            Tag(namespace="none", tag="Girls' Love"),
            Tag(namespace="none", tag="Philosophical"),
            Tag(namespace="none", tag="School Life"),
            Tag(namespace="none", tag="Sci-Fi"),
            Tag(namespace="none", tag="Slice of Life"),
            Title(value="Shimeji Simulation Vol. 1, Ch. 8: Danchi"),
            URL("https://mangadex.org/chapter/2a115ccb-de52-4b84-9166-cebd152d9396"),
        ]
    )


@pytest.mark.parametrize(
    "data, title",
    [
        ({"volume": 1, "chapter": 8}, "Manga Vol. 1, Ch. 8: Title"),
        ({"volume": 0, "chapter": 1}, "Manga Ch. 1: Title"),
        ({"volume": 0, "chapter": 0}, "Manga: Title"),
    ],
    ids=[
        "volume and chapter",
        "chapter only",
        "none",
    ],
)
def test_mangadex_handles_volume_and_chapter(data, title):
    common = {"manga": "Manga", "title": "Title"}
    scraper = Scraper(MangadexHandler(), json.dumps(common | data))

    assert list(scraper.collect()) == [Title(value=title)]


def test_e621_pool():
    scraper = Scraper(
        E621Handler(),
        """
        {
            "id": 2968472,
            "created_at": "2021-10-10T04:13:53.286-04:00",
            "updated_at": "2024-11-02T08:58:06.724-04:00",
            "file": {
                "width": 800,
                "height": 800,
                "ext": "jpg",
                "size": 530984,
                "md5": "1ec7e397bb22c1454ab1986fd3f3edc5",
                "url": "https://static1.e621.net/data/1e/c7/1ec7e397bb22c1454ab1986fd3f3edc5.jpg"
            },
            "preview": {
                "width": 150,
                "height": 150,
                "url": "https://static1.e621.net/data/preview/1e/c7/1ec7e397bb22c1454ab1986fd3f3edc5.jpg"
            },
            "sample": {
                "has": false,
                "height": 800,
                "width": 800,
                "url": "https://static1.e621.net/data/1e/c7/1ec7e397bb22c1454ab1986fd3f3edc5.jpg",
                "alternates": {}
            },
            "score": {
                "up": 202,
                "down": -1,
                "total": 201
            },
            "tags": {
                "general": [
                    "beak"
                ],
                "artist": [
                    "falseknees"
                ],
                "copyright": [],
                "character": [],
                "species": [
                    "bird"
                ],
                "invalid": [],
                "meta": [
                    "comic",
                    "english_text"
                ],
                "lore": [
                    "parent_(lore)",
                    "parent_and_child_(lore)"
                ]
            },
            "locked_tags": [],
            "change_seq": 60808337,
            "flags": {
                "pending": false,
                "flagged": false,
                "note_locked": false,
                "status_locked": false,
                "rating_locked": false,
                "deleted": false
            },
            "rating": "s",
            "fav_count": 194,
            "sources": [
                "https://twitter.com/FalseKnees/status/1324869853627478022"
            ],
            "pools": [
                25779
            ],
            "relationships": {
                "parent_id": null,
                "has_children": false,
                "has_active_children": false,
                "children": []
            },
            "approver_id": 171673,
            "uploader_id": 178921,
            "description": "",
            "comment_count": 1,
            "is_favorited": false,
            "has_notes": false,
            "duration": null,
            "num": 1,
            "filename": "1ec7e397bb22c1454ab1986fd3f3edc5",
            "extension": "jpg",
            "date": "2021-10-10 08:13:53",
            "pool": {
                "id": 25779,
                "name": "Kids say the darnedest shit - falseknees",
                "created_at": "2021-10-10T04:17:07.006-04:00",
                "updated_at": "2021-10-10T04:17:07.006-04:00",
                "creator_id": 178921,
                "description": "The terror of every parent.",
                "is_active": true,
                "category": "series",
                "creator_name": "OneMoreAnonymous",
                "post_count": 4
            },
            "category": "e621",
            "subcategory": "pool"
        }
        """,
    )

    assert set(scraper.collect()) == set(
        [
            Artist(name="falseknees"),
            Category(value=enums.Category.COMIC),
            Censorship(value=enums.Censorship.NONE),
            Date(value=date(2021, 10, 10)),
            Language(value=enums.Language.EN),
            Rating(value=enums.Rating.SAFE),
            Tag(namespace="none", tag="beak"),
            Tag(namespace="none", tag="bird"),
            Title(value="Kids say the darnedest shit - falseknees"),
            URL("https://e621.net/pools/25779"),
        ]
    )


@pytest.mark.parametrize(
    "data, censorship",
    [
        ({"tags": {"meta": ["censor_bar"]}}, enums.Censorship.BAR),
        ({"tags": {"meta": ["mosaic_censorship"]}}, enums.Censorship.MOSAIC),
        ({"tags": {"meta": ["uncensored"]}}, enums.Censorship.NONE),
        ({"tags": {"meta": []}}, enums.Censorship.NONE),
    ],
    ids=[
        "bars",
        "mosaic",
        "uncensored",
        "uncensored (implied)",
    ],
)
def test_e621_handles_censorship(data, censorship):
    common = {"subcategory": "pool"}
    scraper = Scraper(E621Handler(), json.dumps(common | data))

    assert set(scraper.collect()) == set([Censorship(value=censorship)])


def test_exhentai_explicit():
    scraper = Scraper(
        ExHentaiHandler(),
        """
        {
            "gid": 2771624,
            "token": "43108ee23b",
            "thumb": "https://s.exhentai.org/t/12/80/1280a064a2ab3d70b9feb56bd0c55dbfc3ab6a39-309830-950-1351-jpg_250.jpg",
            "title": "[NAGABE] Smell ch.01 - ch.06",
            "title_jpn": "SMELL",
            "eh_category": "Doujinshi",
            "uploader": "randaldog",
            "date": "2023-12-19 23:50:00",
            "parent": "https://exhentai.org/g/2736803/b191bfed72/",
            "expunged": false,
            "language": "English",
            "filesize": 74469868,
            "filecount": "170",
            "favorites": "751",
            "rating": "4.83",
            "torrentcount": "0",
            "lang": "en",
            "tags": [
                "language:english",
                "language:translated",
                "parody:original",
                "artist:nagabe",
                "male:dog boy",
                "male:furry",
                "male:males only",
                "male:smell",
                "male:yaoi",
                "other:story arc"
            ],
            "category": "exhentai",
            "subcategory": "gallery"
        }
        """,
    )

    assert set(scraper.collect()) == set(
        [
            Artist(name="nagabe"),
            Category(value=enums.Category.DOUJINSHI),
            Censorship(value=enums.Censorship.BAR),
            Date(value=date(2023, 12, 19)),
            Direction(value=enums.Direction.RIGHT_TO_LEFT),
            Language(value=enums.Language.EN),
            OriginalTitle(value="SMELL"),
            Rating(value=enums.Rating.EXPLICIT),
            Tag(namespace="male", tag="dog boy"),
            Tag(namespace="male", tag="furry"),
            Tag(namespace="male", tag="males only"),
            Tag(namespace="male", tag="smell"),
            Tag(namespace="male", tag="yaoi"),
            Tag(namespace="other", tag="story arc"),
            Title(value="Smell ch.01 - ch.06"),
            URL("https://exhentai.org/g/2771624/43108ee23b"),
            World(name="original"),
        ]
    )


def test_exhentai_non_h():
    scraper = Scraper(
        ExHentaiHandler(),
        """
        {
            "gid": 1025913,
            "token": "fdaabef1a2",
            "thumb": "https://s.exhentai.org/t/51/17/5117cde63cc14436c5ad7f2dd06abb52c86aff65-23642001-2866-4047-png_250.jpg",
            "title": "(C91) [Animachine (Shimahara)] Iya na Kao Sarenagara Opantsu Misete Moraitai Manga | A manga about girl showing you her panties while making a disgusted face [English] [葛の寺]",
            "title_jpn": "(C91) [アニマルマシーン (40原)] 嫌な顔されながらおパンツ見せてもらいたい漫画 [英訳]",
            "eh_category": "Non-H",
            "uploader": "葛の寺",
            "date": "2017-02-04 04:25:00",
            "parent": "https://exhentai.org/g/1025875/cfe6adccb8/",
            "expunged": false,
            "language": "English",
            "filesize": 0,
            "filecount": "23",
            "favorites": "1088",
            "rating": "4.74",
            "torrentcount": "1",
            "lang": "en",
            "tags": [
                "language:english",
                "language:translated",
                "parody:iya na kao sare nagara opantsu misete moraitai",
                "group:animachine",
                "artist:shimahara",
                "female:femdom",
                "female:schoolgirl uniform",
                "other:full color"
            ],
            "category": "exhentai",
            "subcategory": "gallery"
        }
        """,  # noqa: E501
    )

    assert set(scraper.collect()) == set(
        [
            Artist(name="shimahara"),
            Censorship(value=enums.Censorship.NONE),
            Circle(name="animachine"),
            Date(value=date(2017, 2, 4)),
            Language(value=enums.Language.EN),
            OriginalTitle(value="嫌な顔されながらおパンツ見せてもらいたい漫画"),
            Rating(value=enums.Rating.QUESTIONABLE),
            Tag(namespace="female", tag="femdom"),
            Tag(namespace="female", tag="schoolgirl uniform"),
            Tag(namespace="other", tag="full color"),
            Title(
                value="A manga about girl showing you her panties while making a disgusted face"  # noqa: E501
            ),
            URL("https://exhentai.org/g/1025913/fdaabef1a2"),
            World(name="iya na kao sare nagara opantsu misete moraitai"),
        ]
    )


@pytest.mark.parametrize(
    "text, sanitized",
    [
        ("(foo) Title", "Title"),
        ("[foo] {bar} =baz= Title", "Title"),
        ("Foreign Title | Localized Title", "Localized Title"),
    ],
    ids=[
        "parens at beginning",
        "bracket-likes",
        "split titles",
    ],
)
def test_exhentai_sanitizes(text, sanitized):
    assert exhentai_sanitize(text, split=True) == sanitized


@pytest.mark.parametrize(
    "data, expect",
    [
        (
            {"category": "doujinshi"},
            set(
                [
                    Category(value=enums.Category.DOUJINSHI),
                    Censorship(value=enums.Censorship.BAR),
                    Rating(value=enums.Rating.EXPLICIT),
                    Direction(value=enums.Direction.RIGHT_TO_LEFT),
                ]
            ),
        ),
        (
            {"eh_category": "doujinshi"},
            set(
                [
                    Category(value=enums.Category.DOUJINSHI),
                    Censorship(value=enums.Censorship.BAR),
                    Rating(value=enums.Rating.EXPLICIT),
                    Direction(value=enums.Direction.RIGHT_TO_LEFT),
                ]
            ),
        ),
        (
            {"category": "manga"},
            set(
                [
                    Category(value=enums.Category.MANGA),
                    Censorship(value=enums.Censorship.BAR),
                    Rating(value=enums.Rating.EXPLICIT),
                    Direction(value=enums.Direction.RIGHT_TO_LEFT),
                ]
            ),
        ),
        (
            {"category": "western"},
            set(
                [
                    Censorship(value=enums.Censorship.NONE),
                    Rating(value=enums.Rating.EXPLICIT),
                ]
            ),
        ),
        (
            {"category": "artist cg"},
            set(
                [
                    Category(value=enums.Category.COMIC),
                    Censorship(value=enums.Censorship.BAR),
                    Rating(value=enums.Rating.EXPLICIT),
                ]
            ),
        ),
        (
            {"category": "game cg"},
            set(
                [
                    Category(value=enums.Category.GAME_CG),
                    Censorship(value=enums.Censorship.BAR),
                    Rating(value=enums.Rating.EXPLICIT),
                ]
            ),
        ),
        (
            {"category": "image set"},
            set(
                [
                    Category(value=enums.Category.IMAGE_SET),
                    Censorship(value=enums.Censorship.BAR),
                    Rating(value=enums.Rating.EXPLICIT),
                ]
            ),
        ),
        (
            {"category": "non-h"},
            set(
                [
                    Censorship(value=enums.Censorship.NONE),
                    Rating(value=enums.Rating.QUESTIONABLE),
                ]
            ),
        ),
        (
            {"category": "western", "tags": ["other:western non-h"]},
            set(
                [
                    Censorship(value=enums.Censorship.NONE),
                    Rating(value=enums.Rating.QUESTIONABLE),
                ]
            ),
        ),
    ],
    ids=[
        "category from category field",
        "category from eh_category field",
        "manga category",
        "western category",
        "artist cg category",
        "game cg category",
        "image set category",
        "non-h category",
        "western non-h tag",
    ],
)
def test_exhentai_parses(data, expect):
    scraper = Scraper(ExHentaiHandler(), json.dumps(data | {"gid": 1, "token": 1}))

    expect.add(URL(value="https://exhentai.org/g/1/1"))

    assert set(scraper.collect()) == expect


@pytest.mark.parametrize(
    "tag, parsed",
    [
        ("parody:foo", World(name="foo")),
        ("artist:foo", Artist(name="foo")),
        ("character:foo", Character(name="foo")),
        ("group:foo", Circle(name="foo")),
        ("other:artbook", Category(value=enums.Category.ARTBOOK)),
        ("other:non-h imageset", Category(value=enums.Category.IMAGE_SET)),
        ("other:western imageset", Category(value=enums.Category.IMAGE_SET)),
        ("other:comic", Category(value=enums.Category.COMIC)),
        ("other:variant set", Category(value=enums.Category.VARIANT_SET)),
        ("other:webtoon", Category(value=enums.Category.WEBTOON)),
        ("other:full censorship", Censorship(value=enums.Censorship.FULL)),
        ("other:mosaic censorship", Censorship(value=enums.Censorship.MOSAIC)),
        ("other:uncensored", Censorship(value=enums.Censorship.NONE)),
        ("generic", Tag(namespace=None, tag="generic")),
    ],
    ids=[
        "parody",
        "group",
        "artist",
        "character",
        "other:artbook",
        "other:image set",
        "other:western image set",
        "other:comic",
        "other:variant set",
        "other:webtoon",
        "other:full censorship",
        "other:mosaic censorship",
        "other:uncensored",
        "generic",
    ],
)
def test_exhentai_parses_tags(tag, parsed):
    scraper = Scraper(
        ExHentaiHandler(), json.dumps({"tags": [tag], "gid": 1, "token": 1})
    )
    expect = set([URL(value="https://exhentai.org/g/1/1"), parsed])

    assert set(scraper.collect()) > expect