Handle bookmark import in Jobs

This commit is contained in:
Annika Backstrom 2024-05-26 22:53:09 +01:00
parent a6e867c5ba
commit 2161e1c226
Signed by: annika
GPG Key ID: 3561F004DE1D9AFE
3 changed files with 82 additions and 35 deletions

View File

@ -31,7 +31,7 @@ Set up the Laravel `.env` file and SQLite database:
composer install composer install
cp .env.example .env cp .env.example .env
php artisan key:generate php artisan key:generate
php artisan migrate php artisan migrate # database schema
``` ```
Build the CSS with Vite: Build the CSS with Vite:
@ -47,6 +47,12 @@ Start the web server:
php artisan serve php artisan serve
``` ```
If you need to process queued jobs (e.g. import bookmarks):
```
php artisan queue:work -v
```
### Laravel Herd ### Laravel Herd
If you have [Laravel Herd](https://herd.laravel.com/) installed, you can clone the repository to one of your Herd If you have [Laravel Herd](https://herd.laravel.com/) installed, you can clone the repository to one of your Herd

View File

@ -2,22 +2,20 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Models\Bookmark; use App\Jobs\ImportBookmark;
use App\Models\BookmarkTag;
use App\Models\Tag;
use DateTimeImmutable;
use DateTimeInterface;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB; use RuntimeException;
class ImportBookmarks extends Command class ImportBookmarks extends Command
{ {
static $perBatch = 100;
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'app:import-bookmarks'; protected $signature = 'app:import-bookmarks {json_file}';
/** /**
* The console command description. * The console command description.
@ -31,34 +29,16 @@ class ImportBookmarks extends Command
*/ */
public function handle() public function handle()
{ {
$json = json_decode(file_get_contents("../pinboard_export.2024.05.24_19.25.json"), true); $json_file_path = $this->argument('json_file');
if (!file_exists($json_file_path)) {
foreach ($json as $bookmark_json) { throw new RuntimeException('Unable to find JSON file');
$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(); $json = file_get_contents($json_file_path);
$bookmarks = json_decode($json, true);
foreach (array_chunk($bookmarks, self::$perBatch) as $bookmarks_json) {
ImportBookmark::dispatch(...$bookmarks_json);
}
} }
} }

View File

@ -0,0 +1,61 @@
<?php
namespace App\Jobs;
use App\Models\Bookmark;
use App\Models\BookmarkTag;
use App\Models\Tag;
use DateTimeImmutable;
use DateTimeInterface;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ImportBookmark implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected array $bookmarks_json;
/**
* Create a new job instance.
*/
public function __construct(array ...$bookmarks_json)
{
$this->bookmarks_json = $bookmarks_json;
}
/**
* Execute the job.
*/
public function handle(): void
{
foreach ($this->bookmarks_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();
}
}
}
}