From d9bf673d8571bbcfb2933b4f855ef4c083f6f623 Mon Sep 17 00:00:00 2001 From: shibafu Date: Mon, 4 Feb 2019 02:35:50 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E8=80=85=E5=90=8D=E3=81=A8=E3=81=82?= =?UTF-8?q?=E3=82=89=E3=81=99=E3=81=98=E3=81=AE=E5=8F=96=E5=BE=97=E3=82=92?= =?UTF-8?q?=E8=A9=A6=E3=81=BF=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/MetadataResolver/NarouResolver.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/MetadataResolver/NarouResolver.php b/app/MetadataResolver/NarouResolver.php index cc8f808..5f14d0a 100644 --- a/app/MetadataResolver/NarouResolver.php +++ b/app/MetadataResolver/NarouResolver.php @@ -14,8 +14,31 @@ class NarouResolver implements Resolver $res = $client->get($url, ['cookies' => $cookieJar]); if ($res->getStatusCode() === 200) { $ogpResolver = new OGPResolver(); + $metadata = $ogpResolver->parse($res->getBody()); + $metadata->description = ''; - return $ogpResolver->parse($res->getBody()); + $dom = new \DOMDocument(); + @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'ASCII,JIS,UTF-8,eucJP-win,SJIS-win')); + $xpath = new \DOMXPath($dom); + + $description = []; + + // 作者名 + $writerNodes = $xpath->query('//*[contains(@class, "novel_writername")]'); + if ($writerNodes->length !== 0 && !empty($writerNodes->item(0)->textContent)) { + $description[] = trim($writerNodes->item(0)->textContent); + } + + // あらすじ + $exNodes = $xpath->query('//*[@id="novel_ex"]'); + if ($exNodes->length !== 0 && !empty($exNodes->item(0)->textContent)) { + $summary = trim($exNodes->item(0)->textContent); + $description[] = mb_strimwidth($summary, 0, 101, '…'); // 100 + '…'(1) + } + + $metadata->description = implode(' / ', $description); + + return $metadata; } else { throw new \RuntimeException("{$res->getStatusCode()}: $url"); }