Debian Codegraph @

gallery_dl/extractor/postype.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://www.postype.com/"""
8
9 from .common import Extractor, Message
10 from .. import text
11
12 BASE_PATTERN = r"(?:https?://)?(?:www\.)?postype\.com(?:/[a-z]{2})?"
13
14
15 class PostypeExtractor(Extractor):
16 """Base class for postype extractors"""
17 category = "postype"
18 root = "https://www.postype.com"
19 root_api = "https://api.postype.com/api"
20 directory_fmt = ("{category}", "{channel[name]}")
21 filename_fmt = "{post_id}_{num:>03}.{extension}"
22 archive_fmt = "{post_id}_{num}"
23 request_interval = (1.0, 2.0)
24
25 def items(self):
26 for post in self.posts():
27 post = self._prepare(post)
28 images = self._extract_images(post)
29 post["count"] = len(images)
30
31 yield Message.Directory, "", post
32 for post["num"], image in enumerate(images, 1):
33 post.update(image)
34 url = image["url"]
35 yield Message.Url, url, text.nameext_from_url(url, post)
36
37 def request_api(self, endpoint, params=None):
38 return self.request_json(self.root_api + endpoint, params=params)
39
40 def _prepare(self, post):
41 post["date"] = self.parse_timestamp(post["publishedAt"])
42 post["views"] = post.pop("viewCount", None)
43 post["views"] = post.pop("viewCount", None)
44 post["likes"] = post.pop("likeCount", None)
45 post["post_id"] = post.pop("postId", 0)
46 post["comments"] = post.pop("commentCount", None)
47 post.pop("likeProfileAvatars", None)
48 post.pop("prevPost", None)
49 post.pop("nextPost", None)
50
51 if "channelName" in post["channel"]:
52 post["channel"]["name"] = post["channel"].pop("channelName")
53
54 return post
55
56 def _extract_images(self, post):
57 """Extract image URLs from post HTML content"""
58 data = self.request_api("/v1/post/content/" + str(post["post_id"]))
59 html = data["data"]["html"]
60
61 images = []
62 seen = set()
63 pos = 0
64
65 while True:
66 pos = html.find('data-full-path="', pos) + 16
67 if pos < 16:
68 break
69
70 url = text.unescape(
71 html[pos:html.find('"', pos)]).partition("?")[0]
72 if url in seen:
73 continue
74 seen.add(url)
75
76 ctx = text.rextr(html, "<", ">", pos)
77 images.append({
78 "url" : url,
79 "width" : text.parse_int(text.extr(ctx, 'data-width="', '"')),
80 "height": text.parse_int(text.extr(ctx, 'data-height="', '"')),
81 })
82
83 return images
84
85
86 class PostypePostExtractor(PostypeExtractor):
87 """Extractor for a single postype post"""
88 subcategory = "post"
89 pattern = BASE_PATTERN + r"/@[^/?#]+/post/(\d+)"
90 example = "https://www.postype.com/@USER/post/12345"
91
92 def posts(self):
93 return (self.request_api("/v1/posts/" + self.groups[0]),)
94
95
96 class PostypeChannelExtractor(PostypeExtractor):
97 """Extractor for all posts of a postype channel"""
98 subcategory = "channel"
99 pattern = BASE_PATTERN + r"/@([^/?#]+)/?$"
100 example = "https://www.postype.com/@USER"
101
102 def posts(self):
103 channel = self.request_api(
104 "/v1/channels/by/channel-name/" + self.groups[0])
105 return self._pagination(channel["channelId"])
106
107 def _pagination(self, channel_id):
108 endpoint = f"/v2/channel/{channel_id}/activity/all"
109 params = {"page": 0}
110
111 while True:
112 data = self.request_api(endpoint, params)
113
114 for item in data["content"]:
115 if item["type"] == "POST":
116 yield item["feedItem"]
117
118 if data.get("last", True):
119 break
120 params["page"] += 1