Debian Codegraph @

gallery_dl/extractor/erome.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2021-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://www.erome.com/"""
10
11 from .common import Extractor, Message
12 from .. import text, util
13 import itertools
14
15 BASE_PATTERN = r"(?:https?://)?(?:www\.)?erome\.com"
16
17
18 class EromeExtractor(Extractor):
19 category = "erome"
20 directory_fmt = ("{category}", "{user}")
21 filename_fmt = "{album_id} {title} {num:>02}.{extension}"
22 archive_fmt = "{album_id}_{num}"
23 root = "https://www.erome.com"
24 parent = True
25 _cookies = True
26
27 def items(self):
28 base = self.root + "/a/"
29 data = {"_extractor": EromeAlbumExtractor}
30 for album_id in self.albums():
31 yield Message.Queue, base + album_id, data
32
33 def albums(self):
34 return ()
35
36 def request(self, url, **kwargs):
37 if self._cookies:
38 self._cookies = False
39 self.cookies_update(self.cache(
40 _cookie_cache, _key=None, _mem=False))
41
42 for _ in range(5):
43 response = Extractor.request(self, url, **kwargs)
44 if response.cookies:
45 self.cache_update(_cookie_cache, None, response.cookies)
46 if response.content.find(
47 b"<title>Please wait a few moments</title>", 0, 600) < 0:
48 return response
49 self.sleep(5.0, "check")
50
51 def _pagination(self, url, params):
52 find_albums = EromeAlbumExtractor.pattern.findall
53
54 for params["page"] in itertools.count(
55 text.parse_int(params.get("page"), 1)):
56 page = self.request(url, params=params).text
57
58 album_ids = find_albums(page)[::2]
59 yield from album_ids
60
61 if len(album_ids) < 36:
62 return
63
64
65 class EromeAlbumExtractor(EromeExtractor):
66 """Extractor for albums on erome.com"""
67 subcategory = "album"
68 pattern = BASE_PATTERN + r"/a/(\w+)"
69 example = "https://www.erome.com/a/ID"
70
71 def items(self):
72 album_id = self.groups[0]
73 url = f"{self.root}/a/{album_id}"
74
75 try:
76 page = self.request(url).text
77 except self.exc.HttpError as exc:
78 if exc.status == 410:
79 msg = text.extr(exc.response.text, "<h1>", "<")
80 else:
81 msg = "Unable to fetch album page"
82 raise self.exc.AbortExtraction(
83 f"{album_id}: {msg} ({exc})")
84
85 title, pos = text.extract(
86 page, 'property="og:title" content="', '"')
87 pos = page.index('<div class="user-profile', pos)
88 user, pos = text.extract(
89 page, 'href="https://www.erome.com/', '"', pos)
90 tags, pos = text.extract(
91 page, '<p class="mt-10"', '</p>', pos)
92
93 urls = []
94 date = None
95 groups = page.split('<div class="media-group"')
96 for group in util.advance(groups, 1):
97 url = (text.extr(group, '<source src="', '"') or
98 text.extr(group, 'data-src="', '"'))
99 if url:
100 urls.append(url)
101 if not date:
102 ts = text.extr(group, '?v=', '"')
103 if len(ts) > 1:
104 date = self.parse_timestamp(ts)
105
106 data = {
107 "album_id": album_id,
108 "title" : text.unescape(title),
109 "user" : text.unquote(user),
110 "count" : len(urls),
111 "date" : date,
112 "tags" : ([t.replace("+", " ")
113 for t in text.extract_iter(tags, "?q=", '"')]
114 if tags else ()),
115 "_http_headers": {"Referer": url},
116 }
117
118 yield Message.Directory, "", data
119 for data["num"], url in enumerate(urls, 1):
120 yield Message.Url, url, text.nameext_from_url(url, data)
121
122
123 class EromeUserExtractor(EromeExtractor):
124 subcategory = "user"
125 pattern = BASE_PATTERN + r"/(?!a/|search\?)([^/?#]+)(?:/?\?([^#]+))?"
126 example = "https://www.erome.com/USER"
127
128 def albums(self):
129 user, qs = self.groups
130 url = f"{self.root}/{user}"
131
132 params = text.parse_query(qs)
133 if "t" not in params and not self.config("reposts", False):
134 params["t"] = "posts"
135
136 return self._pagination(url, params)
137
138
139 class EromeSearchExtractor(EromeExtractor):
140 subcategory = "search"
141 pattern = BASE_PATTERN + r"/search/?\?(q=[^#]+)"
142 example = "https://www.erome.com/search?q=QUERY"
143
144 def albums(self):
145 url = self.root + "/search"
146 params = text.parse_query(self.groups[0])
147 return self._pagination(url, params)
148
149
150 def _cookie_cache():
151 return ()