From cedee0a20e073cdf0c98ae4133d0bfc93e89ce63 Mon Sep 17 00:00:00 2001 From: Eai Date: Tue, 12 Feb 2019 22:48:44 +0900 Subject: [PATCH] =?UTF-8?q?pixiv=E3=81=AEOGP=E7=94=BB=E5=83=8F=E3=81=A7?= =?UTF-8?q?=E5=B8=B8=E3=81=AB=E3=83=97=E3=83=AD=E3=82=AD=E3=82=B7URL?= =?UTF-8?q?=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B=20(#92)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/MetadataResolver/PixivResolver.php | 54 +++++++------------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/app/MetadataResolver/PixivResolver.php b/app/MetadataResolver/PixivResolver.php index 41a8ba4..16c89d9 100644 --- a/app/MetadataResolver/PixivResolver.php +++ b/app/MetadataResolver/PixivResolver.php @@ -8,6 +8,7 @@ class PixivResolver implements Resolver * サムネイル画像 URL から最大長辺 1200px の画像 URL に変換する * * @param string $thumbnailUrl サムネイル画像 URL + * * @return string 1200px の画像 URL */ public function thumbnailToMasterUrl(string $thumbnailUrl): string @@ -23,6 +24,7 @@ class PixivResolver implements Resolver * HUGE THANKS TO PIXIV.CAT! * * @param string $pixivUrl i.pximg URL + * * @return string i.pixiv.cat URL */ public function proxize(string $pixivUrl): string @@ -41,52 +43,24 @@ class PixivResolver implements Resolver // 未ログインでは漫画ページを開けないため、URL を作品ページに変換する $url = preg_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()); + $client = new \GuzzleHttp\Client(); + $res = $client->get($url); + if ($res->getStatusCode() === 200) { + $ogpResolver = new OGPResolver(); + $metadata = $ogpResolver->parse($res->getBody()); - 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]; + 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]; - $illustUrl = $this->thumbnailToMasterUrl($illustThumbnailUrl); + $illustUrl = $this->thumbnailToMasterUrl($illustThumbnailUrl); - // 指定ページに変換 - $illustUrl = str_replace('p0_master', "p{$page}_master", $illustUrl); + $metadata->image = $this->proxize($illustUrl); - $metadata->image = $this->proxize($illustUrl); - - return $metadata; - } else { - throw new \RuntimeException("{$res->getStatusCode()}: $url"); - } + return $metadata; } else { - $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')) { - - // 作品ページの場合のみ対応 - if ($params['mode'] === 'medium') { - 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]; - - $illustUrl = $this->thumbnailToMasterUrl($illustThumbnailUrl); - - $metadata->image = $this->proxize($illustUrl); - } - } - - return $metadata; - } else { - throw new \RuntimeException("{$res->getStatusCode()}: $url"); - } + throw new \RuntimeException("{$res->getStatusCode()}: $url"); } } }