generated from Flycro/laravel-nuxt
feat: Command to fetch Cover Art from OpenLib
parent
74d6f3efaa
commit
2953acd630
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\BookRecommendation;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class OpenLibraryFetchCoverArt extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'book:open-library-fetch-cover-art';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Fetch cover art for books from Open Library API.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$books = BookRecommendation::whereNull('cover_image')->get();
|
||||
$this->info('Fetching cover art for '.$books->count().' books.');
|
||||
foreach ($books as $book) {
|
||||
$this->info('Fetching cover art for '.$book->book_name);
|
||||
$isbn = str_replace('-', '', $book->isbn);
|
||||
$url = 'https://covers.openlibrary.org/b/isbn/'.$isbn.'-L.jpg';
|
||||
$response = Http::get($url);
|
||||
if ($response->status() === 200) {
|
||||
$image = getimagesizefromstring($response->body());
|
||||
if ($image[0] <= 1 || $image[1] <= 1) {
|
||||
$this->error('Failed to fetch cover art.');
|
||||
continue;
|
||||
}
|
||||
$this->info('Cover art fetched.');
|
||||
$imagePath = 'cover_images/'.$isbn.'.jpg';
|
||||
Storage::disk('public')->put($imagePath, $response->body());
|
||||
$book->cover_image = $imagePath;
|
||||
$book->save();
|
||||
$this->info('Cover art fetched and saved.');
|
||||
} else {
|
||||
$this->error('Failed to fetch cover art.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue