summaryrefslogtreecommitdiffstatshomepage
path: root/tests/plugins/scrapers/test_ehentai_api.py
blob: c7464404a7d91c64e927270ea2dab5e15552f994 (plain) (blame)
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
from datetime import date

import pytest

import hircine.enums as enums
import hircine.plugins.scrapers.ehentai_api as ehentai_api
from hircine.scraper import ScrapeError
from hircine.scraper.types import (
    URL,
    Artist,
    Censorship,
    Circle,
    Date,
    Language,
    OriginalTitle,
    Rating,
    Tag,
    Title,
    World,
)


def test_does_scrape(requests_mock, gen_comic):
    comic = next(gen_comic)
    comic.url = "https://exhentai.org/g/1025913/fdaabef1a2"

    scraper = ehentai_api.EHentaiAPIScraper(comic)

    requests_mock.post(
        ehentai_api.API_URL,
        text="""
    {
      "gmetadata": [
        {
          "gid": 1025913,
          "token": "fdaabef1a2",
          "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原)] 嫌な顔されながらおパンツ見せてもらいたい漫画 [英訳]",
          "category": "Non-H",
          "thumb": "https://ehgt.org/51/17/5117cde63cc14436c5ad7f2dd06abb52c86aff65-23642001-2866-4047-png_250.jpg",
          "uploader": "葛の寺",
          "posted": "1486182349",
          "filecount": "23",
          "filesize": 528093263,
          "expunged": false,
          "rating": "4.72",
          "torrentcount": "1",
          "torrents": [
            {
              "hash": "30c7124efca83bf0db1b9fd5ab4511da5f28a60b",
              "added": "1486121301",
              "name": "(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] [葛の寺].zip",
              "tsize": "20800",
              "fsize": "528093242"
            }
          ],
          "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"
          ],
          "parent_gid": "1025875",
          "parent_key": "cfe6adccb8",
          "first_gid": "1025646",
          "first_key": "098b4a982a"
        }
      ]
    }
    """,  # noqa: E501
    )

    assert scraper.is_available
    assert scraper.id == 1025913
    assert scraper.token == "fdaabef1a2"

    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"),
        ]
    )


def test_is_not_available_with_wrong_url(gen_comic):
    comic = next(gen_comic)
    comic.url = "https://example.com"

    scraper = ehentai_api.EHentaiAPIScraper(comic)

    assert not scraper.is_available


def test_raises_scrape_error_with_invalid_json(requests_mock, gen_comic):
    comic = next(gen_comic)
    comic.url = "https://exhentai.org/g/1025913/fdaabef1a2"

    scraper = ehentai_api.EHentaiAPIScraper(comic)

    requests_mock.post(ehentai_api.API_URL, text="{")

    assert scraper.is_available
    assert scraper.id == 1025913
    assert scraper.token == "fdaabef1a2"

    with pytest.raises(ScrapeError, match="Could not parse JSON response"):
        assert set(scraper.collect()) == set()


def test_raises_scrape_error_with_missing_field(requests_mock, gen_comic):
    comic = next(gen_comic)
    comic.url = "https://exhentai.org/g/1025913/fdaabef1a2"

    scraper = ehentai_api.EHentaiAPIScraper(comic)

    requests_mock.post(ehentai_api.API_URL, text="{}")

    assert scraper.is_available
    assert scraper.id == 1025913
    assert scraper.token == "fdaabef1a2"

    with pytest.raises(ScrapeError, match="Response is missing 'gmetadata' field"):
        assert set(scraper.collect()) == set()


def test_raises_scrape_error_with_error_code(requests_mock, gen_comic):
    comic = next(gen_comic)
    comic.url = "https://exhentai.org/g/1025913/fdaabef1a2"

    scraper = ehentai_api.EHentaiAPIScraper(comic)

    requests_mock.post(ehentai_api.API_URL, status_code=500)

    assert scraper.is_available
    assert scraper.id == 1025913
    assert scraper.token == "fdaabef1a2"

    with pytest.raises(ScrapeError, match="Request failed with status code 500"):
        assert set(scraper.collect()) == set()