2023-08-22 17:04:37 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Database\Seeders;
|
|
|
|
|
|
|
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
|
|
use Illuminate\Database\Seeder;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\Artwork;
|
|
|
|
use App\Models\Episode;
|
|
|
|
use App\Models\Podcast;
|
|
|
|
use App\Models\Overlay;
|
|
|
|
use App\Jobs\StashAndOptimizeLegacyArtworkJob;
|
|
|
|
use App\Jobs\ImportLegacyUserJob;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
class SeedFromOldApiSeeder extends Seeder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Run the database seeds.
|
|
|
|
*/
|
|
|
|
public function run(): void
|
|
|
|
{
|
|
|
|
//$this->populateLegacyArtworks();
|
|
|
|
//die();
|
2023-12-10 16:10:32 -05:00
|
|
|
$current_page = 0;
|
2023-08-22 17:04:37 -04:00
|
|
|
$totalArtworks = 0;
|
|
|
|
$missingArtworks = 0;
|
|
|
|
$missingModel = 0;
|
|
|
|
$response = $this->getResponseFromApi($current_page);
|
|
|
|
$last_page = $response->object()->users->last_page;
|
|
|
|
while ($current_page <= $last_page) {
|
|
|
|
$this->command->info('Getting Page ' . $current_page);
|
|
|
|
foreach ($response->object()->users->data as $user) {
|
|
|
|
$this->command->line('Getting Art for ' . $user->profile->name . ', found ' . count($user->artworks) . ' artworks.');
|
|
|
|
$totalArtworks += count($user->artworks);
|
|
|
|
$legacyUser = Artist::where('name', $user->profile->name)->first();
|
|
|
|
if (!$legacyUser) {
|
|
|
|
ImportLegacyUserJob::dispatch($user);
|
|
|
|
} else {
|
|
|
|
if ($legacyUser->artworks->count() < count($user->artworks)) {
|
|
|
|
$countDiff = count($user->artworks) - $legacyUser->artworks->count();
|
|
|
|
$missingArtworks += $countDiff;
|
2023-12-10 16:10:32 -05:00
|
|
|
$this->command->comment('Artist ID '
|
|
|
|
. $legacyUser->id
|
|
|
|
. ' '
|
|
|
|
. $legacyUser->name
|
|
|
|
. ' has '
|
|
|
|
. $legacyUser->artworks->count()
|
2023-08-22 17:04:37 -04:00
|
|
|
. ' artworks.');
|
|
|
|
$this->command->error('Missing ' . $countDiff . ' artworks.');
|
|
|
|
foreach ($user->artworks as $artwork) {
|
|
|
|
$date = Carbon::parse($artwork->created_at);
|
2023-12-10 16:10:32 -05:00
|
|
|
$basename = $date->format('Y')
|
|
|
|
. '/' . $date->format('m')
|
|
|
|
. '/' . Str::slug($legacyUser->name)
|
|
|
|
. '-' . Str::slug($artwork->title)
|
2023-08-22 17:04:37 -04:00
|
|
|
. '_' . $artwork->id . '.jpg';
|
|
|
|
if (Storage::disk('static')->exists('artworks/' . $basename)) {
|
|
|
|
$artworkModel = Artwork::where('filename', $basename)->first();
|
|
|
|
if (!$artworkModel) {
|
|
|
|
$missingModel++;
|
|
|
|
$this->command->error($basename . ' exists.');
|
|
|
|
StashAndOptimizeLegacyArtworkJob::dispatch($legacyUser, $artwork);
|
|
|
|
}
|
|
|
|
}
|
2023-12-10 16:10:32 -05:00
|
|
|
|
2023-08-22 17:04:37 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->command->line('Locally stored all of ' . $legacyUser->name . '\'s artworks.');
|
|
|
|
}
|
2023-12-10 16:10:32 -05:00
|
|
|
}
|
2023-08-22 17:04:37 -04:00
|
|
|
}
|
|
|
|
$current_page++;
|
|
|
|
$response = $this->getResponseFromApi($current_page);
|
|
|
|
}
|
|
|
|
$this->command->info('Total Artworks: ' . $totalArtworks);
|
|
|
|
$this->command->info('Total Missing: ' . $missingArtworks);
|
|
|
|
$this->command->info('Total Missing Model: ' . $missingModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getResponseFromApi($current_page) {
|
|
|
|
$response = Http::timeout(180)
|
2023-12-10 16:10:32 -05:00
|
|
|
->get('https://noagendaartgenerator.com/artistapi',
|
2023-08-22 17:04:37 -04:00
|
|
|
[
|
|
|
|
'p' => '7476',
|
|
|
|
'page' => $current_page,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function populateLegacyArtworks() {
|
|
|
|
$artworks = Artwork::whereNull('legacy_id')->get();
|
|
|
|
foreach ($artworks as $artwork) {
|
|
|
|
$file = pathinfo($artwork->filename);
|
|
|
|
$filename = explode('_', $file['filename']);
|
|
|
|
$legacy_id = end($filename);
|
|
|
|
$artwork->legacy_id = $legacy_id;
|
|
|
|
$artwork->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|