From 24dee801ad0a75ea3c1d9e4db7f91042367caa5b Mon Sep 17 00:00:00 2001 From: hina Date: Sun, 17 Feb 2019 02:58:36 +0900 Subject: [PATCH] =?UTF-8?q?Guzzle\Client=E3=82=92=E3=83=A2=E3=83=83?= =?UTF-8?q?=E3=82=AB=E3=83=96=E3=83=AB=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Listeners/LinkCollector.php | 3 ++- app/MetadataResolver/CienResolver.php | 22 ++++++++++++++---- app/MetadataResolver/DLsiteResolver.php | 25 ++++++++++++++++----- app/MetadataResolver/DeviantArtResolver.php | 23 +++++++++++++++---- app/MetadataResolver/FantiaResolver.php | 21 +++++++++++++---- app/MetadataResolver/FanzaResolver.php | 25 ++++++++++++++++----- app/MetadataResolver/IwaraResolver.php | 16 +++++++++++-- app/MetadataResolver/KomifloResolver.php | 15 +++++++++++-- app/MetadataResolver/MelonbooksResolver.php | 22 ++++++++++++++---- app/MetadataResolver/MetadataResolver.php | 8 ++++--- app/MetadataResolver/NarouResolver.php | 22 ++++++++++++++---- app/MetadataResolver/NicoSeigaResolver.php | 23 +++++++++++++++---- app/MetadataResolver/NijieResolver.php | 12 +++++++++- app/MetadataResolver/OGPResolver.php | 15 +++++++++++-- app/MetadataResolver/PatreonResolver.php | 24 +++++++++++++++----- app/MetadataResolver/PixivResolver.php | 23 +++++++++++++++---- app/MetadataResolver/ToranoanaResolver.php | 22 ++++++++++++++---- 17 files changed, 263 insertions(+), 58 deletions(-) diff --git a/app/Listeners/LinkCollector.php b/app/Listeners/LinkCollector.php index 0484f95..0354f65 100644 --- a/app/Listeners/LinkCollector.php +++ b/app/Listeners/LinkCollector.php @@ -21,7 +21,8 @@ class LinkCollector /** * Create the event listener. * - * @return void + * @param Formatter $formatter + * @param MetadataResolver $metadataResolver */ public function __construct(Formatter $formatter, MetadataResolver $metadataResolver) { diff --git a/app/MetadataResolver/CienResolver.php b/app/MetadataResolver/CienResolver.php index 73f8c0e..5af47c3 100644 --- a/app/MetadataResolver/CienResolver.php +++ b/app/MetadataResolver/CienResolver.php @@ -3,16 +3,30 @@ namespace App\MetadataResolver; use Carbon\Carbon; +use GuzzleHttp\Client; class CienResolver extends MetadataResolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); + $metadata = $this->ogpResolver->parse($res->getBody()); // 画像URLから有効期限の起点を拾う parse_str(parse_url($metadata->image, PHP_URL_QUERY), $params); diff --git a/app/MetadataResolver/DLsiteResolver.php b/app/MetadataResolver/DLsiteResolver.php index fc0d522..99aae49 100644 --- a/app/MetadataResolver/DLsiteResolver.php +++ b/app/MetadataResolver/DLsiteResolver.php @@ -2,16 +2,31 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class DLsiteResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); - $metadata->image = str_replace('img_sam.jpg', 'img_main.jpg', $metadata->image); + $metadata = $this->ogpResolver->parse($res->getBody()); + $metadata->image = str_replace('img_sam.jpg', 'img_main.jpg', $metadata->image); return $metadata; } else { diff --git a/app/MetadataResolver/DeviantArtResolver.php b/app/MetadataResolver/DeviantArtResolver.php index fb939d5..2ef5898 100644 --- a/app/MetadataResolver/DeviantArtResolver.php +++ b/app/MetadataResolver/DeviantArtResolver.php @@ -2,15 +2,30 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class DeviantArtResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); + $metadata = $this->ogpResolver->parse($res->getBody()); $dom = new \DOMDocument(); @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8')); diff --git a/app/MetadataResolver/FantiaResolver.php b/app/MetadataResolver/FantiaResolver.php index 0a5e5b3..63af366 100644 --- a/app/MetadataResolver/FantiaResolver.php +++ b/app/MetadataResolver/FantiaResolver.php @@ -2,20 +2,33 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; use Illuminate\Support\Facades\Log; class FantiaResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } public function resolve(string $url): Metadata { preg_match("~\d+~", $url, $match); $postId = $match[0]; - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); + $metadata = $this->ogpResolver->parse($res->getBody()); $dom = new \DOMDocument(); @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8')); diff --git a/app/MetadataResolver/FanzaResolver.php b/app/MetadataResolver/FanzaResolver.php index de06b75..cfb73db 100644 --- a/app/MetadataResolver/FanzaResolver.php +++ b/app/MetadataResolver/FanzaResolver.php @@ -2,16 +2,31 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class FanzaResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); - $metadata->image = preg_replace("~(pr|ps)\.jpg$~", 'pl.jpg', $metadata->image); + $metadata = $this->ogpResolver->parse($res->getBody()); + $metadata->image = preg_replace("~(pr|ps)\.jpg$~", 'pl.jpg', $metadata->image); return $metadata; } else { diff --git a/app/MetadataResolver/IwaraResolver.php b/app/MetadataResolver/IwaraResolver.php index 60aaf1b..361501d 100644 --- a/app/MetadataResolver/IwaraResolver.php +++ b/app/MetadataResolver/IwaraResolver.php @@ -2,12 +2,24 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class IwaraResolver implements Resolver { + /** + * @var Client + */ + private $client; + + public function __construct(Client $client) + { + $this->client = $client; + } + public function resolve(string $url): Metadata { - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); + if ($res->getStatusCode() === 200) { $dom = new \DOMDocument(); @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8')); diff --git a/app/MetadataResolver/KomifloResolver.php b/app/MetadataResolver/KomifloResolver.php index b09396a..f3007d3 100644 --- a/app/MetadataResolver/KomifloResolver.php +++ b/app/MetadataResolver/KomifloResolver.php @@ -2,8 +2,20 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class KomifloResolver implements Resolver { + /** + * @var Client + */ + private $client; + + public function __construct(Client $client) + { + $this->client = $client; + } + public function resolve(string $url): Metadata { if (preg_match('~komiflo\.com(?:/#!)?/comics/(\\d+)~', $url, $matches) !== 1) { @@ -11,8 +23,7 @@ class KomifloResolver implements Resolver } $id = $matches[1]; - $client = new \GuzzleHttp\Client(); - $res = $client->get('https://api.komiflo.com/content/id/' . $id); + $res = $this->client->get('https://api.komiflo.com/content/id/' . $id); if ($res->getStatusCode() === 200) { $json = json_decode($res->getBody()->getContents(), true); $metadata = new Metadata(); diff --git a/app/MetadataResolver/MelonbooksResolver.php b/app/MetadataResolver/MelonbooksResolver.php index 2e8b7fb..5cc3669 100644 --- a/app/MetadataResolver/MelonbooksResolver.php +++ b/app/MetadataResolver/MelonbooksResolver.php @@ -2,19 +2,33 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; use GuzzleHttp\Cookie\CookieJar; class MelonbooksResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { $cookieJar = CookieJar::fromArray(['AUTH_ADULT' => '1'], 'www.melonbooks.co.jp'); - $client = new \GuzzleHttp\Client(); - $res = $client->get($url, ['cookies' => $cookieJar]); + $res = $this->client->get($url, ['cookies' => $cookieJar]); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); + $metadata = $this->ogpResolver->parse($res->getBody()); // censoredフラグの除去 if (mb_strpos($metadata->image, '&c=1') !== false) { diff --git a/app/MetadataResolver/MetadataResolver.php b/app/MetadataResolver/MetadataResolver.php index b55da22..5759bf3 100644 --- a/app/MetadataResolver/MetadataResolver.php +++ b/app/MetadataResolver/MetadataResolver.php @@ -37,7 +37,8 @@ class MetadataResolver implements Resolver { foreach ($this->rules as $pattern => $class) { if (preg_match($pattern, $url) === 1) { - $resolver = new $class(); + /** @var Resolver $resolver */ + $resolver = app($class); return $resolver->resolve($url); } @@ -49,13 +50,14 @@ class MetadataResolver implements Resolver } if (isset($this->defaultResolver)) { - $resolver = new $this->defaultResolver(); + /** @var Resolver $resolver */ + $resolver = app($this->defaultResolver); return $resolver->resolve($url); } throw new \UnexpectedValueException('URL not matched.'); } - + public function resolveWithAcceptHeader(string $url): ?Metadata { try { diff --git a/app/MetadataResolver/NarouResolver.php b/app/MetadataResolver/NarouResolver.php index 5f14d0a..6600b23 100644 --- a/app/MetadataResolver/NarouResolver.php +++ b/app/MetadataResolver/NarouResolver.php @@ -2,19 +2,33 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; use GuzzleHttp\Cookie\CookieJar; class NarouResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { $cookieJar = CookieJar::fromArray(['over18' => 'yes'], '.syosetu.com'); - $client = new \GuzzleHttp\Client(); - $res = $client->get($url, ['cookies' => $cookieJar]); + $res = $this->client->get($url, ['cookies' => $cookieJar]); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); + $metadata = $this->ogpResolver->parse($res->getBody()); $metadata->description = ''; $dom = new \DOMDocument(); diff --git a/app/MetadataResolver/NicoSeigaResolver.php b/app/MetadataResolver/NicoSeigaResolver.php index 3e6d5c0..3f2e000 100644 --- a/app/MetadataResolver/NicoSeigaResolver.php +++ b/app/MetadataResolver/NicoSeigaResolver.php @@ -2,15 +2,30 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class NicoSeigaResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); + $metadata = $this->ogpResolver->parse($res->getBody()); // ページURLからサムネイルURLに変換 preg_match('~http://(?:(?:sp\\.)?seiga\\.nicovideo\\.jp/seiga(?:/#!)?|nico\\.ms)/im(\\d+)~', $url, $matches); diff --git a/app/MetadataResolver/NijieResolver.php b/app/MetadataResolver/NijieResolver.php index 45b73de..c39f096 100644 --- a/app/MetadataResolver/NijieResolver.php +++ b/app/MetadataResolver/NijieResolver.php @@ -2,8 +2,18 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class NijieResolver implements Resolver { + /** @var Client */ + protected $client; + + public function __construct(Client $client) + { + $this->client = $client; + } + public function resolve(string $url): Metadata { if (mb_strpos($url, '//sp.nijie.info') !== false) { @@ -13,7 +23,7 @@ class NijieResolver implements Resolver $url = preg_replace('~view_popup\.php~', 'view.php', $url); } - $client = new \GuzzleHttp\Client(); + $client = $this->client; $res = $client->get($url); if ($res->getStatusCode() === 200) { $ogpResolver = new OGPResolver(); diff --git a/app/MetadataResolver/OGPResolver.php b/app/MetadataResolver/OGPResolver.php index 1cf4c1e..f8e185e 100644 --- a/app/MetadataResolver/OGPResolver.php +++ b/app/MetadataResolver/OGPResolver.php @@ -2,12 +2,23 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class OGPResolver implements Resolver, Parser { + /** + * @var Client + */ + private $client; + + public function __construct(Client $client) + { + $this->client = $client; + } + public function resolve(string $url): Metadata { - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { return $this->parse($res->getBody()); } else { diff --git a/app/MetadataResolver/PatreonResolver.php b/app/MetadataResolver/PatreonResolver.php index 78f461a..5ce28e7 100644 --- a/app/MetadataResolver/PatreonResolver.php +++ b/app/MetadataResolver/PatreonResolver.php @@ -3,19 +3,33 @@ namespace App\MetadataResolver; use Carbon\Carbon; +use GuzzleHttp\Client; class PatreonResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); + $metadata = $this->ogpResolver->parse($res->getBody()); parse_str(parse_url($metadata->image, PHP_URL_QUERY), $temp); - $expires_at_unixtime = $temp["token-time"]; + $expires_at_unixtime = $temp['token-time']; $expires_at = Carbon::createFromTimestamp($expires_at_unixtime); $metadata->expires_at = $expires_at; diff --git a/app/MetadataResolver/PixivResolver.php b/app/MetadataResolver/PixivResolver.php index 16c89d9..094fcd2 100644 --- a/app/MetadataResolver/PixivResolver.php +++ b/app/MetadataResolver/PixivResolver.php @@ -2,8 +2,25 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; + class PixivResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + /** * サムネイル画像 URL から最大長辺 1200px の画像 URL に変換する * @@ -45,11 +62,9 @@ class PixivResolver implements Resolver $url = preg_replace('~mode=manga(_big)?~', 'mode=medium', $url); } - $client = new \GuzzleHttp\Client(); - $res = $client->get($url); + $res = $this->client->get($url); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - $metadata = $ogpResolver->parse($res->getBody()); + $metadata = $this->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]; diff --git a/app/MetadataResolver/ToranoanaResolver.php b/app/MetadataResolver/ToranoanaResolver.php index 968fff6..27b900e 100644 --- a/app/MetadataResolver/ToranoanaResolver.php +++ b/app/MetadataResolver/ToranoanaResolver.php @@ -2,20 +2,34 @@ namespace App\MetadataResolver; +use GuzzleHttp\Client; use GuzzleHttp\Cookie\CookieJar; class ToranoanaResolver implements Resolver { + /** + * @var Client + */ + private $client; + /** + * @var OGPResolver + */ + private $ogpResolver; + + public function __construct(Client $client, OGPResolver $ogpResolver) + { + $this->client = $client; + $this->ogpResolver = $ogpResolver; + } + public function resolve(string $url): Metadata { $cookieJar = CookieJar::fromArray(['adflg' => '0'], 'ec.toranoana.jp'); - $client = new \GuzzleHttp\Client(); - $res = $client->get($url, ['cookies' => $cookieJar]); + $res = $this->client->get($url, ['cookies' => $cookieJar]); if ($res->getStatusCode() === 200) { - $ogpResolver = new OGPResolver(); - return $ogpResolver->parse($res->getBody()); + return $this->ogpResolver->parse($res->getBody()); } else { throw new \RuntimeException("{$res->getStatusCode()}: $url"); }