Debian Codegraph @

gallery_dl/extractor/foriio.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2026 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://foriio.com/"""
10
11 from .common import Extractor, Message
12 from .. import text
13
14 BASE_PATTERN = r"(?:https?://)?(?:www\.)?fori(?:io\.com|\.io)"
15
16
17 class ForiioExtractor(Extractor):
18 """Base class for foriio extractors"""
19 category = "foriio"
20 root = "https://foriio.com"
21 root_api = "https://api.foriio.com/api"
22
23
24 class ForiioWorkExtractor(ForiioExtractor):
25 subcategory = "work"
26 directory_fmt = ("{category}", "{author[screen_name]} ({author[id]})",
27 "{date:%Y-%m-%d} {title[b:230]} ({work_id})")
28 filename_fmt = "{num:>02} {file_id}.{extension}"
29 archive_fmt = "{work_id}/{file_id}/{num}"
30 pattern = BASE_PATTERN + r"/works/(\d+)"
31 example = "https://www.foriio.com/works/12345"
32
33 def items(self):
34 url = f"{self.root_api}/v1/works/{self.groups[0]}"
35 work = self.request_json(url, notfound=True)["work"]
36
37 work.pop("related_works", None)
38 work.pop("single_work_og", None)
39 work.pop("og_image_url_for_instagram", None)
40 work["work_id"] = work.pop("id")
41 work["date"] = self.parse_datetime_iso(work["published_at"])
42
43 if "images" in work:
44 files = work.pop("images")
45 work["count"] = len(files)
46 yield Message.Directory, "", work
47 for work["num"], file in enumerate(files, 1):
48 url = file["urls"]["list"].partition("?")[0]
49
50 work["width"] = file["width"]
51 work["height"] = file["height"]
52 work["file_id"] = file["id"]
53
54 url = _orig(name := url[url.rfind("/")+1:])
55 yield Message.Url, url, text.nameext_from_url(name, work)
56 return
57
58 previews = self.config("previews", False)
59 if "videos" in work:
60 files = work.pop("videos")
61 work["count"] = len(files)
62 yield Message.Directory, "", work
63
64 video = self.config("videos", True)
65 for work["num"], file in enumerate(files, 1):
66 work["file_id"] = file.get("video_id") or file["title"]
67 work.update(file)
68 if video:
69 work["extension"] = "mp4"
70 yield Message.Url, "ytdl:" + file["url"], work
71 if previews:
72 url = file["picture_url"]
73 url = _orig(name := url[url.rfind("/")+1:])
74 yield Message.Url, url, text.nameext_from_url(name, work)
75 return
76
77 if "sounds" in work:
78 files = work.pop("sounds")
79 work["count"] = len(files)
80 yield Message.Directory, "", work
81
82 audio = self.config("audio", True)
83 for work["num"], file in enumerate(files, 1):
84 work["file_id"] = file["title"]
85 work.update(file)
86 if audio:
87 work["extension"] = "mp3"
88 yield Message.Url, "ytdl:" + file["url"], work
89 if previews:
90 url = file["picture_url"]
91 url = _orig(name := url[url.rfind("/")+1:])
92 yield Message.Url, url, text.nameext_from_url(name, work)
93 return
94
95 if "web_articles" in work:
96 files = work.pop("web_articles")
97 work["count"] = len(files)
98 yield Message.Directory, "", work
99
100 external = self.config("external", True)
101 for work["num"], file in enumerate(files, 1):
102 work["file_id"] = file["title"]
103 work.update(file)
104 if external:
105 yield Message.Queue, file["url"], work
106 if previews:
107 url = file["image"]
108 url = _orig(name := url[url.rfind("/")+1:])
109 yield Message.Url, url, text.nameext_from_url(name, work)
110 return
111
112 if "copy_writing" in work:
113 file = work["copy_writing"]
114 work["count"] = work["num"] = 1
115 work["file_id"] = file["id"]
116 url = file["image"]
117 url = _orig(name := url[url.rfind("/")+1:])
118 yield Message.Directory, "", work
119 yield Message.Url, url, text.nameext_from_url(name, work)
120
121 else:
122 return self.log.error("%s: Unsupported type %r",
123 work["id"], work["type"])
124
125
126 class ForiioUserExtractor(ForiioExtractor):
127 subcategory = "user"
128 pattern = BASE_PATTERN + r"/(?!works/)([^/?#]+)"
129 example = "https://foriio.com/USER"
130
131 def items(self):
132 if posts := self.config("posts"):
133 if isinstance(posts, str):
134 posts = posts.split(",")
135 posts = set(posts)
136
137 url = f"{self.root_api}/v1/users/{self.groups[0]}/works"
138 params = {
139 "page" : 1,
140 "per_page": "20",
141 }
142
143 base = self.root + "/works/"
144 while True:
145 data = self.request_json(url, params=params)
146
147 for work in data["works"]:
148 if posts and work.get("type") not in posts:
149 self.log.debug("%s: Skipping work of type %r",
150 work["id"], work["type"])
151 continue
152 work["_extractor"] = ForiioWorkExtractor
153 yield Message.Queue, base + str(work["id"]), work
154
155 try:
156 meta = data["meta"]
157 if meta["current_page"] >= meta["total_pages"]:
158 break
159 except Exception as exc:
160 self.log.traceback(exc)
161 break
162
163 params["page"] += 1
164
165
166 def _orig(name):
167 return "https://foriio.imgix.net/store/" + name