2019-01-14 16:01:58 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\MetadataResolver;
|
|
|
|
|
|
|
|
class PixivResolver implements Resolver
|
|
|
|
{
|
2019-01-14 16:11:14 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* サムネイル画像 URL から最大長辺 1200px の画像 URL に変換する
|
|
|
|
*
|
2019-01-14 18:34:33 +09:00
|
|
|
* @param string $thumbnailUrl サムネイル画像 URL
|
2019-01-14 16:11:14 +09:00
|
|
|
* @return string 1200px の画像 URL
|
|
|
|
*/
|
2019-01-14 18:34:33 +09:00
|
|
|
public function thumbnailToMasterUrl(string $thumbnailUrl):string
|
2019-01-14 16:11:14 +09:00
|
|
|
{
|
2019-01-14 18:34:33 +09:00
|
|
|
$temp = str_replace("/c/128x128", "", $thumbnailUrl);
|
|
|
|
$largeUrl = str_replace("square1200.jpg", "master1200.jpg", $temp);
|
|
|
|
return $largeUrl;
|
2019-01-14 16:01:58 +09:00
|
|
|
}
|
|
|
|
|
2019-01-14 16:11:14 +09:00
|
|
|
/**
|
|
|
|
* 直リン可能な pixiv.cat のプロキシ URL に変換する
|
|
|
|
* HUGE THANKS TO PIXIV.CAT!
|
|
|
|
*
|
2019-01-14 18:34:33 +09:00
|
|
|
* @param string $pixivUrl i.pximg URL
|
2019-01-14 16:11:14 +09:00
|
|
|
* @return string i.pixiv.cat URL
|
|
|
|
*/
|
2019-01-14 18:34:33 +09:00
|
|
|
public function proxize(string $pixivUrl):string
|
2019-01-14 16:11:14 +09:00
|
|
|
{
|
2019-01-14 18:34:33 +09:00
|
|
|
return str_replace("i.pximg.net", "i.pixiv.cat", $pixivUrl);
|
2019-01-14 16:01:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public function resolve(string $url): Metadata
|
|
|
|
{
|
2019-01-14 18:34:33 +09:00
|
|
|
preg_match("~illust_id=(\d+)~", parse_url($url)["query"], $match);
|
|
|
|
$illustId = $match[1];
|
2019-01-14 16:01:58 +09:00
|
|
|
|
|
|
|
// 漫画ページかつページ数あり
|
|
|
|
if (strpos(parse_url($url)["query"], "mode=manga_big") && strpos(parse_url($url)["query"], "page=")) {
|
|
|
|
preg_match("~page=(\d+)~", parse_url($url)["query"], $match);
|
|
|
|
$page = $match[1];
|
|
|
|
|
|
|
|
// 未ログインでは漫画ページを開けないため、URL を作品ページに変換する
|
|
|
|
$url = str_replace("mode=manga_big", "mode=medium", $url);
|
|
|
|
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
$res = $client->get($url);
|
|
|
|
if ($res->getStatusCode() === 200) {
|
|
|
|
$ogpResolver = new OGPResolver();
|
|
|
|
$metadata = $ogpResolver->parse($res->getBody());
|
|
|
|
|
2019-01-14 18:34:33 +09:00
|
|
|
preg_match("~https://i\.pximg\.net/c/128x128/img-master/img/\d{4}/\d{2}/\d{2}/\d{2}/\d{2}/\d{2}/{$illustId}_p0_square1200\.jpg~", $res->getBody(), $match);
|
|
|
|
$illustThumbnailUrl = $match[0];
|
2019-01-14 16:01:58 +09:00
|
|
|
|
2019-01-14 18:34:33 +09:00
|
|
|
$illustUrl = $this->thumbnailToMasterUrl($illustThumbnailUrl);
|
2019-01-14 16:01:58 +09:00
|
|
|
|
|
|
|
// 指定ページに変換
|
2019-01-14 18:34:33 +09:00
|
|
|
$illustUrl = str_replace("p0_master", "p{$page}_master", $illustUrl);
|
2019-01-14 16:01:58 +09:00
|
|
|
|
2019-01-14 18:34:33 +09:00
|
|
|
$metadata->image = $this->proxize($illustUrl);
|
2019-01-14 16:01:58 +09:00
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
2019-01-14 16:11:14 +09:00
|
|
|
} else {
|
2019-01-14 16:01:58 +09:00
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
$res = $client->get($url);
|
|
|
|
if ($res->getStatusCode() === 200) {
|
|
|
|
$ogpResolver = new OGPResolver();
|
|
|
|
$metadata = $ogpResolver->parse($res->getBody());
|
|
|
|
|
|
|
|
// OGP がデフォルト画像であるようならなんとかして画像を取得する
|
|
|
|
if (strpos($metadata->image, "pixiv_logo.gif") || strpos($metadata->image, "pictures.jpg")) {
|
|
|
|
|
|
|
|
// 作品ページの場合のみ対応
|
2019-01-14 16:11:14 +09:00
|
|
|
if (strpos(parse_url($url)["query"], "mode=medium")) {
|
2019-01-14 18:34:33 +09:00
|
|
|
preg_match("~https://i\.pximg\.net/c/128x128/img-master/img/\d{4}/\d{2}/\d{2}/\d{2}/\d{2}/\d{2}/{$illustId}(_p0)?_square1200\.jpg~", $res->getBody(), $match);
|
|
|
|
$illustThumbnailUrl = $match[0];
|
2019-01-14 16:01:58 +09:00
|
|
|
|
2019-01-14 18:34:33 +09:00
|
|
|
$illustUrl = $this->thumbnailToMasterUrl($illustThumbnailUrl);
|
2019-01-14 16:01:58 +09:00
|
|
|
|
2019-01-14 18:34:33 +09:00
|
|
|
$metadata->image = $this->proxize($illustUrl);
|
2019-01-14 16:01:58 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("{$res->getStatusCode()}: $url");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|