From 2953acd6301048436e59086040cb525329036975 Mon Sep 17 00:00:00 2001 From: Flycro Date: Wed, 20 Mar 2024 18:11:41 +0100 Subject: [PATCH] feat: Command to fetch Cover Art from OpenLib --- .../Commands/OpenLibraryFetchCoverArt.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 app/Console/Commands/OpenLibraryFetchCoverArt.php 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.'); + } + } + } +}