-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d474bb7
commit ed4e4fc
Showing
10 changed files
with
268 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App\Enums\Imports; | ||
|
||
enum ImportSources: string | ||
{ | ||
case OPEN_LIBRARY = 'openlibrary'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Enums\Imports\ImportSources; | ||
use Exception; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use App\Traits\Imports\OpenLibraryTrait; | ||
|
||
class ImportFromApiJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
use OpenLibraryTrait; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct( | ||
private ImportSources $source = ImportSources::OPEN_LIBRARY, | ||
private array $query = ['first_publish_year:[0 TO 2024]'], | ||
) | ||
{} | ||
|
||
/** | ||
* Execute the job. | ||
* @throws Exception | ||
*/ | ||
public function handle(): void | ||
{ | ||
// validate if query array is key only and not key-value pair | ||
foreach ($this->query as $key => $value) { | ||
if (is_string($key)) { | ||
throw new Exception('Query array must not be key-value pair'); | ||
} | ||
} | ||
|
||
// replace with switch statement once more sources are added | ||
if ($this->source == ImportSources::OPEN_LIBRARY) { | ||
$this->importOpenLibrary($this->query); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\Traits\Imports; | ||
|
||
use App\Models\Item; | ||
use App\Models\User; | ||
use App\Traits\CommonLibraryTrait; | ||
|
||
trait OpenLibraryTrait | ||
{ | ||
use CommonLibraryTrait; | ||
public function importOpenLibrary($tries = 3) { | ||
if ($tries < 1) $tries = 3; | ||
|
||
$maxOffset = (int) ceil(100 * $tries / 100) * 100; | ||
$offset = 100; | ||
$loop = (int) ceil($offset / 100); | ||
|
||
for ($i = 0; $i < $loop; $i++) { | ||
$this->import($offset); | ||
if ($offset >= $maxOffset) { | ||
break; | ||
} | ||
$offset += 100; | ||
}; | ||
} | ||
|
||
private function import($offset) { | ||
$books = $this->getData($offset); | ||
|
||
foreach ($books as $book) { | ||
$this->importBook($book); | ||
} | ||
} | ||
|
||
private function importBook($book) { | ||
Item::create([ | ||
'identifier' => $this->generateValidItemIdentifier(), | ||
'type' => 'book', | ||
'name' => $book['title'], | ||
'description' => "Dit boek is geschreven door {$book['author_name'][0]} en is uitgegeven in {$book['first_publish_year']}.", | ||
'category' => 1, | ||
'ISBN' => $book['isbn'][0], | ||
'rating' => 1, | ||
'borrowing_time' => 30, | ||
'modified_kind' => 'I', | ||
'modified_user' => User::where('email', '[email protected]')->first()->id, | ||
]); | ||
} | ||
|
||
private function getData($offset) | ||
{ | ||
$baseUrl = "https://openlibrary.org/search.json?q=language%3Adut&sort=new&lang=nl&offset=" . $offset; | ||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $baseUrl); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
|
||
$response = json_decode($response, true); | ||
return $response['docs']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
src/database/migrations/2023_12_20_101128_create_jobs_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('jobs', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('queue')->index(); | ||
$table->longText('payload'); | ||
$table->unsignedTinyInteger('attempts'); | ||
$table->unsignedInteger('reserved_at')->nullable(); | ||
$table->unsignedInteger('available_at'); | ||
$table->unsignedInteger('created_at'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('jobs'); | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
src/database/migrations/2023_12_21_105625_add_author_to_items.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('items', function (Blueprint $table) { | ||
$table->string('author')->nullable()->after('name'); | ||
$table->string('publisher')->nullable()->after('author'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('items', function (Blueprint $table) { | ||
// | ||
}); | ||
} | ||
}; |