| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for FoolFuuka 4chan archives""" |
| 10 |
|
| 11 |
from .common import BaseExtractor, Message |
| 12 |
from .. import text |
| 13 |
import itertools |
| 14 |
|
| 15 |
|
| 16 |
class FoolfuukaExtractor(BaseExtractor): |
| 17 |
"""Base extractor for FoolFuuka based boards/archives""" |
| 18 |
basecategory = "foolfuuka" |
| 19 |
filename_fmt = "{timestamp_ms} {filename_media}.{extension}" |
| 20 |
archive_fmt = "{board[shortname]}_{num}_{timestamp}" |
| 21 |
external = "default" |
| 22 |
|
| 23 |
def __init__(self, match): |
| 24 |
BaseExtractor.__init__(self, match) |
| 25 |
if self.category == "b4k": |
| 26 |
self.remote = self._remote_direct |
| 27 |
elif self.category == "archivedmoe": |
| 28 |
self.referer = False |
| 29 |
self.fixup_redirect = True |
| 30 |
else: |
| 31 |
self.fixup_redirect = False |
| 32 |
|
| 33 |
def items(self): |
| 34 |
yield Message.Directory, "", self.metadata() |
| 35 |
for post in self.posts(): |
| 36 |
if not (media := post.get("media")): |
| 37 |
continue |
| 38 |
board = post["board"]["shortname"] |
| 39 |
url = media["media_link"] |
| 40 |
|
| 41 |
if not url and "remote_media_link" in media: |
| 42 |
url = self.remote(board, media) |
| 43 |
if url and url[0] == "/": |
| 44 |
url = self.root + url |
| 45 |
|
| 46 |
post["filename"], _, post["extension"] = \ |
| 47 |
media["media"].rpartition(".") |
| 48 |
post["filename_media"] = media["media_filename"].rpartition(".")[0] |
| 49 |
post["timestamp_ms"] = text.parse_int( |
| 50 |
media["media_orig"].rpartition(".")[0]) |
| 51 |
yield Message.Url, url, post |
| 52 |
|
| 53 |
def metadata(self): |
| 54 |
"""Return general metadata""" |
| 55 |
|
| 56 |
def posts(self): |
| 57 |
"""Return an iterable with all relevant posts""" |
| 58 |
|
| 59 |
def remote(self, board, media): |
| 60 |
"""Resolve a remote media link""" |
| 61 |
if board in {"wsg", "gif"}: |
| 62 |
return f"https://i.4cdn.org/{board}/{media['media_orig']}" |
| 63 |
page = self.request(media["remote_media_link"]).text |
| 64 |
url = text.extr(page, 'http-equiv="Refresh" content="0; url=', '"') |
| 65 |
|
| 66 |
if url.startswith("https://thebarchive.com/"): |
| 67 |
|
| 68 |
if url.endswith(".webm"): |
| 69 |
url = url[:-1] |
| 70 |
|
| 71 |
elif self.fixup_redirect: |
| 72 |
|
| 73 |
path, _, filename = url.rpartition("/") |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
board_domains = { |
| 78 |
"3" : "warosu.org", |
| 79 |
"biz": "warosu.org", |
| 80 |
"ck" : "warosu.org", |
| 81 |
"diy": "warosu.org", |
| 82 |
"fa" : "warosu.org", |
| 83 |
"ic" : "warosu.org", |
| 84 |
"jp" : "warosu.org", |
| 85 |
"lit": "warosu.org", |
| 86 |
"sci": "warosu.org", |
| 87 |
"tg" : "archive.4plebs.org", |
| 88 |
} |
| 89 |
if board in board_domains: |
| 90 |
domain = board_domains[board] |
| 91 |
url = f"https://{domain}/{board}/full_image/{filename}" |
| 92 |
|
| 93 |
|
| 94 |
elif any(archive in path for archive in ( |
| 95 |
"b4k.", "desuarchive.", "palanq.")): |
| 96 |
name, _, ext = filename.rpartition(".") |
| 97 |
if len(name) > 13: |
| 98 |
url = f"{path}/{name[:13]}.{ext}" |
| 99 |
|
| 100 |
return url |
| 101 |
|
| 102 |
def _remote_direct(self, board, media): |
| 103 |
return media["remote_media_link"] |
| 104 |
|
| 105 |
|
| 106 |
BASE_PATTERN = FoolfuukaExtractor.update({ |
| 107 |
"4plebs": { |
| 108 |
"root": "https://archive.4plebs.org", |
| 109 |
"pattern": r"(?:archive\.)?4plebs\.org", |
| 110 |
}, |
| 111 |
"archivedmoe": { |
| 112 |
"root": "https://archived.moe", |
| 113 |
"pattern": r"archived\.moe", |
| 114 |
}, |
| 115 |
"archiveofsins": { |
| 116 |
"root": "https://archiveofsins.com", |
| 117 |
"pattern": r"(?:www\.)?archiveofsins\.com", |
| 118 |
}, |
| 119 |
"b4k": { |
| 120 |
"root": "https://arch.b4k.dev", |
| 121 |
"pattern": r"arch\.b4k\.(?:dev|co)", |
| 122 |
}, |
| 123 |
"desuarchive": { |
| 124 |
"root": "https://desuarchive.org", |
| 125 |
"pattern": r"desuarchive\.org", |
| 126 |
}, |
| 127 |
"fireden": { |
| 128 |
"root": "https://boards.fireden.net", |
| 129 |
"pattern": r"boards\.fireden\.net", |
| 130 |
}, |
| 131 |
"palanq": { |
| 132 |
"root": "https://archive.palanq.win", |
| 133 |
"pattern": r"archive\.palanq\.win", |
| 134 |
}, |
| 135 |
"rbt": { |
| 136 |
"root": "https://rbt.asia", |
| 137 |
"pattern": r"(?:rbt\.asia|(?:archive\.)?rebeccablacktech\.com)", |
| 138 |
}, |
| 139 |
"thebarchive": { |
| 140 |
"root": "https://thebarchive.com", |
| 141 |
"pattern": r"thebarchive\.com", |
| 142 |
}, |
| 143 |
}) |
| 144 |
|
| 145 |
|
| 146 |
class FoolfuukaThreadExtractor(FoolfuukaExtractor): |
| 147 |
"""Base extractor for threads on FoolFuuka based boards/archives""" |
| 148 |
subcategory = "thread" |
| 149 |
directory_fmt = ("{category}", "{board[shortname]}", |
| 150 |
"{thread_num} {title|comment[:50]}") |
| 151 |
pattern = BASE_PATTERN + r"/([^/?#]+)/thread/(\d+)" |
| 152 |
example = "https://archived.moe/a/thread/12345/" |
| 153 |
|
| 154 |
def __init__(self, match): |
| 155 |
FoolfuukaExtractor.__init__(self, match) |
| 156 |
self.board = self.groups[-2] |
| 157 |
self.thread = self.groups[-1] |
| 158 |
self.data = None |
| 159 |
|
| 160 |
def metadata(self): |
| 161 |
url = self.root + "/_/api/chan/thread/" |
| 162 |
params = {"board": self.board, "num": self.thread} |
| 163 |
self.data = self.request_json(url, params=params)[self.thread] |
| 164 |
return self.data["op"] |
| 165 |
|
| 166 |
def posts(self): |
| 167 |
op = (self.data["op"],) |
| 168 |
if posts := self.data.get("posts"): |
| 169 |
posts = list(posts.values()) |
| 170 |
posts.sort(key=lambda p: p["timestamp"]) |
| 171 |
return itertools.chain(op, posts) |
| 172 |
return op |
| 173 |
|
| 174 |
|
| 175 |
class FoolfuukaBoardExtractor(FoolfuukaExtractor): |
| 176 |
"""Base extractor for FoolFuuka based boards/archives""" |
| 177 |
subcategory = "board" |
| 178 |
pattern = BASE_PATTERN + r"/([^/?#]+)(?:/(?:page/)?(\d*))?$" |
| 179 |
example = "https://archived.moe/a/" |
| 180 |
|
| 181 |
def __init__(self, match): |
| 182 |
FoolfuukaExtractor.__init__(self, match) |
| 183 |
self.board = self.groups[-2] |
| 184 |
self.page = self.groups[-1] |
| 185 |
|
| 186 |
def items(self): |
| 187 |
index_base = f"{self.root}/_/api/chan/index/?board={self.board}&page=" |
| 188 |
thread_base = f"{self.root}/{self.board}/thread/" |
| 189 |
|
| 190 |
page = self.page |
| 191 |
for pnum in itertools.count(text.parse_int(page, 1)): |
| 192 |
with self.request(index_base + str(pnum)) as response: |
| 193 |
try: |
| 194 |
threads = response.json() |
| 195 |
except ValueError: |
| 196 |
threads = None |
| 197 |
|
| 198 |
if not threads: |
| 199 |
return |
| 200 |
|
| 201 |
for num, thread in threads.items(): |
| 202 |
thread["url"] = thread_base + format(num) |
| 203 |
thread["_extractor"] = FoolfuukaThreadExtractor |
| 204 |
yield Message.Queue, thread["url"], thread |
| 205 |
|
| 206 |
if page: |
| 207 |
return |
| 208 |
|
| 209 |
|
| 210 |
class FoolfuukaSearchExtractor(FoolfuukaExtractor): |
| 211 |
"""Base extractor for search results on FoolFuuka based boards/archives""" |
| 212 |
subcategory = "search" |
| 213 |
directory_fmt = ("{category}", "search", "{search}") |
| 214 |
pattern = BASE_PATTERN + r"/([^/?#]+)/search((?:/[^/?#]+/[^/?#]+)+)" |
| 215 |
example = "https://archived.moe/_/search/text/QUERY/" |
| 216 |
request_interval = (0.5, 1.5) |
| 217 |
|
| 218 |
def __init__(self, match): |
| 219 |
FoolfuukaExtractor.__init__(self, match) |
| 220 |
self.params = params = {} |
| 221 |
|
| 222 |
key = None |
| 223 |
for arg in self.groups[-1].split("/"): |
| 224 |
if key: |
| 225 |
params[key] = text.unescape(arg) |
| 226 |
key = None |
| 227 |
else: |
| 228 |
key = arg |
| 229 |
|
| 230 |
board = self.groups[-2] |
| 231 |
if board != "_": |
| 232 |
params["boards"] = board |
| 233 |
|
| 234 |
def metadata(self): |
| 235 |
return {"search": self.params.get("text", "")} |
| 236 |
|
| 237 |
def posts(self): |
| 238 |
url = self.root + "/_/api/chan/search/" |
| 239 |
params = self.params.copy() |
| 240 |
params["page"] = text.parse_int(params.get("page"), 1) |
| 241 |
if "filter" not in params: |
| 242 |
params["filter"] = "text" |
| 243 |
|
| 244 |
while True: |
| 245 |
try: |
| 246 |
data = self.request_json(url, params=params) |
| 247 |
except ValueError: |
| 248 |
return |
| 249 |
|
| 250 |
if isinstance(data, dict): |
| 251 |
if data.get("error"): |
| 252 |
return |
| 253 |
posts = data["0"]["posts"] |
| 254 |
elif isinstance(data, list): |
| 255 |
posts = data[0]["posts"] |
| 256 |
else: |
| 257 |
return |
| 258 |
|
| 259 |
yield from posts |
| 260 |
if len(posts) <= 3: |
| 261 |
return |
| 262 |
params["page"] += 1 |
| 263 |
|
| 264 |
|
| 265 |
class FoolfuukaGalleryExtractor(FoolfuukaExtractor): |
| 266 |
"""Base extractor for FoolFuuka galleries""" |
| 267 |
subcategory = "gallery" |
| 268 |
directory_fmt = ("{category}", "{board}", "gallery") |
| 269 |
pattern = BASE_PATTERN + r"/([^/?#]+)/gallery(?:/(\d+))?" |
| 270 |
example = "https://archived.moe/a/gallery" |
| 271 |
|
| 272 |
def metadata(self): |
| 273 |
self.board = board = self.groups[-2] |
| 274 |
return {"board": board} |
| 275 |
|
| 276 |
def posts(self): |
| 277 |
pnum = self.groups[-1] |
| 278 |
pages = itertools.count(1) if pnum is None else (pnum,) |
| 279 |
base = f"{self.root}/_/api/chan/gallery/?board={self.board}&page=" |
| 280 |
|
| 281 |
for pnum in pages: |
| 282 |
posts = self.request_json(base + str(pnum)) |
| 283 |
if not posts: |
| 284 |
return |
| 285 |
yield from posts |