Debian Codegraph @

gallery_dl/extractor/piczel.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018-2023 Mike Fährmann
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8
9 """Extractors for https://piczel.tv/"""
10
11 from .common import Extractor, Message
12 from .. import text
13
14 BASE_PATTERN = r"(?:https?://)?(?:www\.)?piczel\.tv"
15
16
17 class PiczelExtractor(Extractor):
18 """Base class for piczel extractors"""
19 category = "piczel"
20 directory_fmt = ("{category}", "{user[username]}")
21 filename_fmt = "{category}_{id}_{title}_{num:>02}.{extension}"
22 archive_fmt = "{id}_{num}"
23 root = "https://piczel.tv"
24 root_api = root
25
26 def items(self):
27 for post in self.posts():
28 post["tags"] = [t["title"] for t in post["tags"] if t["title"]]
29 post["date"] = self.parse_datetime_iso(post["created_at"])
30
31 if post["multi"]:
32 images = post["images"]
33 del post["images"]
34 post["count"] = len(images)
35 yield Message.Directory, "", post
36 for post["num"], image in enumerate(images):
37 if "id" in image:
38 del image["id"]
39 post.update(image)
40 url = post["image"]["url"]
41 yield Message.Url, url, text.nameext_from_url(url, post)
42
43 else:
44 post["count"] = 1
45 yield Message.Directory, "", post
46 post["num"] = 0
47 url = post["image"]["url"]
48 yield Message.Url, url, text.nameext_from_url(url, post)
49
50 def posts(self):
51 """Return an iterable with all relevant post objects"""
52
53 def _pagination(self, url, pnum=1):
54 params = {"page": pnum}
55
56 while True:
57 data = self.request_json(url, params=params)
58
59 yield from data["data"]
60
61 params["page"] = data["meta"]["next_page"]
62 if not params["page"]:
63 return
64
65
66 class PiczelUserExtractor(PiczelExtractor):
67 """Extractor for all images from a user's gallery"""
68 subcategory = "user"
69 pattern = BASE_PATTERN + r"/gallery/([^/?#]+)/?$"
70 example = "https://piczel.tv/gallery/USER"
71
72 def posts(self):
73 url = f"{self.root_api}/api/users/{self.groups[0]}/gallery"
74 return self._pagination(url)
75
76
77 class PiczelFolderExtractor(PiczelExtractor):
78 """Extractor for images inside a user's folder"""
79 subcategory = "folder"
80 directory_fmt = ("{category}", "{user[username]}", "{folder[name]}")
81 archive_fmt = "f{folder[id]}_{id}_{num}"
82 pattern = BASE_PATTERN + r"/gallery/(?!image/)[^/?#]+/(\d+)"
83 example = "https://piczel.tv/gallery/USER/12345"
84
85 def posts(self):
86 url = f"{self.root_api}/api/gallery/folder/{self.groups[0]}"
87 return self._pagination(url)
88
89
90 class PiczelImageExtractor(PiczelExtractor):
91 """Extractor for individual images"""
92 subcategory = "image"
93 pattern = BASE_PATTERN + r"/gallery/image/(\d+)"
94 example = "https://piczel.tv/gallery/image/12345"
95
96 def posts(self):
97 url = f"{self.root_api}/api/gallery/{self.groups[0]}"
98 return (self.request_json(url),)