aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/jschan.py
blob: 398256dc7bff925dd1f39dc11405b96724457ce9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Extractors for jschan Imageboards"""

from .common import BaseExtractor, Message
from .. import text
import itertools


class JschanExtractor(BaseExtractor):
    basecategory = "jschan"


BASE_PATTERN = JschanExtractor.update({
    "94chan": {
        "root": "https://94chan.org",
        "pattern": r"94chan\.org"
    }
})


class JschanThreadExtractor(JschanExtractor):
    """Extractor for jschan threads"""
    subcategory = "thread"
    directory_fmt = ("{category}", "{board}",
                     "{threadId} {subject|nomarkup[:50]}")
    filename_fmt = "{postId}{num:?-//} {filename}.{extension}"
    archive_fmt = "{board}_{postId}_{num}"
    pattern = BASE_PATTERN + r"/([^/?#]+)/thread/(\d+)\.html"
    example = "https://94chan.org/a/thread/12345.html"

    def __init__(self, match):
        JschanExtractor.__init__(self, match)
        index = match.lastindex
        self.board = match.group(index-1)
        self.thread = match.group(index)

    def items(self):
        url = "{}/{}/thread/{}.json".format(
            self.root, self.board, self.thread)
        thread = self.request(url).json()
        thread["threadId"] = thread["postId"]
        posts = thread.pop("replies", ())

        yield Message.Directory, thread
        for post in itertools.chain((thread,), posts):
            files = post.pop("files", ())
            if files:
                thread.update(post)
                thread["count"] = len(files)
                for num, file in enumerate(files):
                    url = self.root + "/file/" + file["filename"]
                    file.update(thread)
                    file["num"] = num
                    file["siteFilename"] = file["filename"]
                    text.nameext_from_url(file["originalFilename"], file)
                    yield Message.Url, url, file


class JschanBoardExtractor(JschanExtractor):
    """Extractor for jschan boards"""
    subcategory = "board"
    pattern = (BASE_PATTERN + r"/([^/?#]+)"
               r"(?:/index\.html|/catalog\.html|/\d+\.html|/?$)")
    example = "https://94chan.org/a/"

    def __init__(self, match):
        JschanExtractor.__init__(self, match)
        self.board = match.group(match.lastindex)

    def items(self):
        url = "{}/{}/catalog.json".format(self.root, self.board)
        for thread in self.request(url).json():
            url = "{}/{}/thread/{}.html".format(
                self.root, self.board, thread["postId"])
            thread["_extractor"] = JschanThreadExtractor
            yield Message.Queue, url, thread