aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/cien.py
blob: 45e5dab74193058bfdf70439a0874051dbf16138 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-

# Copyright 2024-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://ci-en.net/"""

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

BASE_PATTERN = r"(?:https?://)?ci-en\.(?:net|dlsite\.com)"


class CienExtractor(Extractor):
    category = "cien"
    root = "https://ci-en.net"
    request_interval = (1.0, 2.0)

    def __init__(self, match):
        self.root = text.root_from_url(match[0])
        Extractor.__init__(self, match)

    def _init(self):
        self.cookies.set("accepted_rating", "r18g", domain="ci-en.dlsite.com")

    def _pagination_articles(self, url, params):
        data = {"_extractor": CienArticleExtractor}
        params["page"] = text.parse_int(params.get("page"), 1)

        while True:
            page = self.request(url, params=params).text

            for card in text.extract_iter(
                    page, ' class="c-cardCase-item', '</div>'):
                article_url = text.extr(card, ' href="', '"')
                yield Message.Queue, article_url, data

            if ' rel="next"' not in page:
                return
            params["page"] += 1


class CienArticleExtractor(CienExtractor):
    subcategory = "article"
    filename_fmt = "{num:>02} {filename}.{extension}"
    directory_fmt = ("{category}", "{author[name]}", "{post_id} {name}")
    archive_fmt = "{post_id}_{num}"
    pattern = BASE_PATTERN + r"/creator/(\d+)/article/(\d+)"
    example = "https://ci-en.net/creator/123/article/12345"

    def items(self):
        author_id, post_id = self.groups
        url = f"{self.root}/creator/{author_id}/article/{post_id}"
        page = self.request(url, notfound="article").text

        files = self._extract_files(page)
        post = self._extract_jsonld(page)[0]
        post["post_url"] = url
        post["post_id"] = text.parse_int(post_id)
        post["count"] = len(files)
        post["date"] = text.parse_datetime(post["datePublished"])

        try:
            post["author"]["id"] = text.parse_int(author_id)
            del post["publisher"]
            del post["sameAs"]
        except Exception:
            pass

        yield Message.Directory, post
        for post["num"], file in enumerate(files, 1):
            post.update(file)
            if "extension" not in file:
                text.nameext_from_url(file["url"], post)
            yield Message.Url, file["url"], post

    def _extract_files(self, page):
        files = []

        filetypes = self.config("files")
        if filetypes is None:
            self._extract_files_image(page, files)
            self._extract_files_video(page, files)
            self._extract_files_download(page, files)
            self._extract_files_gallery(page, files)
        else:
            generators = {
                "image"   : self._extract_files_image,
                "video"   : self._extract_files_video,
                "download": self._extract_files_download,
                "gallery" : self._extract_files_gallery,
                "gallerie": self._extract_files_gallery,
            }
            if isinstance(filetypes, str):
                filetypes = filetypes.split(",")
            for ft in filetypes:
                generators[ft.rstrip("s")](page, files)

        return files

    def _extract_files_image(self, page, files):
        for image in text.extract_iter(
                page, 'class="file-player-image"', "</figure>"):
            size = text.extr(image, ' data-size="', '"')
            w, _, h = size.partition("x")

            files.append({
                "url"   : text.extr(image, ' data-raw="', '"'),
                "width" : text.parse_int(w),
                "height": text.parse_int(h),
                "type"  : "image",
            })

    def _extract_files_video(self, page, files):
        for video in text.extract_iter(
                page, "<vue-file-player", "</vue-file-player>"):
            path = text.extr(video, ' base-path="', '"')
            name = text.extr(video, ' file-name="', '"')
            auth = text.extr(video, ' auth-key="', '"')

            file = text.nameext_from_url(name)
            file["url"] = f"{path}video-web.mp4?{auth}"
            file["type"] = "video"
            files.append(file)

    def _extract_files_download(self, page, files):
        for download in text.extract_iter(
                page, 'class="downloadBlock', "</div>"):
            name = text.extr(download, "<p>", "<")

            file = text.nameext_from_url(name.rpartition(" ")[0])
            file["url"] = text.extr(download, ' href="', '"')
            file["type"] = "download"
            files.append(file)

    def _extract_files_gallery(self, page, files):
        for gallery in text.extract_iter(
                page, "<vue-image-gallery", "</vue-image-gallery>"):

            url = self.root + "/api/creator/gallery/images"
            params = {
                "hash"      : text.extr(gallery, ' hash="', '"'),
                "gallery_id": text.extr(gallery, ' gallery-id="', '"'),
                "time"      : text.extr(gallery, ' time="', '"'),
            }
            data = self.request_json(url, params=params)
            url = self.root + "/api/creator/gallery/imagePath"

            for params["page"], params["file_id"] in enumerate(
                    data["imgList"]):
                path = self.request_json(url, params=params)["path"]

                file = params.copy()
                file["url"] = path
                files.append(file)


class CienCreatorExtractor(CienExtractor):
    subcategory = "creator"
    pattern = BASE_PATTERN + r"/creator/(\d+)(?:/article(?:\?([^#]+))?)?/?$"
    example = "https://ci-en.net/creator/123"

    def items(self):
        url = f"{self.root}/creator/{self.groups[0]}/article"
        params = text.parse_query(self.groups[1])
        params["mode"] = "list"
        return self._pagination_articles(url, params)


class CienRecentExtractor(CienExtractor):
    subcategory = "recent"
    pattern = BASE_PATTERN + r"/mypage/recent(?:\?([^#]+))?"
    example = "https://ci-en.net/mypage/recent"

    def items(self):
        url = self.root + "/mypage/recent"
        params = text.parse_query(self.groups[0])
        return self._pagination_articles(url, params)


class CienFollowingExtractor(CienExtractor):
    subcategory = "following"
    pattern = BASE_PATTERN + r"/mypage/subscription(/following)?"
    example = "https://ci-en.net/mypage/subscription"

    def items(self):
        url = self.root + "/mypage/subscription" + (self.groups[0] or "")
        page = self.request(url).text
        data = {"_extractor": CienCreatorExtractor}

        for subscription in text.extract_iter(
                page, 'class="c-grid-subscriptionInfo', '</figure>'):
            url = text.extr(subscription, ' href="', '"')
            yield Message.Queue, url, data