2018-04-14 18:19:35 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-09 22:33:35 +03:00
|
|
|
/**
|
|
|
|
* Good resource on API return values (Ex: illustType):
|
|
|
|
* https://hackage.haskell.org/package/pixiv-0.1.0/docs/Web-Pixiv-Types.html
|
|
|
|
*/
|
2018-04-14 18:19:35 +03:00
|
|
|
class PixivBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const NAME = 'Pixiv Bridge';
|
|
|
|
const URI = 'https://www.pixiv.net/';
|
|
|
|
const DESCRIPTION = 'Returns the tag search from pixiv.net';
|
2023-09-06 17:16:25 +03:00
|
|
|
const MAINTAINER = 'mruac';
|
|
|
|
const CONFIGURATION = [
|
|
|
|
'cookie' => [
|
|
|
|
'required' => false,
|
|
|
|
'defaultValue' => null
|
|
|
|
],
|
|
|
|
'proxy_url' => [
|
|
|
|
'required' => false,
|
|
|
|
'defaultValue' => null
|
|
|
|
]
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
const PARAMETERS = [
|
|
|
|
'global' => [
|
|
|
|
'posts' => [
|
|
|
|
'name' => 'Post Limit',
|
|
|
|
'type' => 'number',
|
|
|
|
'defaultValue' => '10'
|
|
|
|
],
|
|
|
|
'fullsize' => [
|
|
|
|
'name' => 'Full-size Image',
|
|
|
|
'type' => 'checkbox'
|
|
|
|
],
|
|
|
|
'mode' => [
|
|
|
|
'name' => 'Post Type',
|
|
|
|
'type' => 'list',
|
2023-09-06 17:16:25 +03:00
|
|
|
'values' => [
|
|
|
|
'All Works' => 'all',
|
|
|
|
'Illustrations' => 'illustrations/',
|
|
|
|
'Manga' => 'manga/',
|
|
|
|
'Novels' => 'novels/'
|
|
|
|
]
|
2022-07-01 16:10:30 +03:00
|
|
|
],
|
2023-09-06 17:16:25 +03:00
|
|
|
'mature' => [
|
|
|
|
'name' => 'Include R-18 works',
|
|
|
|
'type' => 'checkbox'
|
|
|
|
],
|
|
|
|
'ai' => [
|
|
|
|
'name' => 'Include AI-Generated works',
|
|
|
|
'type' => 'checkbox'
|
|
|
|
]
|
2022-05-08 03:46:57 +03:00
|
|
|
],
|
2022-07-01 16:10:30 +03:00
|
|
|
'Tag' => [
|
2022-06-24 12:31:24 +03:00
|
|
|
'tag' => [
|
2022-05-08 03:46:57 +03:00
|
|
|
'name' => 'Query to search',
|
|
|
|
'exampleValue' => 'オリジナル',
|
|
|
|
'required' => true
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2018-04-14 18:19:35 +03:00
|
|
|
],
|
2022-05-08 03:46:57 +03:00
|
|
|
'User' => [
|
|
|
|
'userid' => [
|
|
|
|
'name' => 'User ID from profile URL',
|
|
|
|
'exampleValue' => '11',
|
|
|
|
'required' => true
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
]
|
2022-05-08 03:46:57 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
// maps from URLs to json keys by context
|
2021-05-24 22:42:39 +03:00
|
|
|
const JSON_KEY_MAP = [
|
2022-06-24 12:31:24 +03:00
|
|
|
'Tag' => [
|
2022-05-08 03:46:57 +03:00
|
|
|
'illustrations/' => 'illust',
|
|
|
|
'manga/' => 'manga',
|
|
|
|
'novels/' => 'novel'
|
|
|
|
],
|
|
|
|
'User' => [
|
|
|
|
'illustrations/' => 'illusts',
|
|
|
|
'manga/' => 'manga',
|
|
|
|
'novels/' => 'novels'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2021-05-24 22:42:39 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
// Hold the username for getName()
|
|
|
|
private $username = null;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
switch ($this->queriedContext) {
|
2022-06-24 12:31:24 +03:00
|
|
|
case 'Tag':
|
2022-05-08 03:46:57 +03:00
|
|
|
$context = 'Tag';
|
|
|
|
$query = $this->getInput('tag');
|
|
|
|
break;
|
|
|
|
case 'User':
|
|
|
|
$context = 'User';
|
|
|
|
$query = $this->username ?? $this->getInput('userid');
|
2021-05-24 22:42:39 +03:00
|
|
|
break;
|
2022-05-08 03:46:57 +03:00
|
|
|
default:
|
|
|
|
return parent::getName();
|
|
|
|
}
|
2023-09-06 17:16:25 +03:00
|
|
|
return 'Pixiv ' . $this->getKey('mode') . " from {$context} {$query}";
|
2022-05-08 03:46:57 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
switch ($this->queriedContext) {
|
2022-06-24 12:31:24 +03:00
|
|
|
case 'Tag':
|
2022-05-08 17:17:26 +03:00
|
|
|
$uri = static::URI . 'tags/' . urlencode($this->getInput('tag') ?? '');
|
2022-05-08 03:46:57 +03:00
|
|
|
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;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
private function getSearchURI($mode)
|
|
|
|
{
|
|
|
|
switch ($this->queriedContext) {
|
2022-06-24 12:31:24 +03:00
|
|
|
case 'Tag':
|
2022-05-08 03:46:57 +03:00
|
|
|
$query = urlencode($this->getInput('tag'));
|
|
|
|
$uri = static::URI . 'ajax/search/top/' . $query;
|
|
|
|
break;
|
|
|
|
case 'User':
|
|
|
|
$uri = static::URI . 'ajax/user/' . $this->getInput('userid')
|
2023-09-06 17:16:25 +03:00
|
|
|
. '/profile/top';
|
2022-05-08 03:46:57 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
returnClientError('Invalid Context');
|
|
|
|
}
|
|
|
|
return $uri;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
private function getDataFromJSON($json, $json_key)
|
|
|
|
{
|
2023-09-06 17:16:25 +03:00
|
|
|
$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];
|
2022-05-08 03:46:57 +03:00
|
|
|
// Tags context contains subkey
|
2023-09-06 17:16:25 +03:00
|
|
|
if ($this->queriedContext === 'Tag') {
|
2022-05-08 03:46:57 +03:00
|
|
|
$json = $json['data'];
|
2023-09-06 17:16:25 +03:00
|
|
|
if ($this->getOption('cookie') !== null) {
|
|
|
|
switch ($json_key) {
|
|
|
|
case 'illust':
|
|
|
|
$json = array_reduce($json, function ($acc, $i) {
|
|
|
|
if ($i['illustType'] === 0) {
|
|
|
|
$acc[] = $i;
|
2024-04-04 20:12:04 +03:00
|
|
|
}
|
|
|
|
return $acc;
|
2023-09-06 17:16:25 +03:00
|
|
|
}, []);
|
|
|
|
break;
|
|
|
|
case 'manga':
|
|
|
|
$json = array_reduce($json, function ($acc, $i) {
|
|
|
|
if ($i['illustType'] === 1) {
|
|
|
|
$acc[] = $i;
|
|
|
|
}return $acc;
|
|
|
|
}, []);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 03:46:57 +03:00
|
|
|
}
|
|
|
|
return $json;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
private function collectWorksArray()
|
|
|
|
{
|
2023-09-06 17:16:25 +03:00
|
|
|
$content = $this->getData($this->getSearchURI($this->getInput('mode')), true, true);
|
2022-05-08 03:46:57 +03:00
|
|
|
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);
|
2021-05-24 22:42:39 +03:00
|
|
|
}
|
2022-05-08 03:46:57 +03:00
|
|
|
$content = $total;
|
|
|
|
} else {
|
|
|
|
$json_key = self::JSON_KEY_MAP[$this->queriedContext][$this->getInput('mode')];
|
|
|
|
$content = $this->getDataFromJSON($content, $json_key);
|
|
|
|
}
|
|
|
|
return $content;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
2023-09-06 17:16:25 +03:00
|
|
|
$this->checkOptions();
|
|
|
|
$proxy_url = $this->getOption('proxy_url');
|
|
|
|
$proxy_url = $proxy_url ? rtrim($proxy_url, '/') : null;
|
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
$content = $this->collectWorksArray();
|
|
|
|
$content = array_filter($content, function ($v, $k) {
|
|
|
|
return !array_key_exists('isAdContainer', $v);
|
|
|
|
}, ARRAY_FILTER_USE_BOTH);
|
2023-09-06 17:16:25 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
// Sort by updateDate to get newest works
|
|
|
|
usort($content, function ($a, $b) {
|
|
|
|
return $b['updateDate'] <=> $a['updateDate'];
|
|
|
|
});
|
2023-09-06 17:16:25 +03:00
|
|
|
|
|
|
|
//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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
$content = array_slice($content, 0, $this->getInput('posts'));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
foreach ($content as $result) {
|
|
|
|
// Store username for getName()
|
|
|
|
if (!$this->username) {
|
|
|
|
$this->username = $result['userName'];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-04-14 18:19:35 +03:00
|
|
|
$item = [];
|
2022-05-08 03:46:57 +03:00
|
|
|
$item['uid'] = $result['id'];
|
2024-04-04 20:12:04 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
$subpath = array_key_exists('illustType', $result) ? 'artworks/' : 'novel/show.php?id=';
|
|
|
|
$item['uri'] = static::URI . $subpath . $result['id'];
|
2024-04-04 20:12:04 +03:00
|
|
|
|
2021-05-24 22:42:39 +03:00
|
|
|
$item['title'] = $result['title'];
|
2018-06-30 00:55:33 +03:00
|
|
|
$item['author'] = $result['userName'];
|
2021-05-24 22:42:39 +03:00
|
|
|
$item['timestamp'] = $result['updateDate'];
|
2022-06-09 06:05:56 +03:00
|
|
|
$item['categories'] = $result['tags'];
|
2023-09-06 17:16:25 +03:00
|
|
|
|
|
|
|
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 {
|
2024-01-09 22:33:35 +03:00
|
|
|
$img_url = $result['url'];
|
2023-09-06 17:16:25 +03:00
|
|
|
}
|
2024-01-09 22:33:35 +03:00
|
|
|
|
|
|
|
// 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);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
// Additional content items
|
|
|
|
if (array_key_exists('pageCount', $result)) {
|
|
|
|
$item['content'] .= '<br>Page Count: ' . $result['pageCount'];
|
|
|
|
} else {
|
|
|
|
$item['content'] .= '<br>Word Count: ' . $result['wordCount'];
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-04-14 18:19:35 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2023-09-06 17:16:25 +03:00
|
|
|
private function checkOptions()
|
|
|
|
{
|
|
|
|
$proxy = $this->getOption('proxy_url');
|
|
|
|
if ($proxy) {
|
|
|
|
if (
|
|
|
|
!(strlen($proxy) > 0 && preg_match('/https?:\/\/.*/', $proxy))
|
|
|
|
) {
|
2024-01-09 22:33:35 +03:00
|
|
|
returnServerError('Invalid proxy_url value set. The proxy must include the HTTP/S at the beginning of the url.');
|
2023-09-06 17:16:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$cookie = $this->getCookie();
|
|
|
|
if ($cookie) {
|
|
|
|
$isAuth = $this->loadCacheValue('is_authenticated');
|
|
|
|
if (!$isAuth) {
|
2024-01-09 22:33:35 +03:00
|
|
|
$res = $this->getData('https://www.pixiv.net/ajax/webpush', true, true);
|
2023-09-06 17:16:25 +03:00
|
|
|
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
|
2023-09-11 00:35:40 +03:00
|
|
|
$value = $this->loadCacheValue('cookie');
|
2023-09-06 17:16:25 +03:00
|
|
|
if (!isset($value)) {
|
|
|
|
$value = $this->getOption('cookie');
|
2023-09-11 00:35:40 +03:00
|
|
|
|
|
|
|
// 30 days + 1 day to let cookie chance to renew
|
|
|
|
$this->saveCacheValue('cookie', $this->getOption('cookie'), 2678400);
|
2023-09-06 17:16:25 +03:00
|
|
|
}
|
|
|
|
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) {
|
2024-07-31 18:30:06 +03:00
|
|
|
$response = $this->loadCacheValue($url);
|
|
|
|
if (!$response || is_array($response)) {
|
|
|
|
$response = getContents($url, $httpHeaders, $curlOptions, true);
|
|
|
|
$this->saveCacheValue($url, $response);
|
2023-09-06 17:16:25 +03:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-31 18:30:06 +03:00
|
|
|
$response = getContents($url, $httpHeaders, $curlOptions, true);
|
2023-09-06 17:16:25 +03:00
|
|
|
}
|
|
|
|
|
2024-07-31 18:30:06 +03:00
|
|
|
$this->checkCookie($response->getHeaders());
|
2023-09-06 17:16:25 +03:00
|
|
|
|
|
|
|
if ($getJSON) {
|
2024-07-31 18:30:06 +03:00
|
|
|
return json_decode($response->getBody(), true);
|
2023-09-06 17:16:25 +03:00
|
|
|
}
|
2024-07-31 18:30:06 +03:00
|
|
|
return $response->getBody();
|
2023-09-06 17:16:25 +03:00
|
|
|
}
|
2018-04-14 18:19:35 +03:00
|
|
|
}
|