一部サイトに対するサムネイルURL解決の特殊処理を仕込んだ

This commit is contained in:
shibafu 2018-01-05 23:34:49 +09:00
parent f2ed4f85ee
commit 7a386d4d89
3 changed files with 71 additions and 9 deletions

View File

@ -114,10 +114,31 @@
url: $this.find('a').attr('href') url: $this.find('a').attr('href')
} }
}).then(function (data) { }).then(function (data) {
$this.find('.card-title').text(data.title); var $title = $this.find('.card-title');
$this.find('.card-text').text(data.description); var $desc = $this.find('.card-text');
$this.find('img').attr('src', data.image); var $image = $this.find('img');
$this.removeClass('d-none');
if (data.title === '') {
$title.hide();
} else {
$title.text(data.title);
}
if (data.description === '') {
$desc.hide();
} else {
$desc.text(data.description);
}
if (data.image === '') {
$image.hide();
} else {
$image.attr('src', data.image);
}
if (data.title !== '' || data.description !== '' || data.image !== '') {
$this.removeClass('d-none');
}
}); });
}); });
</script> </script>

View File

@ -118,10 +118,31 @@
url: $this.find('a').attr('href') url: $this.find('a').attr('href')
} }
}).then(function (data) { }).then(function (data) {
$this.find('.card-title').text(data.title); var $title = $this.find('.card-title');
$this.find('.card-text').text(data.description); var $desc = $this.find('.card-text');
$this.find('img').attr('src', data.image); var $image = $this.find('img');
$this.removeClass('d-none');
if (data.title === '') {
$title.hide();
} else {
$title.text(data.title);
}
if (data.description === '') {
$desc.hide();
} else {
$desc.text(data.description);
}
if (data.image === '') {
$image.hide();
} else {
$image.attr('src', data.image);
}
if (data.title !== '' || data.description !== '' || data.image !== '') {
$this.removeClass('d-none');
}
}); });
}); });
</script> </script>

View File

@ -21,9 +21,10 @@ Route::get('/checkin/card', function (Request $request) {
$request->validate([ $request->validate([
'url:required|url' 'url:required|url'
]); ]);
$url = $request->input('url');
$client = new GuzzleHttp\Client(); $client = new GuzzleHttp\Client();
$res = $client->get($request->input('url')); $res = $client->get($url);
if ($res->getStatusCode() === 200) { if ($res->getStatusCode() === 200) {
$dom = new DOMDocument(); $dom = new DOMDocument();
@$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8')); @$dom->loadHTML(mb_convert_encoding($res->getBody(), 'HTML-ENTITIES', 'UTF-8'));
@ -59,6 +60,25 @@ Route::get('/checkin/card', function (Request $request) {
} }
} }
// 一部サイトについては別のサムネイルの取得を試みる
if (mb_strpos($url, 'nico.ms/im') !== false ||
mb_strpos($url, 'seiga.nicovideo.jp/seiga/im') !== false ||
mb_strpos($url, 'sp.seiga.nicovideo.jp/seiga/#!/im') !== false) {
// ニコニコ静画用の処理
preg_match('~http://(?:(?:sp\\.)?seiga\\.nicovideo\\.jp/seiga(?:/#!)?|nico\\.ms)/im(\\d+)~', $url, $matches);
$result['image'] = "http://lohas.nicoseiga.jp/thumb/${matches[1]}l?";
} elseif (mb_strpos($url, 'nijie.info/view.php')) {
// ニジエ用の処理
$dataNode = $xpath->query('//script[substring(@type, string-length(@type) - 3, 4) = "json"]');
foreach ($dataNode as $node) {
$imageData = json_decode($node->nodeValue, true);
if (isset($imageData['thumbnailUrl'])) {
$result['image'] = preg_replace('~nijie\\.info/.*/nijie_picture/~', 'nijie.info/nijie_picture/', $imageData['thumbnailUrl']);
break;
}
}
}
$response = response()->json($result); $response = response()->json($result);
if (!config('app.debug')) { if (!config('app.debug')) {
$response = $response->setCache(['public' => true, 'max_age' => 86400]); $response = $response->setCache(['public' => true, 'max_age' => 86400]);