Debian Codegraph @

gallery_dl/extractor/leakgallery.py

raw symbols SCIP
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://leakgallery.com"""
8
9 from .common import Extractor, Message
10 from .. import text
11
12 BASE_PATTERN = r"(?:https?://)?(?:www\.)?leakgallery\.com"
13
14
15 class LeakgalleryExtractor(Extractor):
16 category = "leakgallery"
17 directory_fmt = ("{category}", "{creator}")
18 filename_fmt = "{id}_{filename}.{extension}"
19 archive_fmt = "{creator}_{id}"
20
21 def _yield_media_items(self, medias, creator=None):
22 seen = set()
23 for media in medias:
24 path = media["file_path"]
25 if path in seen:
26 continue
27 seen.add(path)
28
29 if creator is None:
30 try:
31 media["creator"] = \
32 media["profile"]["username"] or "unknown"
33 except Exception:
34 media["creator"] = "unknown"
35 else:
36 media["creator"] = creator
37
38 media["url"] = url = "https://cdn.leakgallery.com/" + path
39 text.nameext_from_url(url, media)
40 yield Message.Directory, "", media
41 yield Message.Url, url, media
42
43 def _pagination(self, type, base, params=None, creator=None, pnum=1):
44 while True:
45 try:
46 data = self.request_json(base + str(pnum), params=params)
47
48 if not data:
49 return
50 if "medias" in data:
51 data = data["medias"]
52 if not data or not isinstance(data, list):
53 return
54
55 yield from self._yield_media_items(data, creator)
56 pnum += 1
57 except Exception as exc:
58 self.log.error("Failed to retrieve %s page %s: %s",
59 type, pnum, exc)
60 return
61
62
63 class LeakgalleryUserExtractor(LeakgalleryExtractor):
64 """Extractor for profile posts on leakgallery.com"""
65 subcategory = "user"
66 pattern = (
67 BASE_PATTERN +
68 r"/(?!trending-medias|most-liked|random/medias)([^/?#]+)"
69 r"(?:/(Photos|Videos|All))?"
70 r"(?:/(MostRecent|MostViewed|MostLiked))?/?$"
71 )
72 example = "https://leakgallery.com/creator"
73
74 def items(self):
75 creator, mtype, msort = self.groups
76 base = f"https://api.leakgallery.com/profile/{creator}/"
77 params = {"type": mtype or "All", "sort": msort or "MostRecent"}
78 return self._pagination(creator, base, params, creator)
79
80
81 class LeakgalleryTrendingExtractor(LeakgalleryExtractor):
82 """Extractor for trending posts on leakgallery.com"""
83 subcategory = "trending"
84 pattern = BASE_PATTERN + r"/trending-medias(?:/([\w-]+))?"
85 example = "https://leakgallery.com/trending-medias/Week"
86
87 def items(self):
88 period = self.groups[0] or "Last-Hour"
89 base = f"https://api.leakgallery.com/popular/media/{period}/"
90 return self._pagination("trending", base)
91
92
93 class LeakgalleryMostlikedExtractor(LeakgalleryExtractor):
94 """Extractor for most liked posts on leakgallery.com"""
95 subcategory = "mostliked"
96 pattern = BASE_PATTERN + r"/most-liked"
97 example = "https://leakgallery.com/most-liked"
98
99 def items(self):
100 base = "https://api.leakgallery.com/most-liked/"
101 return self._pagination("most-liked", base)
102
103
104 class LeakgalleryPostExtractor(LeakgalleryExtractor):
105 """Extractor for individual posts on leakgallery.com"""
106 subcategory = "post"
107 pattern = BASE_PATTERN + r"/([^/?#]+)/(\d+)"
108 example = "https://leakgallery.com/CREATOR/12345"
109
110 def items(self):
111 creator, post_id = self.groups
112 url = f"https://leakgallery.com/{creator}/{post_id}"
113
114 try:
115 page = self.request(url).text
116 video_urls = text.re(
117 r"https://cdn\.leakgallery\.com/content[^/?#]*/"
118 r"(?!profile_pic/)[^/?#]+/"
119 r"(?:compressed_)?watermark_[^\"]+\."
120 r"(?:mp4|mov|m4a|webm)"
121 ).findall(page)
122 image_urls = text.re(
123 r"https://cdn\.leakgallery\.com/content[^/?#]*/"
124 r"(?!profile_pic/)[^/?#]+/"
125 r"watermark_[^\"]+\.(?:jpe?g|png)"
126 ).findall(page)
127
128 seen = set()
129 for url in video_urls + image_urls:
130 if url in seen:
131 continue
132 seen.add(url)
133 data = {
134 "id": post_id,
135 "creator": creator,
136 "url": url,
137 }
138 text.nameext_from_url(url, data)
139 yield Message.Directory, "", data
140 yield Message.Url, url, data
141 except Exception as exc:
142 self.log.error("Failed to extract post page %s/%s: %s",
143 creator, post_id, exc)