| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
"""Extractors for Postmill instances""" |
| 8 |
|
| 9 |
from .common import BaseExtractor, Message |
| 10 |
from .. import text |
| 11 |
|
| 12 |
|
| 13 |
class PostmillExtractor(BaseExtractor): |
| 14 |
"""Base class for Postmill extractors""" |
| 15 |
basecategory = "postmill" |
| 16 |
directory_fmt = ("{category}", "{instance}", "{forum}") |
| 17 |
filename_fmt = "{id}_{title[:220]}.{extension}" |
| 18 |
archive_fmt = "{filename}" |
| 19 |
|
| 20 |
def _init(self): |
| 21 |
self.instance = self.root.partition("://")[2] |
| 22 |
self.save_link_post_body = self.config("save-link-post-body", False) |
| 23 |
self._search_canonical_url = text.re(r"/f/([\w\d_]+)/(\d+)/").search |
| 24 |
self._search_image_tag = text.re( |
| 25 |
r'<a href="[^"]+"\n +class="submission__image-link"').search |
| 26 |
|
| 27 |
def items(self): |
| 28 |
for post_url in self.post_urls(): |
| 29 |
page = self.request(post_url).text |
| 30 |
extr = text.extract_from(page) |
| 31 |
|
| 32 |
title = text.unescape(extr( |
| 33 |
'<meta property="og:title" content="', '">')) |
| 34 |
date = self.parse_datetime_iso(extr( |
| 35 |
'<meta property="og:article:published_time" content="', '">')) |
| 36 |
username = extr( |
| 37 |
'<meta property="og:article:author" content="', '">') |
| 38 |
post_canonical_url = text.unescape(extr( |
| 39 |
'<link rel="canonical" href="', '">')) |
| 40 |
|
| 41 |
url = text.unescape(extr( |
| 42 |
'<h1 class="submission__title unheaderize inline"><a href="', |
| 43 |
'"')) |
| 44 |
body = extr( |
| 45 |
'<div class="submission__body break-text text-flow">', |
| 46 |
'</div>') |
| 47 |
|
| 48 |
match = self._search_canonical_url(post_canonical_url) |
| 49 |
forum = match[1] |
| 50 |
id = int(match[2]) |
| 51 |
|
| 52 |
is_text_post = (url[0] == "/") |
| 53 |
is_image_post = self._search_image_tag(page) is not None |
| 54 |
data = { |
| 55 |
"title": title, |
| 56 |
"date": date, |
| 57 |
"username": username, |
| 58 |
"forum": forum, |
| 59 |
"id": id, |
| 60 |
"flair": [text.unescape(i) for i in text.extract_iter( |
| 61 |
page, '<span class="flair__label">', '</span>')], |
| 62 |
"instance": self.instance, |
| 63 |
} |
| 64 |
|
| 65 |
urls = [] |
| 66 |
if is_text_post or self.save_link_post_body: |
| 67 |
urls.append((Message.Url, "text:" + body)) |
| 68 |
|
| 69 |
if is_image_post: |
| 70 |
urls.append((Message.Url, url)) |
| 71 |
elif not is_text_post: |
| 72 |
urls.append((Message.Queue, url)) |
| 73 |
|
| 74 |
data["count"] = len(urls) |
| 75 |
yield Message.Directory, "", data |
| 76 |
for data["num"], (msg, url) in enumerate(urls, 1): |
| 77 |
if url.startswith("text:"): |
| 78 |
data["filename"], data["extension"] = "", "htm" |
| 79 |
else: |
| 80 |
data = text.nameext_from_url(url, data) |
| 81 |
|
| 82 |
yield msg, url, data |
| 83 |
|
| 84 |
|
| 85 |
class PostmillSubmissionsExtractor(PostmillExtractor): |
| 86 |
"""Base class for Postmill submissions extractors""" |
| 87 |
whitelisted_parameters = () |
| 88 |
|
| 89 |
def __init__(self, match): |
| 90 |
PostmillExtractor.__init__(self, match) |
| 91 |
groups = match.groups() |
| 92 |
self.base = groups[-3] |
| 93 |
self.sorting_path = groups[-2] or "" |
| 94 |
self.query = {key: value for key, value in text.parse_query( |
| 95 |
groups[-1]).items() if self.acceptable_query(key)} |
| 96 |
|
| 97 |
def items(self): |
| 98 |
url = f"{self.root}{self.base}{self.sorting_path}" |
| 99 |
|
| 100 |
while url: |
| 101 |
response = self.request(url, params=self.query) |
| 102 |
if response.history: |
| 103 |
redirect_url = response.url |
| 104 |
if redirect_url == self.root + "/login": |
| 105 |
raise self.exc.AbortExtraction( |
| 106 |
f"HTTP redirect to login page ({redirect_url})") |
| 107 |
page = response.text |
| 108 |
|
| 109 |
for nav in text.extract_iter(page, |
| 110 |
'<nav class="submission__nav">', |
| 111 |
'</nav>'): |
| 112 |
post_url = text.unescape(text.extr(nav, '<a href="', '"')) |
| 113 |
yield Message.Queue, text.urljoin(url, post_url), \ |
| 114 |
{"_extractor": PostmillPostExtractor} |
| 115 |
|
| 116 |
url = text.unescape(text.extr(page, |
| 117 |
'<link rel="next" href="', '">')) |
| 118 |
|
| 119 |
def acceptable_query(self, key): |
| 120 |
return key in self.whitelisted_parameters or key == "t" or \ |
| 121 |
(key.startswith("next[") and key.endswith("]")) |
| 122 |
|
| 123 |
|
| 124 |
BASE_PATTERN = PostmillExtractor.update({ |
| 125 |
"raddle": { |
| 126 |
"root" : None, |
| 127 |
"pattern": (r"(?:raddle\.me|" |
| 128 |
r"c32zjeghcp5tj3kb72pltz56piei66drc63vkhn5yixiyk4cmerrjtid" |
| 129 |
r"\.onion)"), |
| 130 |
} |
| 131 |
}) |
| 132 |
QUERY_RE = r"(?:\?([^#]+))?$" |
| 133 |
SORTING_RE = (r"(/(?:hot|new|active|top|controversial|most_commented))?" + |
| 134 |
QUERY_RE) |
| 135 |
|
| 136 |
|
| 137 |
class PostmillPostExtractor(PostmillExtractor): |
| 138 |
"""Extractor for a single submission URL""" |
| 139 |
subcategory = "post" |
| 140 |
pattern = BASE_PATTERN + r"/f/(\w+)/(\d+)" |
| 141 |
example = "https://raddle.me/f/FORUM/123/TITLE" |
| 142 |
|
| 143 |
def __init__(self, match): |
| 144 |
PostmillExtractor.__init__(self, match) |
| 145 |
self.forum = match[3] |
| 146 |
self.post_id = match[4] |
| 147 |
|
| 148 |
def post_urls(self): |
| 149 |
return (f"{self.root}/f/{self.forum}/{self.post_id}",) |
| 150 |
|
| 151 |
|
| 152 |
class PostmillShortURLExtractor(PostmillExtractor): |
| 153 |
"""Extractor for short submission URLs""" |
| 154 |
subcategory = "shorturl" |
| 155 |
pattern = BASE_PATTERN + r"(/\d+)$" |
| 156 |
example = "https://raddle.me/123" |
| 157 |
|
| 158 |
def items(self): |
| 159 |
url = self.root + self.groups[2] |
| 160 |
location = self.request_location(url) |
| 161 |
full_url = text.urljoin(url, location) |
| 162 |
yield Message.Queue, full_url, {"_extractor": PostmillPostExtractor} |
| 163 |
|
| 164 |
|
| 165 |
class PostmillHomeExtractor(PostmillSubmissionsExtractor): |
| 166 |
"""Extractor for the home page""" |
| 167 |
subcategory = "home" |
| 168 |
pattern = rf"{BASE_PATTERN}(/(?:featured|subscribed|all)?){SORTING_RE}" |
| 169 |
example = "https://raddle.me/" |
| 170 |
|
| 171 |
|
| 172 |
class PostmillForumExtractor(PostmillSubmissionsExtractor): |
| 173 |
"""Extractor for submissions on a forum""" |
| 174 |
subcategory = "forum" |
| 175 |
pattern = rf"{BASE_PATTERN}(/f/\w+){SORTING_RE}" |
| 176 |
example = "https://raddle.me/f/FORUM" |
| 177 |
|
| 178 |
|
| 179 |
class PostmillUserSubmissionsExtractor(PostmillSubmissionsExtractor): |
| 180 |
"""Extractor for submissions made by a user""" |
| 181 |
subcategory = "usersubmissions" |
| 182 |
pattern = rf"{BASE_PATTERN}(/user/\w+/submissions)(){QUERY_RE}" |
| 183 |
example = "https://raddle.me/user/USER/submissions" |
| 184 |
|
| 185 |
|
| 186 |
class PostmillTagExtractor(PostmillSubmissionsExtractor): |
| 187 |
"""Extractor for submissions on a forum with a specific tag""" |
| 188 |
subcategory = "tag" |
| 189 |
pattern = rf"{BASE_PATTERN}(/tag/\w+){SORTING_RE}" |
| 190 |
example = "https://raddle.me/tag/TAG" |
| 191 |
|
| 192 |
|
| 193 |
class PostmillSearchExtractor(PostmillSubmissionsExtractor): |
| 194 |
"""Extractor for search results""" |
| 195 |
subcategory = "search" |
| 196 |
pattern = BASE_PATTERN + r"(/search)()\?(q=[^#]+)$" |
| 197 |
example = "https://raddle.me/search?q=QUERY" |
| 198 |
whitelisted_parameters = ("q",) |