From 54477bb214be65b429acad9f5102ffe6e3d57273 Mon Sep 17 00:00:00 2001 From: eai04191 Date: Fri, 8 Mar 2019 06:58:30 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BD=99=E5=88=86=E3=81=AA=E6=96=87=E3=81=AE?= =?UTF-8?q?=E5=89=8A=E9=99=A4=E3=83=BB=E6=95=B4=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/MetadataResolver/MelonbooksResolver.php | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/MetadataResolver/MelonbooksResolver.php b/app/MetadataResolver/MelonbooksResolver.php index 5cc3669..b94b719 100644 --- a/app/MetadataResolver/MelonbooksResolver.php +++ b/app/MetadataResolver/MelonbooksResolver.php @@ -30,11 +30,40 @@ class MelonbooksResolver implements Resolver if ($res->getStatusCode() === 200) { $metadata = $this->ogpResolver->parse($res->getBody()); + $dom = new \DOMDocument(); + @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8')); + $xpath = new \DOMXPath($dom); + $descriptionNodelist = $xpath->query('//div[@id="description"]//p'); + $specialDescriptionNodelist = $xpath->query('//div[@id="special_description"]//p'); + // censoredフラグの除去 if (mb_strpos($metadata->image, '&c=1') !== false) { $metadata->image = preg_replace('/&c=1/u', '', $metadata->image); } + // 抽出 + preg_match('~^(.+)((.+))の通販・購入はメロンブックス$~', $metadata->title, $match); + $title = $match[1]; + $maker = $match[2]; + + // 整形 + $description = 'サークル: ' . $maker . "\n"; + + if ($specialDescriptionNodelist->length !== 0) { + $description .= trim(str_replace('
', "\n", $specialDescriptionNodelist->item(0)->nodeValue)) . "\n"; + if ($specialDescriptionNodelist->length === 2) { + $description .= "\n"; + $description .= trim(str_replace('
', "\n", $specialDescriptionNodelist->item(1)->nodeValue)) . "\n"; + } + } + + if ($descriptionNodelist->length !== 0) { + $description .= trim(str_replace('
', "\n", $descriptionNodelist->item(0)->nodeValue)); + } + + $metadata->title = $title; + $metadata->description = trim($description); + return $metadata; } else { throw new \RuntimeException("{$res->getStatusCode()}: $url");