1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-01-17 22:28:22 +01:00

[PixivBridge] Rewrite Bridge (#2111)

Also added options:
- Search for Illustrations, Manga or Novels
- Custom Post Limit
- Choose between thumbnails and full-sized image
This commit is contained in:
Yaman Qalieh 2021-05-24 15:42:39 -04:00 committed by GitHub
parent 63d257d9d0
commit 44e01a4282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,72 +1,113 @@
<?php <?php
class PixivBridge extends BridgeAbstract { class PixivBridge extends BridgeAbstract {
const MAINTAINER = 'teromene'; const MAINTAINER = 'Yaman Qalieh';
const NAME = 'Pixiv Bridge'; const NAME = 'Pixiv Bridge';
const URI = 'https://www.pixiv.net/'; const URI = 'https://www.pixiv.net/';
const DESCRIPTION = 'Returns the tag search from pixiv.net'; const DESCRIPTION = 'Returns the tag search from pixiv.net';
const CACHE_TIMEOUT = 21600; // 6h
const PARAMETERS = array( array( const PARAMETERS = array( array(
'mode' => array(
'name' => 'Post Type',
'type' => 'list',
'values' => array('Illustration' => 'illustrations/',
'Manga' => 'manga/',
'Novel' => 'novels/')
),
'tag' => array( 'tag' => array(
'name' => 'Tag to search', 'name' => 'Query to search',
'exampleValue' => 'example', 'exampleValue' => '葬送のフリーレン',
'required' => true 'required' => true
), ),
'posts' => array(
'name' => 'Post Limit',
'type' => 'number',
'defaultValue' => '10'
),
'fullsize' => array(
'name' => 'Full-size Image',
'type' => 'checkbox'
)
)); ));
public function collectData(){ const JSON_KEY_MAP = array(
'illustrations/' => 'illust',
'manga/' => 'manga',
'novels/' => 'novel'
);
const WORK_LINK_MAP = array(
'illustrations/' => 'artworks/',
'manga/' => 'artworks/',
'novels/' => 'novel/show.php?id='
);
$html = getContents(static::URI . 'search.php?word=' . urlencode($this->getInput('tag'))) public function collectData() {
or returnClientError('Unable to query pixiv.net'); $content = getContents($this->getSearchURI());
$regex = '/<input type="hidden"id="js-mount-point-search-result-list"data-items="([^"]*)/'; $content = json_decode($content, true);
$timeRegex = '/img\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([0-9]{2})\/([0-9]{2})\/([0-9]{2})\//';
preg_match_all($regex, $html, $matches, PREG_SET_ORDER, 0); $key = self::JSON_KEY_MAP[$this->getInput('mode')];
if(!$matches) return;
$content = json_decode(html_entity_decode($matches[0][1]), true);
$count = 0; $count = 0;
foreach($content as $result) { foreach($content['body'][$key]['data'] as $result) {
if($count == 10) break;
$count++; $count++;
if ($count > $this->getInput('posts')) {
break;
}
$item = array(); $item = array();
$item['id'] = $result['illustId']; $item['id'] = $result['id'];
$item['uri'] = 'https://www.pixiv.net/member_illust.php?mode=medium&illust_id=' . $result['illustId']; $item['uri'] = static::URI . self::WORK_LINK_MAP[$this->getInput('mode')] . $result['id'];
$item['title'] = $result['illustTitle']; $item['title'] = $result['title'];
$item['author'] = $result['userName']; $item['author'] = $result['userName'];
$item['timestamp'] = $result['updateDate'];
preg_match_all($timeRegex, $result['url'], $dt, PREG_SET_ORDER, 0);
$elementDate = DateTime::createFromFormat('YmdHis',
$dt[0][1] . $dt[0][2] . $dt[0][3] . $dt[0][4] . $dt[0][5] . $dt[0][6],
new DateTimeZone('Asia/Tokyo'));
$item['timestamp'] = $elementDate->getTimestamp();
$item['content'] = "<img src='" . $this->cacheImage($result['url'], $item['id']) . "' />"; $item['content'] = "<img src='" . $this->cacheImage($result['url'], $item['id']) . "' />";
$this->items[] = $item; $this->items[] = $item;
} }
} }
private function getSearchURI() {
$query = urlencode($this->getInput('tag'));
$uri = static::URI . 'ajax/search/' . $this->getInput('mode')
. $query . '?word=' . $query . '&order=date_d&mode=all&p=1';
return $uri;
}
private function cacheImage($url, $illustId) { private function cacheImage($url, $illustId) {
$illustId = preg_replace('/[^0-9]/', '', $illustId);
$thumbnailurl = $url;
$url = str_replace('_master1200', '', $url);
$url = str_replace('c/240x240/img-master/', 'img-original/', $url);
$path = PATH_CACHE . 'pixiv_img/'; $path = PATH_CACHE . 'pixiv_img/';
if(!is_dir($path)) if(!is_dir($path))
mkdir($path, 0755, true); mkdir($path, 0755, true);
if(!is_file($path . '/' . $illustId . '.jpeg')) { $path .= $illustId;
$headers = array('Referer: https://www.pixiv.net/member_illust.php?mode=medium&illust_id=' . $illustId); if ($this->getInput('fullsize')) {
$illust = getContents($url, $headers); $path .= '_fullsize';
if(strpos($illust, '404 Not Found') !== false) { }
$illust = getContents(str_replace('jpg', 'png', $url), $headers); $path .= '.jpg';
if(!is_file($path)) {
// Get fullsize URL
if (!$this->getInput('mode') !== 'novels/' && $this->getInput('fullsize')) {
$ajax_uri = static::URI . 'ajax/illust/' . $illustId;
$imagejson = json_decode(getContents($ajax_uri), true);
$url = $imagejson['body']['urls']['original'];
} }
file_put_contents($path . '/' . $illustId . '.jpeg', $illust);
$headers = array('Referer: ' . static::URI);
try {
$illust = getContents($url, $headers);
} catch (Exception $e) {
$illust = getContents($thumbnailurl, $headers); // Original thumbnail
}
file_put_contents($path, $illust);
} }
return 'cache/pixiv_img/' . $illustId . '.jpeg'; return 'cache/pixiv_img/' . preg_replace('/.*\//', '', $path);
} }
} }