diff --git a/app/Console/Commands/OpenLibraryFetchCoverArt.php b/app/Console/Commands/OpenLibraryFetchCoverArt.php new file mode 100644 index 0000000..b6bc762 --- /dev/null +++ b/app/Console/Commands/OpenLibraryFetchCoverArt.php @@ -0,0 +1,55 @@ +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.'); + } + } + } +}