feat: adding ability to optimize and import older site content.

This commit is contained in:
2023-08-22 16:04:37 -05:00
parent 5c218918ee
commit 9a7aa89362
35 changed files with 1267 additions and 398 deletions

View File

@@ -29,7 +29,7 @@ class ArtistFactory extends Factory
'header' => fake()->imageUrl(270, 185),
'location' => fake()->city() . ', ' . fake()->state(),
'website' => rand(0, 1) ? fake()->url : null,
'bio' => fake()->paragraphs(rand(1,3, true)),
'bio' => fake()->paragraphs(rand(1, 3), true),
];
}
}

View File

@@ -27,14 +27,18 @@ class ArtworkFactory extends Factory
*/
public function definition()
{
$created = fake()->dateTimeThisDecade();
return [
'title' => fake()->name(),
'description' => rand(0, 1) ? fake()->paragraphs(rand((1, 3, true))) : null,
'description' => rand(0, 1) ? fake()->paragraphs(rand(1, 3), true) : null,
'artist_id' => Artist::factory(),
'podcast_id' => Podcast::factory(),
'episode_id' => Episode::factory(),
'overlay_id' => rand(0, 1) ? Overlay::factory() : null,
'filename' => fake()->imageUrl(3000, 3000),
'created_at' => $created,
'updated_at' => $created,
'legacy_id' => null,
];
}
}

View File

@@ -2,11 +2,14 @@
namespace Database\Factories;
use App\Models\Artist;
use App\Models\Artwork;
use App\Models\Episode;
use App\Models\Podcast;
use App\Models\Artwork;
use App\Models\Overlay;
use Illuminate\Database\Eloquent\Factories\Factory;
use Carbon\Carbon;
use Illuminate\Support\Str;
class EpisodeFactory extends Factory
{
@@ -24,11 +27,20 @@ class EpisodeFactory extends Factory
*/
public function definition()
{
$title = fake()->name();
$slug = Str::slug($title);
$created = fake()->dateTimeThisDecade();
return [
'podcast_id' => Podcast::factory(),
'artwork_id' => Artwork::factory(),
'published' => fake()->boolean(),
'title' => fake()->name()
'episode_date' => fake()->dateTimeThisDecade(),
'slug' => $slug,
'title' => $title,
'mp3' => fake()->url(),
'created_at' => $created,
'updated_at' => $created,
'legacy_id' => null,
];
}
}

View File

@@ -4,12 +4,23 @@ namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Podcast;
use App\Models\Artist;
use App\Models\Artwork;
use App\Models\Episode;
use App\Models\Podcast;
use App\Models\Overlay;
class OverlayFactory extends Factory
{
/**
* The name of the factory's corresponding model
*
* @var string
*/
protected $model = Overlay::class;
/**
* Define the model's default state.
*

View File

@@ -4,10 +4,21 @@ namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Artist;
use App\Models\Artwork;
use App\Models\Episode;
use App\Models\Podcast;
use App\Models\Overlay;
class PodcastFactory extends Factory
{
/**
* The name of the factory's corresponding model
*
* @var string
*/
protected $model = Podcast::class;
/**
* Define the model's default state.
*

View File

@@ -5,9 +5,22 @@ namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use App\Models\Artist;
use App\Models\Artwork;
use App\Models\Episode;
use App\Models\Podcast;
use App\Models\Overlay;
use App\Models\User;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model
*
* @var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*

View File

@@ -3,6 +3,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Models\User;
class CreateArtistsTable extends Migration
{

View File

@@ -0,0 +1,28 @@
<?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('episodes', function (Blueprint $table) {
$table->decimal('episode_number', 8, 1)->after('episode_date')->default(0);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('episodes', function (Blueprint $table) {
$table->dropColumn('episode_number');
});
}
};

View File

@@ -0,0 +1,28 @@
<?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('artworks', function (Blueprint $table) {
$table->bigInteger('legacy_id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('artworks', function (Blueprint $table) {
$table->dropColumn('legacy_id');
});
}
};

View File

@@ -0,0 +1,28 @@
<?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('episodes', function (Blueprint $table) {
$table->bigInteger('legacy_id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('episodes', function (Blueprint $table) {
$table->dropColumn('legacy_id');
});
}
};

View File

@@ -0,0 +1,60 @@
<?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;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Episode;
use App\Models\Podcast;
use App\Models\Artwork;
use Illuminate\Support\Str;
use Illuminate\Support\Facade\Log;
class FixLegacyEpisodeSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$episodes = Episode::all();
foreach ($episodes as $episode) {
if (is_null($episode->episode_number) || $episode->episode_number == 0) {
$ep_num_arr = explode('_', $episode->slug);
$episode->episode_number = $ep_num_arr[0];
}
if ($episode->isDirty()) {
$episode->save();
}
}
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class MapLegacyIdsSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
}
}

View File

@@ -132,7 +132,14 @@ class PodcastSeeder extends Seeder
$feed = 'https://www.unrelenting.show/feed/podcast/';
$added = '2021-10-29 20:00:00';
$this->createPodcast($name, $description, $website, $slug, $feed, $added);
$name = 'The Boostagram Ball';
$description = 'The First Podcast with Value4Value Music hosted by Adam Curry';
$website = 'https://boostagramball.com';
$slug = 'boostagram-ball';
$feed = 'https://mp3s.nashownotes.com/bballrss.xml';
$added = '2023-07-29 20:00:00';
$this->createPodcast($name, $description, $website, $slug, $feed, $added);
}
private function createPodcast($name, $description, $website, $slug, $feed, $added) {

View File

@@ -0,0 +1,105 @@
<?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 = 283;
$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();
}
}
}