2018-04-14 18:19:35 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-04-14 18:19:35 +03:00
|
|
|
class PixivBridge extends BridgeAbstract
|
|
|
|
{
|
2022-05-08 03:46:57 +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
|
|
|
const NAME = 'Pixiv Bridge';
|
|
|
|
const URI = 'https://www.pixiv.net/';
|
|
|
|
const DESCRIPTION = 'Returns the tag search from pixiv.net';
|
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',
|
|
|
|
'values' => ['All Works' => 'all',
|
|
|
|
'Illustrations' => 'illustrations/',
|
|
|
|
'Manga' => 'manga/',
|
|
|
|
'Novels' => 'novels/']
|
2022-07-01 16:10:30 +03:00
|
|
|
],
|
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-03-06 22:01:51 +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')
|
|
|
|
. '/profile/top';
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$json = $json['body'][$json_key];
|
|
|
|
// Tags context contains subkey
|
2022-06-24 12:31:24 +03:00
|
|
|
if ($this->queriedContext == 'Tag') {
|
2022-05-08 03:46:57 +03:00
|
|
|
$json = $json['data'];
|
|
|
|
}
|
|
|
|
return $json;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-08 03:46:57 +03:00
|
|
|
private function collectWorksArray()
|
|
|
|
{
|
|
|
|
$content = getContents($this->getSearchURI($this->getInput('mode')));
|
|
|
|
$content = json_decode($content, 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);
|
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()
|
|
|
|
{
|
|
|
|
$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'];
|
|
|
|
});
|
|
|
|
$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'];
|
|
|
|
$subpath = array_key_exists('illustType', $result) ? 'artworks/' : 'novel/show.php?id=';
|
|
|
|
$item['uri'] = static::URI . $subpath . $result['id'];
|
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'];
|
2022-06-04 21:53:10 +03:00
|
|
|
$cached_image = $this->cacheImage(
|
|
|
|
$result['url'],
|
|
|
|
$result['id'],
|
|
|
|
array_key_exists('illustType', $result)
|
|
|
|
);
|
|
|
|
$item['content'] = "<img src='" . $cached_image . "' />";
|
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-03-20 21:10:01 +03:00
|
|
|
/**
|
|
|
|
* todo: remove manual file cache
|
|
|
|
*/
|
2022-06-04 21:53:10 +03:00
|
|
|
private function cacheImage($url, $illustId, $isImage)
|
|
|
|
{
|
2021-05-24 22:42:39 +03:00
|
|
|
$illustId = preg_replace('/[^0-9]/', '', $illustId);
|
|
|
|
$thumbnailurl = $url;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-10 21:48:05 +03:00
|
|
|
$path = PATH_CACHE . 'pixiv_img/';
|
2018-04-14 18:19:35 +03:00
|
|
|
if (!is_dir($path)) {
|
|
|
|
mkdir($path, 0755, true);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 22:42:39 +03:00
|
|
|
$path .= $illustId;
|
|
|
|
if ($this->getInput('fullsize')) {
|
|
|
|
$path .= '_fullsize';
|
2018-04-14 18:19:35 +03:00
|
|
|
}
|
2021-05-24 22:42:39 +03:00
|
|
|
$path .= '.jpg';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-05-24 22:42:39 +03:00
|
|
|
if (!is_file($path)) {
|
|
|
|
// Get fullsize URL
|
2022-06-04 21:53:10 +03:00
|
|
|
if ($isImage && $this->getInput('fullsize')) {
|
2021-05-24 22:42:39 +03:00
|
|
|
$ajax_uri = static::URI . 'ajax/illust/' . $illustId;
|
|
|
|
$imagejson = json_decode(getContents($ajax_uri), true);
|
|
|
|
$url = $imagejson['body']['urls']['original'];
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-05-24 22:42:39 +03:00
|
|
|
$headers = ['Referer: ' . static::URI];
|
|
|
|
try {
|
|
|
|
$illust = getContents($url, $headers);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$illust = getContents($thumbnailurl, $headers); // Original thumbnail
|
|
|
|
}
|
|
|
|
file_put_contents($path, $illust);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-09-02 21:35:17 +03:00
|
|
|
return get_home_page_url() . 'cache/pixiv_img/' . preg_replace('/.*\//', '', $path);
|
2018-04-14 18:19:35 +03:00
|
|
|
}
|
|
|
|
}
|