aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/picarto.py
blob: b0fa079f9a752b1fa50e6c500ada9d2e05aaf57e (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
# -*- coding: utf-8 -*-

# Copyright 2021-2025 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://picarto.tv/"""

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


class PicartoGalleryExtractor(Extractor):
    """Extractor for picarto galleries"""
    category = "picarto"
    subcategory = "gallery"
    root = "https://picarto.tv"
    directory_fmt = ("{category}", "{channel[name]}")
    filename_fmt = "{id} {title}.{extension}"
    archive_fmt = "{id}"
    pattern = r"(?:https?://)?picarto\.tv/([^/?#]+)/gallery"
    example = "https://picarto.tv/USER/gallery/TITLE/"

    def __init__(self, match):
        Extractor.__init__(self, match)
        self.username = match[1]

    def items(self):
        for post in self.posts():
            post["date"] = self.parse_datetime_iso(post["created_at"])
            variations = post.pop("variations", ())
            yield Message.Directory, "", post

            image = post["default_image"]
            if not image:
                continue
            url = "https://images.picarto.tv/gallery/" + image["name"]
            text.nameext_from_url(url, post)
            yield Message.Url, url, post

            for variation in variations:
                post.update(variation)
                image = post["default_image"]
                url = "https://images.picarto.tv/gallery/" + image["name"]
                text.nameext_from_url(url, post)
                yield Message.Url, url, post

    def posts(self):
        url = "https://ptvintern.picarto.tv/api/channel-gallery"
        params = {
            "first": "30",
            "page": 1,
            "filter_params[album_id]": "",
            "filter_params[channel_name]": self.username,
            "filter_params[q]": "",
            "filter_params[visibility]": "",
            "order_by[field]": "published_at",
            "order_by[order]": "DESC",
        }

        while True:
            posts = self.request_json(url, params=params)
            if not posts:
                return
            yield from posts
            params["page"] += 1