Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/gallery_dl/extractor/4archive.py
5399 views
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://4archive.org/"""
8
9
from .common import Extractor, Message
10
from .. import text, util
11
12
13
class _4archiveThreadExtractor(Extractor):
14
"""Extractor for 4archive threads"""
15
category = "4archive"
16
subcategory = "thread"
17
directory_fmt = ("{category}", "{board}", "{thread} {title}")
18
filename_fmt = "{no} {filename}.{extension}"
19
archive_fmt = "{board}_{thread}_{no}"
20
root = "https://4archive.org"
21
referer = False
22
pattern = r"(?:https?://)?4archive\.org/board/([^/?#]+)/thread/(\d+)"
23
example = "https://4archive.org/board/a/thread/12345/"
24
25
def __init__(self, match):
26
Extractor.__init__(self, match)
27
self.board, self.thread = match.groups()
28
29
def items(self):
30
url = f"{self.root}/board/{self.board}/thread/{self.thread}"
31
page = self.request(url).text
32
data = self.metadata(page)
33
posts = self.posts(page)
34
35
if not data["title"]:
36
data["title"] = posts[0]["com"][:50]
37
38
for post in posts:
39
post.update(data)
40
post["time"] = int(util.datetime_to_timestamp(post["date"]))
41
yield Message.Directory, post
42
if "url" in post:
43
yield Message.Url, post["url"], text.nameext_from_url(
44
post["filename"], post)
45
46
def metadata(self, page):
47
return {
48
"board" : self.board,
49
"thread": text.parse_int(self.thread),
50
"title" : text.unescape(text.extr(
51
page, 'class="subject">', "</span>"))
52
}
53
54
def posts(self, page):
55
return [
56
self.parse(post)
57
for post in page.split('class="postContainer')[1:]
58
]
59
60
def parse(self, post):
61
extr = text.extract_from(post)
62
data = {
63
"name": extr('class="name">', "</span>"),
64
"date": text.parse_datetime(
65
extr('class="dateTime postNum" >', "<").strip(),
66
"%Y-%m-%d %H:%M:%S"),
67
"no" : text.parse_int(extr(">Post No.", "<")),
68
}
69
if 'class="file"' in post:
70
extr('class="fileText"', ">File: <a")
71
data.update({
72
"url" : extr('href="', '"'),
73
"filename": extr(
74
'rel="noreferrer noopener"', "</a>").strip()[1:],
75
"size" : text.parse_bytes(extr(" (", ", ")[:-1]),
76
"width" : text.parse_int(extr("", "x")),
77
"height" : text.parse_int(extr("", "px")),
78
})
79
extr("<blockquote ", "")
80
data["com"] = text.unescape(text.remove_html(
81
extr(">", "</blockquote>")))
82
return data
83
84
85
class _4archiveBoardExtractor(Extractor):
86
"""Extractor for 4archive boards"""
87
category = "4archive"
88
subcategory = "board"
89
root = "https://4archive.org"
90
pattern = r"(?:https?://)?4archive\.org/board/([^/?#]+)(?:/(\d+))?/?$"
91
example = "https://4archive.org/board/a/"
92
93
def __init__(self, match):
94
Extractor.__init__(self, match)
95
self.board = match[1]
96
self.num = text.parse_int(match[2], 1)
97
98
def items(self):
99
data = {"_extractor": _4archiveThreadExtractor}
100
while True:
101
url = f"{self.root}/board/{self.board}/{self.num}"
102
page = self.request(url).text
103
if 'class="thread"' not in page:
104
return
105
for thread in text.extract_iter(page, 'class="thread" id="t', '"'):
106
url = f"{self.root}/board/{self.board}/thread/{thread}"
107
yield Message.Queue, url, data
108
self.num += 1
109
110