summaryrefslogtreecommitdiffstatshomepage
path: root/src/hircine/api/types.py
blob: b9fe0e71b09a55c09f57800ed28af63b6564b736 (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
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
import datetime
from typing import Generic, List, Optional, TypeVar

import strawberry

import hircine.scraper.types as scraped
from hircine.enums import Category, Censorship, Direction, Language, Layout, Rating

T = TypeVar("T")


@strawberry.type
class Base:
    id: int

    def __init__(self, model):
        self.id = model.id


@strawberry.type
class MixinName:
    name: str

    def __init__(self, model):
        self.name = model.name
        super().__init__(model)


@strawberry.type
class MixinFavourite:
    favourite: bool

    def __init__(self, model):
        self.favourite = model.favourite
        super().__init__(model)


@strawberry.type
class MixinOrganized:
    organized: bool

    def __init__(self, model):
        self.organized = model.organized
        super().__init__(model)


@strawberry.type
class MixinBookmarked:
    bookmarked: bool

    def __init__(self, model):
        self.bookmarked = model.bookmarked
        super().__init__(model)


@strawberry.type
class MixinCreatedAt:
    created_at: datetime.datetime

    def __init__(self, model):
        self.created_at = model.created_at
        super().__init__(model)


@strawberry.type
class MixinModifyDates(MixinCreatedAt):
    updated_at: datetime.datetime

    def __init__(self, model):
        self.updated_at = model.updated_at
        super().__init__(model)


@strawberry.type
class FilterResult(Generic[T]):
    count: int
    edges: List["T"]


@strawberry.type
class Archive(MixinName, MixinOrganized, Base):
    cover: "Image"
    path: str
    size: int
    page_count: int

    def __init__(self, model):
        super().__init__(model)
        self.path = model.path
        self.cover = Image(model.cover)
        self.size = model.size
        self.page_count = model.page_count


@strawberry.type
class FullArchive(MixinCreatedAt, Archive):
    pages: List["Page"]
    comics: List["Comic"]
    mtime: datetime.datetime

    def __init__(self, model):
        super().__init__(model)
        self.mtime = model.mtime
        self.pages = [Page(p) for p in model.pages]
        self.comics = [Comic(c) for c in model.comics]


@strawberry.type
class Page(Base):
    path: str
    image: "Image"
    comic_id: Optional[int]

    def __init__(self, model):
        super().__init__(model)
        self.path = model.path
        self.image = Image(model.image)
        self.comic_id = model.comic_id


@strawberry.type
class Image(Base):
    hash: str
    width: int
    height: int
    aspect_ratio: float

    def __init__(self, model):
        super().__init__(model)
        self.hash = model.hash
        self.width = model.width
        self.height = model.height
        self.aspect_ratio = model.aspect_ratio


@strawberry.type
class Comic(MixinFavourite, MixinOrganized, MixinBookmarked, Base):
    title: str
    original_title: Optional[str]
    language: Optional[Language]
    date: Optional[datetime.date]
    cover: "Image"
    rating: Optional[Rating]
    category: Optional[Category]
    censorship: Optional[Censorship]
    tags: List["ComicTag"]
    artists: List["Artist"]
    characters: List["Character"]
    circles: List["Circle"]
    worlds: List["World"]
    page_count: int

    def __init__(self, model):
        super().__init__(model)
        self.title = model.title
        self.original_title = model.original_title
        self.language = model.language
        self.date = model.date
        self.cover = Image(model.cover)
        self.rating = model.rating
        self.category = model.category
        self.censorship = model.censorship
        self.tags = [ComicTag(t.namespace, t.tag) for t in model.tags]
        self.artists = [Artist(a) for a in model.artists]
        self.characters = [Character(c) for c in model.characters]
        self.worlds = [World(w) for w in model.worlds]
        self.circles = [Circle(g) for g in model.circles]
        self.page_count = model.page_count


@strawberry.type
class FullComic(MixinModifyDates, Comic):
    archive: "Archive"
    url: Optional[str]
    pages: List["Page"]
    direction: Direction
    layout: Layout

    def __init__(self, model):
        super().__init__(model)
        self.direction = model.direction
        self.layout = model.layout
        self.archive = Archive(model.archive)
        self.pages = [Page(p) for p in model.pages]
        self.url = model.url


@strawberry.type
class Tag(MixinName, Base):
    description: Optional[str]

    def __init__(self, model):
        super().__init__(model)
        self.description = model.description


@strawberry.type
class FullTag(Tag):
    namespaces: List["Namespace"]

    def __init__(self, model):
        super().__init__(model)
        self.namespaces = [Namespace(n) for n in model.namespaces]


@strawberry.type
class Namespace(MixinName, Base):
    sort_name: Optional[str]

    def __init__(self, model):
        super().__init__(model)
        self.sort_name = model.sort_name


@strawberry.type
class ComicTag:
    id: str
    name: str
    description: Optional[str]

    def __init__(self, namespace=None, tag=None):
        tag_name, tag_id = ("", "")
        ns_name, ns_id = ("", "")

        if tag:
            tag_name, tag_id = (tag.name, tag.id)
        if namespace:
            ns_name, ns_id = (namespace.name, namespace.id)

        self.name = f"{ns_name}:{tag_name}"
        self.id = f"{ns_id}:{tag_id}"
        if tag:
            self.description = tag.description


@strawberry.type
class Artist(MixinName, Base):
    def __init__(self, model):
        super().__init__(model)


@strawberry.type
class Character(MixinName, Base):
    def __init__(self, model):
        super().__init__(model)


@strawberry.type
class Circle(MixinName, Base):
    def __init__(self, model):
        super().__init__(model)


@strawberry.type
class World(MixinName, Base):
    def __init__(self, model):
        super().__init__(model)


@strawberry.type
class ComicScraper:
    id: str
    name: str

    def __init__(self, id, scraper):
        self.id = id
        self.name = scraper.name


@strawberry.type
class ScrapeComicResult:
    data: "ScrapedComic"
    warnings: List[str] = strawberry.field(default_factory=lambda: [])


@strawberry.type
class ScrapedComic:
    title: Optional[str] = None
    original_title: Optional[str] = None
    url: Optional[str] = None
    language: Optional[Language] = None
    date: Optional[datetime.date] = None
    rating: Optional[Rating] = None
    category: Optional[Category] = None
    censorship: Optional[Censorship] = None
    direction: Optional[Direction] = None
    layout: Optional[Layout] = None
    tags: List[str] = strawberry.field(default_factory=lambda: [])
    artists: List[str] = strawberry.field(default_factory=lambda: [])
    characters: List[str] = strawberry.field(default_factory=lambda: [])
    circles: List[str] = strawberry.field(default_factory=lambda: [])
    worlds: List[str] = strawberry.field(default_factory=lambda: [])

    @classmethod
    def from_generator(cls, generator):
        data = cls()

        seen = set()
        for item in generator:
            if not item or item in seen:
                continue

            seen.add(item)

            match item:
                case scraped.Title():
                    data.title = item.value
                case scraped.OriginalTitle():
                    data.original_title = item.value
                case scraped.URL():
                    data.url = item.value
                case scraped.Language():
                    data.language = item.value
                case scraped.Date():
                    data.date = item.value
                case scraped.Rating():
                    data.rating = item.value
                case scraped.Category():
                    data.category = item.value
                case scraped.Censorship():
                    data.censorship = item.value
                case scraped.Direction():
                    data.direction = item.value
                case scraped.Layout():
                    data.layout = item.value
                case scraped.Tag():
                    data.tags.append(item.to_string())
                case scraped.Artist():
                    data.artists.append(item.name)
                case scraped.Character():
                    data.characters.append(item.name)
                case scraped.Circle():
                    data.circles.append(item.name)
                case scraped.World():
                    data.worlds.append(item.name)

        return data