Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/gallery_dl/extractor/2ch.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://2ch.hk/"""
8
9
from .common import Extractor, Message
10
from .. import text, util
11
12
13
class _2chThreadExtractor(Extractor):
14
"""Extractor for 2ch threads"""
15
category = "2ch"
16
subcategory = "thread"
17
root = "https://2ch.hk"
18
directory_fmt = ("{category}", "{board}", "{thread} {title}")
19
filename_fmt = "{tim}{filename:? //}.{extension}"
20
archive_fmt = "{board}_{thread}_{tim}"
21
pattern = r"(?:https?://)?2ch\.hk/([^/?#]+)/res/(\d+)"
22
example = "https://2ch.hk/a/res/12345.html"
23
24
def __init__(self, match):
25
Extractor.__init__(self, match)
26
self.board, self.thread = match.groups()
27
28
def items(self):
29
url = f"{self.root}/{self.board}/res/{self.thread}.json"
30
posts = self.request_json(url)["threads"][0]["posts"]
31
32
op = posts[0]
33
title = op.get("subject") or text.remove_html(op["comment"])
34
35
thread = {
36
"board" : self.board,
37
"thread": self.thread,
38
"title" : text.unescape(title)[:50],
39
}
40
41
yield Message.Directory, thread
42
for post in posts:
43
if files := post.get("files"):
44
post["post_name"] = post["name"]
45
post["date"] = text.parse_timestamp(post["timestamp"])
46
del post["files"]
47
del post["name"]
48
49
for file in files:
50
file.update(thread)
51
file.update(post)
52
53
file["filename"] = file["fullname"].rpartition(".")[0]
54
file["tim"], _, file["extension"] = \
55
file["name"].rpartition(".")
56
57
yield Message.Url, self.root + file["path"], file
58
59
60
class _2chBoardExtractor(Extractor):
61
"""Extractor for 2ch boards"""
62
category = "2ch"
63
subcategory = "board"
64
root = "https://2ch.hk"
65
pattern = r"(?:https?://)?2ch\.hk/([^/?#]+)/?$"
66
example = "https://2ch.hk/a/"
67
68
def __init__(self, match):
69
Extractor.__init__(self, match)
70
self.board = match[1]
71
72
def items(self):
73
base = f"{self.root}/{self.board}"
74
75
# index page
76
url = f"{base}/index.json"
77
index = self.request_json(url)
78
index["_extractor"] = _2chThreadExtractor
79
for thread in index["threads"]:
80
url = f"{base}/res/{thread['thread_num']}.html"
81
yield Message.Queue, url, index
82
83
# pages 1..n
84
for n in util.advance(index["pages"], 1):
85
url = f"{base}/{n}.json"
86
page = self.request_json(url)
87
page["_extractor"] = _2chThreadExtractor
88
for thread in page["threads"]:
89
url = f"{base}/res/{thread['thread_num']}.html"
90
yield Message.Queue, url, page
91
92