aboutsummaryrefslogtreecommitdiff
path: root/bridges/PixivBridge.php
blob: e464b12d03c97b91e5021b72b2757fa3f9f95fdb (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php

/**
 * Good resource on API return values (Ex: illustType):
 * https://hackage.haskell.org/package/pixiv-0.1.0/docs/Web-Pixiv-Types.html
 */
class PixivBridge extends BridgeAbstract
{
    const NAME = 'Pixiv Bridge';
    const URI = 'https://www.pixiv.net/';
    const DESCRIPTION = 'Returns the tag search from pixiv.net';
    const MAINTAINER = 'mruac';
    const CONFIGURATION = [
        'cookie' => [
            'required' => false,
            'defaultValue' => null
        ],
        'proxy_url' => [
            'required' => false,
            'defaultValue' => null
        ]
    ];

    const PARAMETERS = [
        'global' => [
            'posts' => [
                'name' => 'Post Limit',
                'type' => 'number',
                'defaultValue' => '10'
            ],
            'fullsize' => [
                'name' => 'Full-size Image',
                'type' => 'checkbox'
            ],
            'mode' => [
                'name' => 'Post Type',
                'type' => 'list',
                'values' => [
                    'All Works' => 'all',
                    'Illustrations' => 'illustrations/',
                    'Manga' => 'manga/',
                    'Novels' => 'novels/'
                ]
            ],
            'mature' => [
                'name' => 'Include R-18 works',
                'type' => 'checkbox'
            ],
            'ai' => [
                'name' => 'Include AI-Generated works',
                'type' => 'checkbox'
            ]
        ],
        'Tag' => [
            'tag' => [
                'name' => 'Query to search',
                'exampleValue' => 'オリジナル',
                'required' => true
            ]
        ],
        'User' => [
            'userid' => [
                'name' => 'User ID from profile URL',
                'exampleValue' => '11',
                'required' => true
            ]
        ]
    ];

    // maps from URLs to json keys by context
    const JSON_KEY_MAP = [
        'Tag' => [
            'illustrations/' => 'illust',
            'manga/' => 'manga',
            'novels/' => 'novel'
        ],
        'User' => [
            'illustrations/' => 'illusts',
            'manga/' => 'manga',
            'novels/' => 'novels'
        ]
    ];

    // Hold the username for getName()
    private $username = null;

    public function getName()
    {
        switch ($this->queriedContext) {
            case 'Tag':
                $context = 'Tag';
                $query = $this->getInput('tag');
                break;
            case 'User':
                $context = 'User';
                $query = $this->username ?? $this->getInput('userid');
                break;
            default:
                return parent::getName();
        }
        return 'Pixiv ' . $this->getKey('mode') . " from {$context} {$query}";
    }

    public function getURI()
    {
        switch ($this->queriedContext) {
            case 'Tag':
                $uri = static::URI . 'tags/' . urlencode($this->getInput('tag') ?? '');
                break;
            case 'User':
                $uri = static::URI . 'users/' . $this->getInput('userid');
                break;
            default:
                return parent::getURI();
        }
        if ($this->getInput('mode') != 'all') {
            $uri = $uri . '/' . $this->getInput('mode');
        }
        return $uri;
    }

    private function getSearchURI($mode)
    {
        switch ($this->queriedContext) {
            case 'Tag':
                $query = urlencode($this->getInput('tag'));
                $uri = static::URI . 'ajax/search/top/' . $query;
                break;
            case 'User':
                $uri = static::URI . 'ajax/user/' . $this->getInput('userid')
                    . '/profile/top';
                break;
            default:
                returnClientError('Invalid Context');
        }
        return $uri;
    }

    private function getDataFromJSON($json, $json_key)
    {
        $key = $json_key;
        if (
            $this->queriedContext === 'Tag' &&
            $this->getOption('cookie') !== null
        ) {
            switch ($json_key) {
                case 'illust':
                case 'manga':
                    $key = 'illustManga';
                    break;
            }
        }
        $json = $json['body'][$key];
        // Tags context contains subkey
        if ($this->queriedContext === 'Tag') {
            $json = $json['data'];
            if ($this->getOption('cookie') !== null) {
                switch ($json_key) {
                    case 'illust':
                        $json = array_reduce($json, function ($acc, $i) {
                            if ($i['illustType'] === 0) {
                                $acc[] = $i;
                            }
                            return $acc;
                        }, []);
                        break;
                    case 'manga':
                        $json = array_reduce($json, function ($acc, $i) {
                            if ($i['illustType'] === 1) {
                                $acc[] = $i;
                            }return $acc;
                        }, []);
                        break;
                }
            }
        }
        return $json;
    }

    private function collectWorksArray()
    {
        $content = $this->getData($this->getSearchURI($this->getInput('mode')), true, true);
        if ($this->getInput('mode') == 'all') {
            $total = [];
            foreach (self::JSON_KEY_MAP[$this->queriedContext] as $mode => $json_key) {
                $current = $this->getDataFromJSON($content, $json_key);
                $total = array_merge($total, $current);
            }
            $content = $total;
        } else {
            $json_key = self::JSON_KEY_MAP[$this->queriedContext][$this->getInput('mode')];
            $content = $this->getDataFromJSON($content, $json_key);
        }
        return $content;
    }

    public function collectData()
    {
        $this->checkOptions();
        $proxy_url = $this->getOption('proxy_url');
        $proxy_url = $proxy_url ? rtrim($proxy_url, '/') : null;

        $content = $this->collectWorksArray();
        $content = array_filter($content, function ($v, $k) {
            return !array_key_exists('isAdContainer', $v);
        }, ARRAY_FILTER_USE_BOTH);

        // Sort by updateDate to get newest works
        usort($content, function ($a, $b) {
            return $b['updateDate'] <=> $a['updateDate'];
        });

        //exclude AI generated works if unchecked.
        if ($this->getInput('ai') !== true) {
            $content = array_filter($content, function ($v) {
                $isAI = $v['aiType'] === 2;
                return !$isAI;
            });
        }

        //exclude R-18 works if unchecked.
        if ($this->getInput('mature') !== true) {
            $content = array_filter($content, function ($v) {
                $isMature = $v['xRestrict'] > 0;
                return !$isMature;
            });
        }

        $content = array_slice($content, 0, $this->getInput('posts'));

        foreach ($content as $result) {
            // Store username for getName()
            if (!$this->username) {
                $this->username = $result['userName'];
            }

            $item = [];
            $item['uid'] = $result['id'];

            $subpath = array_key_exists('illustType', $result) ? 'artworks/' : 'novel/show.php?id=';
            $item['uri'] = static::URI . $subpath . $result['id'];

            $item['title'] = $result['title'];
            $item['author'] = $result['userName'];
            $item['timestamp'] = $result['updateDate'];
            $item['categories'] = $result['tags'];

            if ($proxy_url) {
                //use proxy image host if set.
                if ($this->getInput('fullsize')) {
                    $ajax_uri = static::URI . 'ajax/illust/' . $result['id'];
                    $imagejson = $this->getData($ajax_uri, true, true);
                    $img_url = preg_replace('/https:\/\/i\.pximg\.net/', $proxy_url, $imagejson['body']['urls']['original']);
                } else {
                    $img_url = preg_replace('/https:\/\/i\.pximg\.net/', $proxy_url, $result['url']);
                }
            } else {
                $img_url = $result['url'];
            }

            // Currently, this might result in broken image due to their strict referrer check
            $item['content'] = sprintf('<a href="%s"><img src="%s"/></a>', $img_url, $img_url);

            // Additional content items
            if (array_key_exists('pageCount', $result)) {
                $item['content'] .= '<br>Page Count: ' . $result['pageCount'];
            } else {
                $item['content'] .= '<br>Word Count: ' . $result['wordCount'];
            }

            $this->items[] = $item;
        }
    }

    private function checkOptions()
    {
        $proxy = $this->getOption('proxy_url');
        if ($proxy) {
            if (
                !(strlen($proxy) > 0 && preg_match('/https?:\/\/.*/', $proxy))
            ) {
                returnServerError('Invalid proxy_url value set. The proxy must include the HTTP/S at the beginning of the url.');
            }
        }

        $cookie = $this->getCookie();
        if ($cookie) {
            $isAuth = $this->loadCacheValue('is_authenticated');
            if (!$isAuth) {
                $res = $this->getData('https://www.pixiv.net/ajax/webpush', true, true);
                if ($res['error'] === false) {
                    $this->saveCacheValue('is_authenticated', true);
                }
            }
        }
    }

    private function checkCookie(array $headers)
    {
        if (array_key_exists('set-cookie', $headers)) {
            foreach ($headers['set-cookie'] as $value) {
                if (str_starts_with($value, 'PHPSESSID=')) {
                    parse_str(strtr($value, ['&' => '%26', '+' => '%2B', ';' => '&']), $cookie);
                    if ($cookie['PHPSESSID'] != $this->getCookie()) {
                        $this->saveCacheValue('cookie', $cookie['PHPSESSID']);
                    }
                    break;
                }
            }
        }
    }

    private function getCookie()
    {
        // checks if cookie is set, if not initialise it with the cookie from the config
        $value = $this->loadCacheValue('cookie');
        if (!$value) {
            $value = $this->getOption('cookie');

            // 30 days + 1 day to let cookie chance to renew
            $this->saveCacheValue('cookie', $this->getOption('cookie'), 2678400);
        }
        return $value;
    }

    //Cache getContents by default
    private function getData(string $url, bool $cache = true, bool $getJSON = false, array $httpHeaders = [], array $curlOptions = [])
    {
        $cookie_str = $this->getCookie();
        if ($cookie_str) {
            $curlOptions[CURLOPT_COOKIE] = 'PHPSESSID=' . $cookie_str;
        }

        if ($cache) {
            $response = $this->loadCacheValue($url);
            if (!$response || is_array($response)) {
                $response = getContents($url, $httpHeaders, $curlOptions, true);
                $this->saveCacheValue($url, $response);
            }
        } else {
            $response = getContents($url, $httpHeaders, $curlOptions, true);
        }

        $this->checkCookie($response->getHeaders());

        if ($getJSON) {
            return json_decode($response->getBody(), true);
        }
        return $response->getBody();
    }
}