Debian Codegraph @

gallery_dl/extractor/pornpics.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2023-2025 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.pornpics.com/"""
10
11 from .common import GalleryExtractor, Extractor, Message
12 from .. import text
13
14 BASE_PATTERN = r"(?:https?://)?(?:www\.)?pornpics\.com(?:/\w\w)?"
15
16
17 class PornpicsExtractor(Extractor):
18 """Base class for pornpics extractors"""
19 category = "pornpics"
20 root = "https://www.pornpics.com"
21 request_interval = (0.5, 1.5)
22
23 def items(self):
24 for gallery in self.galleries():
25 gallery["_extractor"] = PornpicsGalleryExtractor
26 yield Message.Queue, gallery["g_url"], gallery
27
28 def _pagination(self, url, params=None):
29 if params is None:
30 # fetch first 20 galleries from HTML
31 # since '"offset": 0' does not return a JSON response
32 page = self.request(url).text
33 for href in text.extract_iter(
34 page, 'class="rel-link" href="', '"'):
35 if href[0] == "/":
36 href = self.root + href
37 yield {"g_url": href}
38 del page
39 params = {"offset": 20}
40
41 limit = params["limit"] = 20
42 limit //= 2
43
44 headers = {
45 "Accept": "application/json, text/javascript, */*; q=0.01",
46 "Referer": url if params["offset"] else self.root + "/",
47 "X-Requested-With": "XMLHttpRequest",
48 }
49
50 while True:
51 galleries = self.request_json(
52 url, params=params, headers=headers)
53 yield from galleries
54
55 if len(galleries) < limit:
56 return
57 params["offset"] += limit
58
59
60 class PornpicsGalleryExtractor(PornpicsExtractor, GalleryExtractor):
61 """Extractor for pornpics galleries"""
62 pattern = BASE_PATTERN + r"/galleries/((?:[^/?#]+-)?(\d+))"
63 example = "https://www.pornpics.com/galleries/TITLE-12345/"
64
65 def __init__(self, match):
66 url = f"{self.root}/galleries/{match[1]}/"
67 GalleryExtractor.__init__(self, match, url)
68
69 items = GalleryExtractor.items
70
71 def metadata(self, page):
72 extr = text.extract_from(page)
73
74 return {
75 "gallery_id": text.parse_int(self.groups[1]),
76 "slug" : extr("/galleries/", "/").rpartition("-")[0],
77 "title" : text.unescape(extr("<h1>", "<")),
78 "channel" : text.split_html(extr(">Channel:&nbsp;", '</div>')),
79 "models" : text.split_html(extr(
80 ">Models:", '<span class="suggest')),
81 "categories": text.split_html(extr(
82 ">Categories:", '<span class="suggest')),
83 "tags" : text.split_html(extr(
84 ">Tags List:", ' </div>')),
85 "views" : text.parse_int(extr(">Views:", "<").replace(",", "")),
86 }
87
88 def images(self, page):
89 return [
90 (url, None)
91 for url in text.extract_iter(page, "class='rel-link' href='", "'")
92 ]
93
94
95 class PornpicsTagExtractor(PornpicsExtractor):
96 """Extractor for galleries from pornpics tag searches"""
97 subcategory = "tag"
98 pattern = BASE_PATTERN + r"/tags/([^/?#]+)"
99 example = "https://www.pornpics.com/tags/TAGS/"
100
101 def galleries(self):
102 url = f"{self.root}/tags/{self.groups[0]}/"
103 return self._pagination(url)
104
105
106 class PornpicsSearchExtractor(PornpicsExtractor):
107 """Extractor for galleries from pornpics search results"""
108 subcategory = "search"
109 pattern = BASE_PATTERN + r"/(?:\?q=|pornstars/|channels/)([^/&#]+)"
110 example = "https://www.pornpics.com/?q=QUERY"
111
112 def galleries(self):
113 url = self.root + "/search/srch.php"
114 params = {
115 "q" : self.groups[0].replace("-", " "),
116 "lang" : "en",
117 "offset": 0,
118 }
119 return self._pagination(url, params)
120
121
122 class PornpicsListingExtractor(PornpicsExtractor):
123 """Extractor for galleries from pornpics listing pages
124
125 These pages (popular, recent, etc.) don't support JSON pagination
126 and use single quotes in HTML, unlike category pages.
127 """
128 subcategory = "listing"
129 pattern = (BASE_PATTERN +
130 r"/(popular|recent|rating|likes|views|comments)/?$")
131 example = "https://www.pornpics.com/popular/"
132
133 def galleries(self):
134 url = f"{self.root}/{self.groups[0]}/"
135 page = self.request(url).text
136 return [
137 {"g_url": href}
138 for href in text.extract_iter(
139 page, "class='rel-link' href='", "'")
140 ]
141
142
143 class PornpicsCategoryExtractor(PornpicsExtractor):
144 """Extractor for galleries from pornpics categories"""
145 subcategory = "category"
146 pattern = BASE_PATTERN + r"/([^/?#]+)/?$"
147 example = "https://www.pornpics.com/ass/"
148
149 def galleries(self):
150 url = f"{self.root}/{self.groups[0]}/"
151 return self._pagination(url)