Debian Codegraph @

gallery_dl/extractor/slickpic.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2019-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.slickpic.com/"""
10
11 from .common import Extractor, Message
12 from .. import text
13 import time
14
15 BASE_PATTERN = r"(?:https?://)?([\w-]+)\.slickpic\.com"
16
17
18 class SlickpicExtractor(Extractor):
19 """Base class for slickpic extractors"""
20 category = "slickpic"
21
22 def __init__(self, match):
23 Extractor.__init__(self, match)
24 self.user = match[1]
25 self.root = f"https://{self.user}.slickpic.com"
26
27
28 class SlickpicAlbumExtractor(SlickpicExtractor):
29 """Extractor for albums on slickpic.com"""
30 subcategory = "album"
31 directory_fmt = ("{category}", "{user[name]}",
32 "{album[id]} {album[title]}")
33 filename_fmt = "{num:>03}_{id}{title:?_//}.{extension}"
34 archive_fmt = "{id}"
35 pattern = BASE_PATTERN + r"/albums/([^/?#]+)"
36 example = "https://USER.slickpic.com/albums/TITLE/"
37
38 def __init__(self, match):
39 SlickpicExtractor.__init__(self, match)
40 self.album = match[2]
41
42 def items(self):
43 data = self.metadata()
44 imgs = self.images(data)
45
46 data = {
47 "album": {
48 "id" : text.parse_int(data["aid"]),
49 "title": text.unescape(data["title"]),
50 },
51 "user": {
52 "id" : text.parse_int(data["uid"]),
53 "name": text.unescape(data["user"]),
54 "nick": self.user
55 },
56 "count": len(imgs),
57 }
58
59 yield Message.Directory, "", data
60 for num, img in enumerate(imgs, 1):
61 url = img["url_rsz"] + "/o/" + img["fname"]
62 img = text.nameext_from_url(img["fname"], {
63 "url" : url,
64 "num" : num,
65 "id" : text.parse_int(img["id"]),
66 "width" : text.parse_int(img["width"]),
67 "height" : text.parse_int(img["height"]),
68 "title" : img["title"],
69 "description": img["descr"],
70 })
71 img.update(data)
72 yield Message.Url, url, img
73
74 def metadata(self):
75 url = f"{self.root}/albums/{self.album}/?wallpaper"
76 extr = text.extract_from(self.request(url).text)
77
78 title = text.unescape(extr("<title>", "</title>"))
79 title, _, user = title.rpartition(" by ")
80
81 return {
82 "title": title,
83 "user" : user,
84 "tk" : extr('tk = "', '"'),
85 "shd" : extr('shd = "', '"'),
86 "aid" : extr('data-aid="', '"', ),
87 "uid" : extr('data-uid="', '"', ),
88 }
89
90 def images(self, data):
91 url = self.root + "/xhr/photo/get/list"
92 data = {
93 "tm" : time.time(),
94 "tk" : data["tk"],
95 "shd" : data["shd"],
96 "aid" : data["aid"],
97 "uid" : data["uid"],
98 "col" : "0",
99 "sys" : self.album,
100 "vw" : "1280",
101 "vh" : "1024",
102 "skey" : "",
103 "viewer": "false",
104 "pub" : "1",
105 "sng" : "0",
106 "whq" : "1",
107 }
108 return self.request_json(url, method="POST", data=data)["list"]
109
110
111 class SlickpicUserExtractor(SlickpicExtractor):
112 subcategory = "user"
113 pattern = BASE_PATTERN + r"(?:/gallery)?/?(?:$|[?#])"
114 example = "https://USER.slickpic.com/"
115
116 def items(self):
117 page = self.request(self.root + "/gallery?viewer").text
118 data = {"_extractor": SlickpicAlbumExtractor}
119 base = self.root + "/albums/"
120
121 for album in text.extract_iter(page, 'href="' + base, '"'):
122 yield Message.Queue, base + album, data