Debian Codegraph @

gallery_dl/extractor/hotleak.py

raw
1 # -*- coding: utf-8 -*-
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2 as
5 # published by the Free Software Foundation.
6
7 """Extractors for https://hotleak.vip/"""
8
9 from .common import Extractor, Message
10 from .. import text, util
11
12 BASE_PATTERN = r"(?:https?://)?(?:www\.)?hotleak\.vip"
13
14
15 class HotleakExtractor(Extractor):
16 """Base class for hotleak extractors"""
17 category = "hotleak"
18 directory_fmt = ("{category}", "{creator}",)
19 filename_fmt = "{creator}_{id}.{extension}"
20 archive_fmt = "{type}_{creator}_{id}"
21 root = "https://hotleak.vip"
22
23 def items(self):
24 for post in self.posts():
25 if not post["url"].startswith("ytdl:"):
26 post["url"] = (
27 post["url"]
28 .replace("/storage/storage/", "/storage/")
29 .replace("_thumb.", ".")
30 )
31 post["_http_expected_status"] = (404,)
32 yield Message.Directory, "", post
33 yield Message.Url, post["url"], post
34
35 def posts(self):
36 """Return an iterable containing relevant posts"""
37 return ()
38
39 def _pagination(self, url, params):
40 params = text.parse_query(params)
41 params["page"] = text.parse_int(params.get("page"), 1)
42
43 while True:
44 page = self.request(url, params=params).text
45 if "</article>" not in page:
46 return
47
48 for item in text.extract_iter(
49 page, '<article class="movie-item', '</article>'):
50 yield text.extr(item, '<a href="', '"')
51
52 params["page"] += 1
53
54
55 def decode_video_url(url):
56 # cut first and last 16 characters, reverse, base64 decode
57 return util.b64decode(url[-17:15:-1])
58
59
60 class HotleakPostExtractor(HotleakExtractor):
61 """Extractor for individual posts on hotleak"""
62 subcategory = "post"
63 pattern = (BASE_PATTERN + r"/(?!(?:hot|creators|videos|photos)(?:$|/))"
64 r"([^/]+)/(photo|video)/(\d+)")
65 example = "https://hotleak.vip/MODEL/photo/12345"
66
67 def __init__(self, match):
68 HotleakExtractor.__init__(self, match)
69 self.creator, self.type, self.id = match.groups()
70
71 def posts(self):
72 url = f"{self.root}/{self.creator}/{self.type}/{self.id}"
73 page = self.request(url).text
74 page = text.extr(
75 page, '<div class="movie-image thumb">', '</article>')
76 data = {
77 "id" : text.parse_int(self.id),
78 "creator": self.creator,
79 "type" : self.type,
80 }
81
82 if self.type == "photo":
83 data["url"] = text.extr(page, 'data-src="', '"')
84 text.nameext_from_url(data["url"], data)
85
86 elif self.type == "video":
87 data["url"] = "ytdl:" + decode_video_url(text.extr(
88 text.unescape(page), '"src":"', '"'))
89 text.nameext_from_url(data["url"], data)
90 data["extension"] = "mp4"
91
92 return (data,)
93
94
95 class HotleakCreatorExtractor(HotleakExtractor):
96 """Extractor for all posts from a hotleak creator"""
97 subcategory = "creator"
98 pattern = (BASE_PATTERN + r"/(?!(?:hot|creators|videos|photos)(?:$|/))"
99 r"([^/?#]+)/?$")
100 example = "https://hotleak.vip/MODEL"
101
102 def __init__(self, match):
103 HotleakExtractor.__init__(self, match)
104 self.creator = match[1]
105
106 def posts(self):
107 url = f"{self.root}/{self.creator}"
108 return self._pagination(url)
109
110 def _pagination(self, url):
111 headers = {"X-Requested-With": "XMLHttpRequest"}
112 params = {"page": 1}
113
114 while True:
115 try:
116 response = self.request(
117 url, headers=headers, params=params, notfound=True)
118 except self.exc.HttpError as exc:
119 if exc.response.status_code == 429:
120 self.wait(
121 until=exc.response.headers.get("X-RateLimit-Reset"))
122 continue
123 raise
124
125 posts = response.json()
126 if not posts:
127 return
128
129 data = {"creator": self.creator}
130 for post in posts:
131 data["id"] = text.parse_int(post["id"])
132
133 if post["type"] == 0:
134 data["type"] = "photo"
135 data["url"] = self.root + "/storage/" + post["image"]
136 text.nameext_from_url(data["url"], data)
137
138 elif post["type"] == 1:
139 data["type"] = "video"
140 data["url"] = "ytdl:" + decode_video_url(
141 post["stream_url_play"])
142 text.nameext_from_url(data["url"], data)
143 data["extension"] = "mp4"
144
145 yield data
146 params["page"] += 1
147
148
149 class HotleakCategoryExtractor(HotleakExtractor):
150 """Extractor for hotleak categories"""
151 subcategory = "category"
152 pattern = BASE_PATTERN + r"/(hot|creators|videos|photos)(?:/?\?([^#]+))?"
153 example = "https://hotleak.vip/photos"
154
155 def __init__(self, match):
156 HotleakExtractor.__init__(self, match)
157 self._category, self.params = match.groups()
158
159 def items(self):
160 url = f"{self.root}/{self._category}"
161
162 if self._category in {"hot", "creators"}:
163 data = {"_extractor": HotleakCreatorExtractor}
164 elif self._category in {"videos", "photos"}:
165 data = {"_extractor": HotleakPostExtractor}
166
167 for item in self._pagination(url, self.params):
168 yield Message.Queue, item, data
169
170
171 class HotleakSearchExtractor(HotleakExtractor):
172 """Extractor for hotleak search results"""
173 subcategory = "search"
174 pattern = BASE_PATTERN + r"/search(?:/?\?([^#]+))"
175 example = "https://hotleak.vip/search?search=QUERY"
176
177 def __init__(self, match):
178 HotleakExtractor.__init__(self, match)
179 self.params = match[1]
180
181 def items(self):
182 data = {"_extractor": HotleakCreatorExtractor}
183 for creator in self._pagination(self.root + "/search", self.params):
184 yield Message.Queue, creator, data