podcastartgenerator/site/database/seeders/EpisodeSeeder.php

61 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Episode;
use App\Models\Podcast;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Carbon\Carbon;
class EpisodeSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$current_page = 1;
$response = $this->getResponseFromApi($current_page);
$last_page = $response->object()->last_page;
$this->command->info('Last Page: ' . $last_page);
$podcast = Podcast::find(1);
while ($current_page <= $last_page) {
$this->command->info('Getting Page ' . $current_page);
foreach ($response->object()->data as $episode) {
$podcastEpisode = Episode::where('title', $episode->title)->first();
if (!$podcastEpisode) {
$podcastEpisode = Episode::factory()->state([
'podcast_id' => 1,
'episode_date' => Carbon::parse($episode->show_date),
'published' => (bool)$episode->published,
'artwork_id' => null,
'slug' => $episode->episode_number . '_' . Str::slug($episode->title),
'title' => $episode->title,
'mp3' => $episode->link,
'created_at' => Carbon::parse($episode->created_at),
'updated_at' => Carbon::parse($episode->updated_at),
'legacy_id' => $episode->id ?? null
])->create();
} else {
$podcastEpisode->legacy_id = $episode->id ?? null;
if ($podcastEpisode->isDirty()) {
$podcastEpisode->save();
}
}
$this->command->info('Created ' . $episode->show_date . ' - (' . $episode->episode_number . ') ' . $episode->title);
}
$current_page++;
$response = $this->getResponseFromApi($current_page);
}
}
private function getResponseFromApi($current_page) {
$response = Http::timeout(180)
->get('https://noagendaartgenerator.com/episodesjson?p=7476&page=' . $current_page);
return $response;
}
}