urlsnail/app/Console/Commands/ImportBookmarks.php

65 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use App\Models\Bookmark;
use App\Models\BookmarkTag;
use App\Models\Tag;
use DateTimeImmutable;
use DateTimeInterface;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ImportBookmarks extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:import-bookmarks';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$json = json_decode(file_get_contents("../pinboard_export.2024.05.24_19.25.json"), true);
foreach ($json as $bookmark_json) {
$created_at = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601_EXPANDED, $bookmark_json['time']);
$bookmark = new Bookmark;
$bookmark->href = $bookmark_json['href'];
$bookmark->title = $bookmark_json['description'];
$bookmark->description = $bookmark_json['extended'];
$bookmark->created_at = $created_at;
$bookmark->updated_at = $created_at;
$bookmark->save();
$tokens = explode(' ', $bookmark_json['tags']);
foreach ($tokens as $tag_raw) {
$tag = Tag::firstOrCreate(
[
'name' => $tag_raw,
]
);
$bookmark_tag = new BookmarkTag;
$bookmark_tag->bookmark_id = $bookmark->id;
$bookmark_tag->tag_id = $tag->id;
$bookmark_tag->save();
}
}
DB::rollBack();
}
}