summaryrefslogtreecommitdiffstatshomepage
path: root/docs/_examples/example_scraper.py
blob: d00c2924ee3c6fdef73d71f5b25df297db94c894 (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
import json

from hircine.scraper import Scraper
from hircine.scraper.types import Artist, Character, Tag, Title
from hircine.scraper.utils import open_archive_file, parse_dict


class MyScraper(Scraper):
    name = "Example scraper"
    source = "example"

    def __init__(self, comic):
        super().__init__(comic)

        self.data = self.load()

        if self.data:
            self.is_available = True

    def load(self):
        try:
            with open_archive_file(self.comic.archive, "metadata.json") as jif:
                return json.load(jif)
        except Exception:
            return {}

    def scrape(self):
        parsers = {
            "title": Title,
            "tags": {
                "artists": Artist,
                "misc": Tag.from_string,
                "characters": Character,
            },
        }

        yield from parse_dict(parsers, self.data)