不要な成否判定の削除

ref: c0b76e5
This commit is contained in:
eai04191 2019-09-10 07:03:46 +09:00
parent fb6c1a0574
commit 033784bfc8
17 changed files with 257 additions and 335 deletions

View File

@ -25,7 +25,6 @@ class CienResolver extends MetadataResolver
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
// 画像URLから有効期限の起点を拾う // 画像URLから有効期限の起点を拾う
@ -36,8 +35,5 @@ class CienResolver extends MetadataResolver
$metadata->expires_at = Carbon::createFromTimestamp($params['px-time'])->addHour(1); $metadata->expires_at = Carbon::createFromTimestamp($params['px-time'])->addHour(1);
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -73,7 +73,6 @@ class DLsiteResolver implements Resolver
} }
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$dom = new \DOMDocument(); $dom = new \DOMDocument();
@ -124,8 +123,5 @@ class DLsiteResolver implements Resolver
$metadata->tags = $this->extractTags($res->getBody()); $metadata->tags = $this->extractTags($res->getBody());
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -24,7 +24,6 @@ class FC2ContentsResolver implements Resolver
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$dom = new \DOMDocument(); $dom = new \DOMDocument();
@ -37,8 +36,5 @@ class FC2ContentsResolver implements Resolver
} }
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -28,7 +28,6 @@ class FantiaResolver implements Resolver
$postId = $match[0]; $postId = $match[0];
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$dom = new \DOMDocument(); $dom = new \DOMDocument();
@ -49,8 +48,5 @@ class FantiaResolver implements Resolver
} }
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -24,14 +24,10 @@ class FanzaResolver implements Resolver
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$metadata->image = preg_replace("~(pr|ps)\.jpg$~", 'pl.jpg', $metadata->image); $metadata->image = preg_replace("~(pr|ps)\.jpg$~", 'pl.jpg', $metadata->image);
$metadata->description = str_replace('<>', '', $metadata->description); $metadata->description = str_replace('<>', '', $metadata->description);
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -20,7 +20,6 @@ class IwaraResolver implements Resolver
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = new Metadata(); $metadata = new Metadata();
$html = (string) $res->getBody(); $html = (string) $res->getBody();
$crawler = new Crawler($html); $crawler = new Crawler($html);
@ -57,8 +56,5 @@ class IwaraResolver implements Resolver
} }
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -24,14 +24,12 @@ class KomifloResolver implements Resolver
$id = $matches[1]; $id = $matches[1];
$res = $this->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); $json = json_decode($res->getBody()->getContents(), true);
$metadata = new Metadata(); $metadata = new Metadata();
$metadata->title = $json['content']['data']['title'] ?? ''; $metadata->title = $json['content']['data']['title'] ?? '';
$metadata->description = ($json['content']['attributes']['artists']['children'][0]['data']['name'] ?? '?') . $metadata->description = ($json['content']['attributes']['artists']['children'][0]['data']['name'] ?? '?') .
' - ' . ' - ' . ($json['content']['parents'][0]['data']['title'] ?? '?');
($json['content']['parents'][0]['data']['title'] ?? '?');
$metadata->image = 'https://t.komiflo.com/564_mobile_large_3x/' . $json['content']['named_imgs']['cover']['filename']; $metadata->image = 'https://t.komiflo.com/564_mobile_large_3x/' . $json['content']['named_imgs']['cover']['filename'];
// 作者情報 // 作者情報
@ -49,8 +47,5 @@ class KomifloResolver implements Resolver
} }
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -27,7 +27,6 @@ class MelonbooksResolver implements Resolver
$cookieJar = CookieJar::fromArray(['AUTH_ADULT' => '1'], 'www.melonbooks.co.jp'); $cookieJar = CookieJar::fromArray(['AUTH_ADULT' => '1'], 'www.melonbooks.co.jp');
$res = $this->client->get($url, ['cookies' => $cookieJar]); $res = $this->client->get($url, ['cookies' => $cookieJar]);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$dom = new \DOMDocument(); $dom = new \DOMDocument();
@ -65,8 +64,5 @@ class MelonbooksResolver implements Resolver
$metadata->description = trim($description); $metadata->description = trim($description);
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -27,7 +27,6 @@ class NarouResolver implements Resolver
$cookieJar = CookieJar::fromArray(['over18' => 'yes'], '.syosetu.com'); $cookieJar = CookieJar::fromArray(['over18' => 'yes'], '.syosetu.com');
$res = $this->client->get($url, ['cookies' => $cookieJar]); $res = $this->client->get($url, ['cookies' => $cookieJar]);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$metadata->description = ''; $metadata->description = '';
@ -53,8 +52,5 @@ class NarouResolver implements Resolver
$metadata->description = implode(' / ', $description); $metadata->description = implode(' / ', $description);
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -32,7 +32,6 @@ class NijieResolver implements Resolver
} }
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$html = (string) $res->getBody(); $html = (string) $res->getBody();
$metadata = $this->ogpResolver->parse($html); $metadata = $this->ogpResolver->parse($html);
$crawler = new Crawler($html); $crawler = new Crawler($html);
@ -56,8 +55,5 @@ class NijieResolver implements Resolver
$metadata->tags = $crawler->filter('#view-tag span.tag_name')->extract('_text'); $metadata->tags = $crawler->filter('#view-tag span.tag_name')->extract('_text');
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -19,11 +19,7 @@ class OGPResolver implements Resolver, Parser
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
return $this->parse($res->getBody()); return $this->parse($res->getBody());
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
public function parse(string $html): Metadata public function parse(string $html): Metadata

View File

@ -25,7 +25,6 @@ class PatreonResolver implements Resolver
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
parse_str(parse_url($metadata->image, PHP_URL_QUERY), $query); parse_str(parse_url($metadata->image, PHP_URL_QUERY), $query);
@ -35,8 +34,5 @@ class PatreonResolver implements Resolver
} }
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -38,14 +38,10 @@ class PixivResolver implements Resolver
{ {
if (preg_match('~www\.pixiv\.net/user/\d+/series/\d+~', $url, $matches)) { if (preg_match('~www\.pixiv\.net/user/\d+/series/\d+~', $url, $matches)) {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$metadata->image = $this->proxize($metadata->image); $metadata->image = $this->proxize($metadata->image);
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
parse_str(parse_url($url, PHP_URL_QUERY), $params); parse_str(parse_url($url, PHP_URL_QUERY), $params);
@ -58,7 +54,6 @@ class PixivResolver implements Resolver
} }
$res = $this->client->get('https://www.pixiv.net/ajax/illust/' . $illustId); $res = $this->client->get('https://www.pixiv.net/ajax/illust/' . $illustId);
if ($res->getStatusCode() === 200) {
$json = json_decode($res->getBody()->getContents(), true); $json = json_decode($res->getBody()->getContents(), true);
$metadata = new Metadata(); $metadata = new Metadata();
@ -82,8 +77,5 @@ class PixivResolver implements Resolver
} }
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -24,7 +24,6 @@ class PlurkResolver implements Resolver
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$dom = new \DOMDocument(); $dom = new \DOMDocument();
@ -37,8 +36,5 @@ class PlurkResolver implements Resolver
} }
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -24,7 +24,6 @@ class SteamResolver implements Resolver
$appid = $matches[1]; $appid = $matches[1];
$res = $this->client->get('https://store.steampowered.com/api/appdetails/?l=japanese&appids=' . $appid); $res = $this->client->get('https://store.steampowered.com/api/appdetails/?l=japanese&appids=' . $appid);
if ($res->getStatusCode() === 200) {
$json = json_decode($res->getBody()->getContents(), true); $json = json_decode($res->getBody()->getContents(), true);
if ($json[$appid]['success'] === false) { if ($json[$appid]['success'] === false) {
throw new \RuntimeException("API response [$appid][success] is false: $url"); throw new \RuntimeException("API response [$appid][success] is false: $url");
@ -37,8 +36,5 @@ class SteamResolver implements Resolver
$metadata->image = $data['header_image'] ?? ''; $metadata->image = $data['header_image'] ?? '';
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -25,7 +25,6 @@ class ToranoanaResolver implements Resolver
public function resolve(string $url): Metadata public function resolve(string $url): Metadata
{ {
$res = $this->client->get($url); $res = $this->client->get($url);
if ($res->getStatusCode() === 200) {
$metadata = $this->ogpResolver->parse($res->getBody()); $metadata = $this->ogpResolver->parse($res->getBody());
$dom = new \DOMDocument(); $dom = new \DOMDocument();
@ -37,8 +36,5 @@ class ToranoanaResolver implements Resolver
} }
return $metadata; return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
}
} }
} }

View File

@ -3,21 +3,12 @@
namespace Tests\Unit\MetadataResolver; namespace Tests\Unit\MetadataResolver;
use App\MetadataResolver\OGPResolver; use App\MetadataResolver\OGPResolver;
use GuzzleHttp\Exception\ClientException;
use Tests\TestCase; use Tests\TestCase;
class OGPResolverTest extends TestCase class OGPResolverTest extends TestCase
{ {
use CreateMockedResolver; use CreateMockedResolver;
public function testMissingUrl()
{
$this->createResolver(OGPResolver::class, '', [], 404);
$this->expectException(\RuntimeException::class);
$this->resolver->resolve('http://example.com/404');
}
public function testResolve() public function testResolve()
{ {
$response = <<< 'HTML' $response = <<< 'HTML'