makerをOGPタイトルに含まれる著者名などから取得するように変更
This commit is contained in:
		@@ -37,16 +37,36 @@ class DLsiteResolver implements Resolver
 | 
			
		||||
            @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
 | 
			
		||||
            $xpath = new \DOMXPath($dom);
 | 
			
		||||
 | 
			
		||||
            // 抽出
 | 
			
		||||
            $title = $xpath->query('//title')->item(0)->textContent;
 | 
			
		||||
            preg_match('~\[([^\[\]]*)\] \| DLsite.+$~', $title, $match);
 | 
			
		||||
            $maker = $match[1];
 | 
			
		||||
            // OGPタイトルから[]に囲まれているmakerを取得する
 | 
			
		||||
            // 複数の作者がいる場合スペース区切りになるためexplodeしている
 | 
			
		||||
            // スペースを含むmakerの場合名前の一部しか取れないが動作には問題ない
 | 
			
		||||
            preg_match('~ \[([^\[\]]*)\] \| DLsite(がるまに)?$~', $metadata->title, $match);
 | 
			
		||||
            $makers = explode(' ', $match[1]);
 | 
			
		||||
 | 
			
		||||
            // makerに一致するthのテキストを探す
 | 
			
		||||
            $makerHead = trim($xpath->query('//a[contains(text(), "'.$maker.'")]/ancestor::tr/th')->item(0)->textContent);
 | 
			
		||||
            //フォローボタン(.btn_follow)はテキストを含んでしまうことがあるので要素を削除しておく
 | 
			
		||||
            $followButtonNode = $xpath->query('//*[@class="btn_follow"]')->item(0);
 | 
			
		||||
            $followButtonNode->parentNode->removeChild($followButtonNode);
 | 
			
		||||
 | 
			
		||||
            // maker, makerHeadを探す
 | 
			
		||||
 | 
			
		||||
            // makers
 | 
			
		||||
            // #work_makerから「makerを含むテキスト」を持つ要素を持つtdを探す
 | 
			
		||||
            // 作者名単体の場合もあるし、"作者A / 作者B"のようになることもある
 | 
			
		||||
            $makersNode = $xpath->query('//*[@id="work_maker"]//*[contains(text(), "' . $makers[0] . '")]/ancestor::td')->item(0);
 | 
			
		||||
            $makers = trim($makersNode->textContent);
 | 
			
		||||
 | 
			
		||||
            // makersHaed
 | 
			
		||||
            // $makerNode(td)に対するthを探す
 | 
			
		||||
            // "著者", "サークル名", "ブランド名"など
 | 
			
		||||
            $makersHeadNode = $xpath->query('preceding-sibling::th', $makersNode)->item(0);
 | 
			
		||||
            $makersHead = trim($makersHeadNode->textContent);
 | 
			
		||||
 | 
			
		||||
            // 余分な文を消す
 | 
			
		||||
 | 
			
		||||
            // OGPタイトルから作者名とサイト名を消す
 | 
			
		||||
            $metadata->title = trim(preg_replace('~ \[([^\[\]]*)\] \| DLsite(がるまに)?$~', '', $metadata->title));
 | 
			
		||||
 | 
			
		||||
            // OGP説明文から定型文を消す
 | 
			
		||||
            if (strpos($url, 'dlsite.com/eng/') || strpos($url, 'dlsite.com/ecchi-eng/')) {
 | 
			
		||||
                $metadata->description = trim(preg_replace('~DLsite.+ is a download shop for .+With a huge selection of products, we\'re sure you\'ll find whatever tickles your fancy\. DLsite is one of the greatest indie contents download shops in Japan\.$~', '', $metadata->description));
 | 
			
		||||
            } else {
 | 
			
		||||
@@ -54,7 +74,7 @@ class DLsiteResolver implements Resolver
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // 整形
 | 
			
		||||
            $metadata->description = $makerHead.': ' . $maker . PHP_EOL . $metadata->description;
 | 
			
		||||
            $metadata->description = $makersHead . ': ' . $makers . PHP_EOL . $metadata->description;
 | 
			
		||||
            $metadata->image = str_replace('img_sam.jpg', 'img_main.jpg', $metadata->image);
 | 
			
		||||
 | 
			
		||||
            return $metadata;
 | 
			
		||||
 
 | 
			
		||||
@@ -56,7 +56,7 @@ class DLsiteResolverTest extends TestCase
 | 
			
		||||
 | 
			
		||||
        $metadata = $this->resolver->resolve('https://www.dlsite.com/comic/work/=/product_id/BJ138581.html');
 | 
			
		||||
        $this->assertEquals('快楽ヒストリエ', $metadata->title);
 | 
			
		||||
        $this->assertEquals('出版社名: ワニマガジン社' . PHP_EOL . '天地創造と原初の人類を描いた「創世編」をはじめ、英雄たちの偉業を大真面目に考証した正真正銘の学術コミック全15編。', $metadata->description);
 | 
			
		||||
        $this->assertEquals('著者: 火鳥' . PHP_EOL . '天地創造と原初の人類を描いた「創世編」をはじめ、英雄たちの偉業を大真面目に考証した正真正銘の学術コミック全15編。', $metadata->description);
 | 
			
		||||
        $this->assertEquals('https://img.dlsite.jp/modpub/images2/work/books/BJ139000/BJ138581_img_main.jpg', $metadata->image);
 | 
			
		||||
        if ($this->shouldUseMock()) {
 | 
			
		||||
            $this->assertSame('https://www.dlsite.com/comic/work/=/product_id/BJ138581.html', (string) $this->handler->getLastRequest()->getUri());
 | 
			
		||||
@@ -101,7 +101,7 @@ class DLsiteResolverTest extends TestCase
 | 
			
		||||
 | 
			
		||||
        $metadata = $this->resolver->resolve('https://www.dlsite.com/books/work/=/product_id/BJ191317.html');
 | 
			
		||||
        $this->assertEquals('永遠娘 vol.6', $metadata->title);
 | 
			
		||||
        $this->assertEquals('出版社名: 茜新社' . PHP_EOL . '君の命はどんな味なのだろうな?', $metadata->description);
 | 
			
		||||
        $this->assertEquals('著者: あまがえる / 玉之けだま / びんせん / 甘露アメ / 源五郎 / すみやお / 宇宙烏賊 / 毒茸人 / あやね / ガロウド / ハードボイルドよし子 / 夜歌 / 黒青郎君' . PHP_EOL . '君の命はどんな味なのだろうな?', $metadata->description);
 | 
			
		||||
        $this->assertEquals('https://img.dlsite.jp/modpub/images2/work/books/BJ192000/BJ191317_img_main.jpg', $metadata->image);
 | 
			
		||||
        if ($this->shouldUseMock()) {
 | 
			
		||||
            $this->assertSame('https://www.dlsite.com/books/work/=/product_id/BJ191317.html', (string) $this->handler->getLastRequest()->getUri());
 | 
			
		||||
@@ -131,7 +131,7 @@ class DLsiteResolverTest extends TestCase
 | 
			
		||||
 | 
			
		||||
        $metadata = $this->resolver->resolve('https://www.dlsite.com/girls-pro/work/=/product_id/BJ170641.html');
 | 
			
		||||
        $this->assertEquals('×××レクチャー', $metadata->title);
 | 
			
		||||
        $this->assertEquals('出版社名: 竹書房' . PHP_EOL . '昔、告白してくれた地味な同級生・瀬尾は超人気セクシー男優になっていて!?', $metadata->description);
 | 
			
		||||
        $this->assertEquals('著者: 江口尋' . PHP_EOL . '昔、告白してくれた地味な同級生・瀬尾は超人気セクシー男優になっていて!?', $metadata->description);
 | 
			
		||||
        $this->assertEquals('https://img.dlsite.jp/modpub/images2/work/books/BJ171000/BJ170641_img_main.jpg', $metadata->image);
 | 
			
		||||
        if ($this->shouldUseMock()) {
 | 
			
		||||
            $this->assertSame('https://www.dlsite.com/girls-pro/work/=/product_id/BJ170641.html', (string) $this->handler->getLastRequest()->getUri());
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user