Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MetadataResolverからタグ情報を保存できるようにする #185

Merged
merged 4 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/Http/Controllers/Api/CardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Metadata;
use App\MetadataResolver\MetadataResolver;
use App\Tag;
use App\Utilities\Formatter;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -41,6 +42,13 @@ public function show(Request $request)
'image' => $resolved->image,
'expires_at' => $resolved->expires_at
]);

$tagIds = [];
foreach ($resolved->tags as $tagName) {
$tag = Tag::firstOrCreate(['name' => $tagName]);
$tagIds[] = $tag->id;
}
$metadata->tags()->sync($tagIds);
}

$response = response($metadata);
Expand Down
10 changes: 9 additions & 1 deletion app/Listeners/LinkCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Events\LinkDiscovered;
use App\Metadata;
use App\MetadataResolver\MetadataResolver;
use App\Tag;
use App\Utilities\Formatter;
use GuzzleHttp\Exception\TransferException;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -47,12 +48,19 @@ public function handle(LinkDiscovered $event)
if ($metadata == null || ($metadata->expires_at !== null && $metadata->expires_at < now())) {
try {
$resolved = $this->metadataResolver->resolve($url);
Metadata::updateOrCreate(['url' => $url], [
$metadata = Metadata::updateOrCreate(['url' => $url], [
'title' => $resolved->title,
'description' => $resolved->description,
'image' => $resolved->image,
'expires_at' => $resolved->expires_at
]);

$tagIds = [];
foreach ($resolved->tags as $tagName) {
$tag = Tag::firstOrCreate(['name' => $tagName]);
$tagIds[] = $tag->id;
}
$metadata->tags()->sync($tagIds);
} catch (TransferException $e) {
// 何らかの通信エラーによってメタデータの取得に失敗した時、とりあえずエラーログにURLを残す
Log::error(self::class . ': メタデータの取得に失敗 URL=' . $url);
Expand Down
5 changes: 5 additions & 0 deletions app/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ class Metadata extends Model
protected $visible = ['url', 'title', 'description', 'image', 'expires_at'];

protected $dates = ['created_at', 'updated_at', 'expires_at'];

public function tags()
{
return $this->belongsToMany(Tag::class)->withTimestamps();
}
}
14 changes: 14 additions & 0 deletions app/MetadataResolver/KomifloResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ public function resolve(string $url): Metadata
($json['content']['parents'][0]['data']['title'] ?? '?');
$metadata->image = 'https://t.komiflo.com/564_mobile_large_3x/' . $json['content']['named_imgs']['cover']['filename'];

// 作者情報
if (!empty($json['content']['attributes']['artists']['children'])) {
foreach ($json['content']['attributes']['artists']['children'] as $artist) {
$metadata->tags[] = preg_replace('/\s/', '_', $artist['data']['name']);
}
}

// タグ
if (!empty($json['content']['attributes']['tags']['children'])) {
foreach ($json['content']['attributes']['tags']['children'] as $tag) {
$metadata->tags[] = preg_replace('/\s/', '_', $tag['data']['name']);
}
}

return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
Expand Down
14 changes: 13 additions & 1 deletion app/MetadataResolver/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@

class Metadata
{
/** @var string タイトル */
public $title = '';

/** @var string 概要 */
public $description = '';

/** @var string サムネイルのURL */
public $image = '';
/** @var Carbon|null */

/** @var Carbon|null メタデータの有効期限 */
public $expires_at = null;

/**
* @var string[] タグ
* チェックインタグと同様に保存されるため、スペースや改行文字を含めてはいけません。
*/
public $tags = [];
}
39 changes: 39 additions & 0 deletions app/MetadataResolver/PixivResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,43 @@ public function proxize(string $pixivUrl): string
return str_replace('i.pximg.net', 'i.pixiv.cat', $pixivUrl);
}

/**
* HTMLからタグとして利用可能な情報を抽出する
* @param string $html ページ HTML
* @return string[] タグ
*/
public function extractTags(string $html): array
{
$dom = new \DOMDocument();
@$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new \DOMXPath($dom);

$nodes = $xpath->query("//meta[@name='keywords']");
if ($nodes->length === 0) {
return [];
}

$keywords = $nodes->item(0)->getAttribute('content');
$tags = [];

foreach (mb_split(',', $keywords) as $keyword) {
$keyword = trim($keyword);

if (empty($keyword)) {
continue;
}

// 一部の固定キーワードは無視
if (array_search($keyword, ['R-18', 'イラスト', 'pixiv', 'ピクシブ'], true)) {
continue;
}

$tags[] = preg_replace('/\s/', '_', $keyword);
}

return $tags;
}

public function resolve(string $url): Metadata
{
parse_str(parse_url($url, PHP_URL_QUERY), $params);
Expand Down Expand Up @@ -78,6 +115,8 @@ public function resolve(string $url): Metadata

$metadata->image = $this->proxize($illustUrl);

$metadata->tags = $this->extractTags($res->getBody());

return $metadata;
} else {
throw new \RuntimeException("{$res->getStatusCode()}: $url");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMetadataTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('metadata_tag', function (Blueprint $table) {
$table->increments('id');
$table->text('metadata_url')->index();
$table->integer('tag_id')->index();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('metadata_tag');
}
}