Debian Codegraph @

gallery_dl/extractor/xfolio.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2025-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://xfolio.jp/"""
10
11 from .common import Extractor, Message
12 from .. import text
13
14 BASE_PATTERN = r"(?:https?://)?xfolio\.jp(?:/[^/?#]+)?"
15
16
17 class XfolioExtractor(Extractor):
18 """Base class for xfolio extractors"""
19 category = "xfolio"
20 root = "https://xfolio.jp"
21 cookies_domain = ".xfolio.jp"
22 directory_fmt = ("{category}", "{creator_slug}", "{work_id}")
23 filename_fmt = "{work_id}_{image_id}.{extension}"
24 archive_fmt = "{work_id}_{image_id}"
25 request_interval = (0.5, 1.5)
26 _warning = True
27
28 def _init(self):
29 if self.cookies_check(("xfolio_session",)):
30 self._logged_in = True
31 else:
32 self._logged_in = False
33 if self._warning:
34 XfolioExtractor._warning = False
35 self.log.error("'xfolio_session' cookie required")
36
37 def items(self):
38 data = {"_extractor": XfolioWorkExtractor}
39 for work in self.works():
40 yield Message.Queue, work, data
41
42 def request(self, url, **kwargs):
43 response = Extractor.request(self, url, **kwargs)
44
45 if "/system/recaptcha" in response.url:
46 raise self.exc.AbortExtraction("Bot check / CAPTCHA page")
47
48 return response
49
50
51 class XfolioWorkExtractor(XfolioExtractor):
52 subcategory = "work"
53 pattern = BASE_PATTERN + r"/portfolio/([^/?#]+)/works/(\d+)"
54 example = "https://xfolio.jp/portfolio/USER/works/12345"
55
56 def items(self):
57 creator, work_id = self.groups
58 url = f"{self.root}/portfolio/{creator}/works/{work_id}"
59 html = self.request(url).text
60
61 work = self._extract_data(html)
62 files = self._extract_files(html, work)
63 work["count"] = len(files)
64
65 yield Message.Directory, "", work
66 for work["num"], file in enumerate(files, 1):
67 file.update(work)
68 yield Message.Url, file["url"], file
69
70 def _extract_data(self, html):
71 creator, work_id = self.groups
72 extr = text.extract_from(html)
73 return {
74 "title" : text.unescape(extr(
75 'property="og:title" content="', '"').rpartition(" - ")[0]),
76 "description" : text.unescape(extr(
77 'property="og:description" content="', '"')),
78 "creator_id" : extr(' data-creator-id="', '"'),
79 "creator_userid" : extr(' data-creator-user-id="', '"'),
80 "creator_name" : extr(' data-creator-name="', '"'),
81 "creator_profile": text.unescape(extr(
82 ' data-creator-profile="', '"')),
83 "series_id" : extr("/series/", '"'),
84 "creator_slug" : creator,
85 "work_id" : work_id,
86 }
87
88 def _extract_files(self, html, work):
89 work_id = work["work_id"]
90
91 if self._logged_in and self.config("fullsize", True) and \
92 (pos := html.find("-fullscale_download_")) >= 0 and \
93 text.extract(html, 'data-is-purchased="', '"', pos)[0] == "1":
94 return ({
95 "url": (f"{self.root}/user_asset.php"
96 f"?id={work_id}&type=work_zip"),
97 "extension": "zip",
98 "image_id" : 0,
99 },)
100
101 files = []
102 for img in text.extract_iter(
103 html, 'class="article__wrap_img', "</div>"):
104 if image_id := text.extr(img, "/fullscale_image?image_id=", "&"):
105 ext = "jpg"
106 url = (f"{self.root}/user_asset.php?id={image_id}"
107 f"&work_id={work_id}&work_image_id={image_id}"
108 f"&type=work_image")
109 headers = {"Referer": (
110 f"{self.root}/fullscale_image"
111 f"?image_id={image_id}&work_id={work_id}")}
112 elif url := text.extr(img, ' src="', '"'):
113 image_id = url.split("/", 5)[-2]
114 url = text.unescape(url)
115 ext = text.ext_from_url(url)
116 headers = {
117 "Referer" : self.root + "/",
118 "Sec-Fetch-Dest": "image",
119 "Sec-Fetch-Mode": "no-cors",
120 "Sec-Fetch-Site": "same-site",
121 }
122 else:
123 self.log.debug(html)
124 self.log.warning("%s: Failed to find image", work_id)
125 continue
126
127 files.append({
128 "image_id" : image_id,
129 "url" : url,
130 "extension": ext,
131 "_http_headers": headers,
132 })
133 return files
134
135
136 class XfolioUserExtractor(XfolioExtractor):
137 subcategory = "user"
138 pattern = BASE_PATTERN + r"/portfolio/([^/?#]+)(?:/works)?/?(?:$|\?|#)"
139 example = "https://xfolio.jp/portfolio/USER"
140
141 def works(self):
142 url = f"{self.root}/portfolio/{self.groups[0]}/works"
143
144 while True:
145 html = self.request(url).text
146
147 for item in text.extract_iter(
148 html, '<div class="postItem', "</div>"):
149 yield text.extr(item, ' href="', '"')
150
151 pager = text.extr(html, ' class="pager__list_next', "</li>")
152 url = text.extr(pager, ' href="', '"')
153 if not url:
154 return
155 url = text.unescape(url)
156
157
158 class XfolioSeriesExtractor(XfolioExtractor):
159 subcategory = "series"
160 pattern = BASE_PATTERN + r"/portfolio/([^/?#]+)/series/(\d+)"
161 example = "https://xfolio.jp/portfolio/USER/series/12345"
162
163 def works(self):
164 creator, series_id = self.groups
165 url = f"{self.root}/portfolio/{creator}/series/{series_id}"
166 html = self.request(url).text
167
168 return [
169 text.extr(item, ' href="', '"')
170 for item in text.extract_iter(
171 html, 'class="listWrap--title">', "</a>")
172 ]