summaryrefslogblamecommitdiffstatshomepage
path: root/tests/plugins/scrapers/test_ehentai_api.py
blob: c7464404a7d91c64e927270ea2dab5e15552f994 (plain) (tree)





























































































































                                                                                                                                                                                                                















                                                                                   













                                                                                 
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()