aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/piczel.py
blob: 2775dac7cee404b0936a9d20086b8a0e162a6739 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-

# Copyright 2018-2019 Mike Fährmann
#
# 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://piczel.tv/"""

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


class PiczelExtractor(Extractor):
    """Base class for piczel extractors"""
    category = "piczel"
    directory_fmt = ("{category}", "{user[username]}")
    filename_fmt = "{category}_{id}_{title}_{num:>02}.{extension}"
    archive_fmt = "{id}_{num}"
    root = "https://piczel.tv"

    def items(self):
        yield Message.Version, 1
        for image in self.unpack(self.images()):
            url = self.root + "/static" + image["image"]["image"]["url"]
            yield Message.Directory, image
            yield Message.Url, url, text.nameext_from_url(url, image)

    @staticmethod
    def unpack(images):
        """Unpack 'images' into individual image objects"""
        for image in images:
            if image["multi"]:
                multi = image["images"]
                del image["images"]
                for image["num"], img in enumerate(multi):
                    image["image"] = img
                    yield image
            else:
                image["num"] = 0
                yield image

    def images(self):
        """Return an iterable with all relevant image objects"""

    def _pagination(self, url, folder_id=None):
        params = {
            "hideNsfw" : "false",
            "from_id"  : None,
            "folder_id": folder_id,
        }

        while True:
            data = self.request(url, params=params).json()
            yield from data

            if len(data) < 32:
                return
            params["from_id"] = data[-1]["id"]


class PiczelUserExtractor(PiczelExtractor):
    """Extractor for all images from a user's gallery"""
    subcategory = "user"
    pattern = r"(?:https?://)?(?:www\.)?piczel\.tv/gallery/([^/?&#]+)/?$"
    test = ("https://piczel.tv/gallery/Maximumwarp", {
        "count": ">= 50",
    })

    def __init__(self, match):
        PiczelExtractor.__init__(self, match)
        self.user = match.group(1)

    def images(self):
        url = "{}/api/users/{}/gallery".format(self.root, self.user)
        return self._pagination(url)


class PiczelFolderExtractor(PiczelExtractor):
    """Extractor for images inside a user's folder"""
    subcategory = "folder"
    directory_fmt = ("{category}", "{user[username]}", "{folder[name]}")
    archive_fmt = "f{folder[id]}_{id}_{num}"
    pattern = (r"(?:https?://)?(?:www\.)?piczel\.tv"
               r"/gallery/(?!image)([^/?&#]+)/(\d+)")
    test = ("https://piczel.tv/gallery/Lulena/1114", {
        "count": ">= 4",
    })

    def __init__(self, match):
        PiczelExtractor.__init__(self, match)
        self.user, self.folder_id = match.groups()

    def images(self):
        url = "{}/api/users/{}/gallery".format(self.root, self.user)
        return self._pagination(url, self.folder_id)


class PiczelImageExtractor(PiczelExtractor):
    """Extractor for individual images"""
    subcategory = "image"
    pattern = r"(?:https?://)?(?:www\.)?piczel\.tv/gallery/image/(\d+)"
    test = ("https://piczel.tv/gallery/image/7807", {
        "url": "85225dd53a03c3b6028f6c4a45b71eccc07f7066",
        "content": "df9a053a24234474a19bce2b7e27e0dec23bff87",
        "keyword": {
            "created_at": "2018-07-22T05:13:58.000Z",
            "description": None,
            "extension": "png",
            "favorites_count": int,
            "folder": dict,
            "folder_id": 1113,
            "id": 7807,
            "is_flash": False,
            "is_video": False,
            "multi": False,
            "nsfw": False,
            "num": 0,
            "password_protected": False,
            "tags": "fanart, commission, altair, recreators, ",
            "title": "Altair",
            "user": dict,
            "views": int,
        },
    })

    def __init__(self, match):
        PiczelExtractor.__init__(self, match)
        self.image_id = match.group(1)

    def images(self):
        url = "{}/api/gallery/image/{}".format(self.root, self.image_id)
        return (self.request(url).json(),)