aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/4archive.py
blob: 4c434643ea1c7828252ed5bf93e24cbfbde5c080 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- 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 https://4archive.org/"""

from .common import Extractor, Message
from .. import text, util


class _4archiveThreadExtractor(Extractor):
    """Extractor for 4archive threads"""
    category = "4archive"
    subcategory = "thread"
    directory_fmt = ("{category}", "{board}", "{thread} {title}")
    filename_fmt = "{no} {filename}.{extension}"
    archive_fmt = "{board}_{thread}_{no}"
    root = "https://4archive.org"
    referer = False
    pattern = r"(?:https?://)?4archive\.org/board/([^/?#]+)/thread/(\d+)"
    example = "https://4archive.org/board/a/thread/12345/"

    def __init__(self, match):
        Extractor.__init__(self, match)
        self.board, self.thread = match.groups()

    def items(self):
        url = f"{self.root}/board/{self.board}/thread/{self.thread}"
        page = self.request(url).text
        data = self.metadata(page)
        posts = self.posts(page)

        if not data["title"]:
            data["title"] = posts[0]["com"][:50]

        for post in posts:
            post.update(data)
            post["time"] = int(util.datetime_to_timestamp(post["date"]))
            yield Message.Directory, post
            if "url" in post:
                yield Message.Url, post["url"], text.nameext_from_url(
                    post["filename"], post)

    def metadata(self, page):
        return {
            "board" : self.board,
            "thread": text.parse_int(self.thread),
            "title" : text.unescape(text.extr(
                page, 'class="subject">', "</span>"))
        }

    def posts(self, page):
        return [
            self.parse(post)
            for post in page.split('class="postContainer')[1:]
        ]

    def parse(self, post):
        extr = text.extract_from(post)
        data = {
            "name": extr('class="name">', "</span>"),
            "date": text.parse_datetime(
                (extr('class="dateTime">', "<") or
                 extr('class="dateTime postNum" >', "<")).strip(),
                "%Y-%m-%d %H:%M:%S"),
            "no"  : text.parse_int(extr(">Post No.", "<")),
        }
        if 'class="file"' in post:
            extr('class="fileText"', ">File: <a")
            data.update({
                "url"     : extr('href="', '"'),
                "filename": extr('alt="Image: ', '"'),
                "size"    : text.parse_bytes(extr(" (", ", ")[:-1]),
                "width"   : text.parse_int(extr("", "x")),
                "height"  : text.parse_int(extr("", "px")),
            })
        extr("<blockquote ", "")
        data["com"] = text.unescape(text.remove_html(
            extr(">", "</blockquote>")))
        return data


class _4archiveBoardExtractor(Extractor):
    """Extractor for 4archive boards"""
    category = "4archive"
    subcategory = "board"
    root = "https://4archive.org"
    pattern = r"(?:https?://)?4archive\.org/board/([^/?#]+)(?:/(\d+))?/?$"
    example = "https://4archive.org/board/a/"

    def __init__(self, match):
        Extractor.__init__(self, match)
        self.board = match[1]
        self.num = text.parse_int(match[2], 1)

    def items(self):
        data = {"_extractor": _4archiveThreadExtractor}
        while True:
            url = f"{self.root}/board/{self.board}/{self.num}"
            page = self.request(url).text
            if 'class="thread"' not in page:
                return
            for thread in text.extract_iter(page, 'class="thread" id="t', '"'):
                url = f"{self.root}/board/{self.board}/thread/{thread}"
                yield Message.Queue, url, data
            self.num += 1