podcastartgenerator/site/database/seeders/SeedFromOldApiSeeder.php
Paul Couture c4398c641e feat/factory_creation (#1)
Prepping for launch.

Reviewed-on: #1
Co-authored-by: Paul Couture <paul@paulcouture.com>
Co-committed-by: Paul Couture <paul@paulcouture.com>
2023-12-14 11:33:03 -06:00

106 lines
4.3 KiB
PHP

<?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();
$current_page = 0;
$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;
$this->command->comment('Artist ID '
. $legacyUser->id
. ' '
. $legacyUser->name
. ' has '
. $legacyUser->artworks->count()
. ' artworks.');
$this->command->error('Missing ' . $countDiff . ' artworks.');
foreach ($user->artworks as $artwork) {
$date = Carbon::parse($artwork->created_at);
$basename = $date->format('Y')
. '/' . $date->format('m')
. '/' . Str::slug($legacyUser->name)
. '-' . Str::slug($artwork->title)
. '_' . $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);
}
}
}
} else {
$this->command->line('Locally stored all of ' . $legacyUser->name . '\'s artworks.');
}
}
}
$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)
->get('https://noagendaartgenerator.com/artistapi',
[
'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();
}
}
}