From 274e11193d278087eaf6c61564b6596db2135b71 Mon Sep 17 00:00:00 2001 From: Charles T Date: Sun, 1 Oct 2023 22:23:38 +0800 Subject: [PATCH] Add (ai gen) --- app/Console/Kernel.php | 30 +- .../Tasks/GenerateShopeeAIArticleTask.php | 20 + .../ShopeeSellerTopProductScraperTask.php | 1 - app/Models/ShopeeSellerCategory.php | 48 ++ app/Models/ShopeeSellerScrape.php | 2 + ...805_create_shopee_seller_scrapes_table.php | 2 +- ..._132038_create_shopee_categories_table.php | 32 ++ database/seeders/ShopeeTechCategorySeeder.php | 533 ++++++++++++++++++ 8 files changed, 664 insertions(+), 4 deletions(-) create mode 100644 app/Models/ShopeeSellerCategory.php create mode 100644 database/migrations/2023_10_01_132038_create_shopee_categories_table.php create mode 100644 database/seeders/ShopeeTechCategorySeeder.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ffad544..9d3b495 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,6 +2,8 @@ namespace App\Console; +use App\Jobs\ShopeeSellerTopProductScraperJob; +use App\Models\Category; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -10,9 +12,31 @@ class Kernel extends ConsoleKernel /** * Define the application's command schedule. */ - protected function schedule(Schedule $schedule): void + protected function schedule(Schedule $schedule) { - $schedule->command('sitemap:generate')->hourly()->runInBackground(); + // Schdule MY TECH Category + $category = Category::where('country_locale_slug','my')->where('name','Technology')->first(); + + if (!is_null($category)) + { + $hour = rand(0, 23); + $minute = rand(0, 59); + + $schedule->call(function () use ($category) { + + $shopee_seller_category = \App\Models\ShopeeSellerCategory::where('category_id', $category->id)->where(function($query) { + $query->whereNull('last_ai_written_at') + ->orWhere('last_ai_written_at', '=', \App\Models\ShopeeSellerCategory::whereNotNull('last_ai_written_at')->orderBy('last_ai_written_at', 'asc')->value('last_ai_written_at')); + })->first(); + + $task = ShopeeSellerTopProductScraperJob::dispatch($shopee_seller_category->seller, $category->country_locale->country_iso, $category) + ->onQueue('default') + ->onConnection('default'); + + + })->dailyAt("{$hour}:{$minute}"); + } + } /** @@ -24,4 +48,6 @@ protected function commands(): void require base_path('routes/console.php'); } + + } diff --git a/app/Jobs/Tasks/GenerateShopeeAIArticleTask.php b/app/Jobs/Tasks/GenerateShopeeAIArticleTask.php index 4fe3d71..844b8fa 100644 --- a/app/Jobs/Tasks/GenerateShopeeAIArticleTask.php +++ b/app/Jobs/Tasks/GenerateShopeeAIArticleTask.php @@ -10,6 +10,7 @@ use App\Models\AiWriteup; use App\Models\Post; use App\Models\PostCategory; +use App\Models\ShopeeSellerCategory; use App\Models\ShopeeSellerScrape; use App\Models\ShopeeSellerScrapedImage; use Exception; @@ -96,6 +97,25 @@ public static function handle(ShopeeSellerScrape $shopee_seller_scrape) $post = Post::create($post_data); if (! is_null($post)) { + + $shopee_seller_scrape->write_counts = $shopee_seller_scrape->write_counts + 1; + $shopee_seller_scrape->last_ai_written_at = now(); + $shopee_seller_scrape->save(); + + $shopee_seller_category = ShopeeSellerCategory::where('seller', $shopee_seller_scrape->seller)->first(); + + if (is_null($shopee_seller_category)) + { + $shopee_seller_category = new ShopeeSellerCategory; + $shopee_seller_category->seller = $shopee_seller_scrape->seller; + $shopee_seller_category->category_id = $shopee_seller_scrape->category_id; + } + + $shopee_seller_category->last_ai_written_at = $shopee_seller_scrape->last_ai_written_at; + $shopee_seller_category->write_counts = $shopee_seller_scrape->write_counts; + + $shopee_seller_category->save(); + PostCategory::create([ 'post_id' => $post->id, 'category_id' => $shopee_seller_scrape->category->id, diff --git a/app/Jobs/Tasks/ShopeeSellerTopProductScraperTask.php b/app/Jobs/Tasks/ShopeeSellerTopProductScraperTask.php index deb9b39..5f2a071 100644 --- a/app/Jobs/Tasks/ShopeeSellerTopProductScraperTask.php +++ b/app/Jobs/Tasks/ShopeeSellerTopProductScraperTask.php @@ -77,7 +77,6 @@ public static function handle(string $seller, string $country_iso, Category $cat $shopee_seller_scrape->epoch = $epoch; $shopee_seller_scrape->filename = $filename; $shopee_seller_scrape->category_id = $category->id; - $shopee_seller_scrape->last_ai_written_at = now(); if ($shopee_seller_scrape->save()) { return (object) compact('seller_shop_task', 'product_task', 'shopee_seller_scrape'); diff --git a/app/Models/ShopeeSellerCategory.php b/app/Models/ShopeeSellerCategory.php new file mode 100644 index 0000000..6821339 --- /dev/null +++ b/app/Models/ShopeeSellerCategory.php @@ -0,0 +1,48 @@ + 'int', + 'last_ai_written_at' => 'datetime', + 'write_counts' => 'int' + ]; + + protected $fillable = [ + 'seller', + 'category_id', + 'last_ai_written_at', + 'write_counts' + ]; + + public function category() + { + return $this->belongsTo(Category::class); + } +} diff --git a/app/Models/ShopeeSellerScrape.php b/app/Models/ShopeeSellerScrape.php index 6c72973..c7ccb54 100644 --- a/app/Models/ShopeeSellerScrape.php +++ b/app/Models/ShopeeSellerScrape.php @@ -32,6 +32,7 @@ class ShopeeSellerScrape extends Model 'category_id' => 'int', 'epoch' => 'int', 'last_ai_written_at' => 'datetime', + 'write_counts' => 'int', ]; protected $fillable = [ @@ -41,6 +42,7 @@ class ShopeeSellerScrape extends Model 'epoch', 'filename', 'last_ai_written_at', + 'write_counts' ]; public function category() diff --git a/database/migrations/2023_09_29_123805_create_shopee_seller_scrapes_table.php b/database/migrations/2023_09_29_123805_create_shopee_seller_scrapes_table.php index 007b5ba..bb66925 100644 --- a/database/migrations/2023_09_29_123805_create_shopee_seller_scrapes_table.php +++ b/database/migrations/2023_09_29_123805_create_shopee_seller_scrapes_table.php @@ -18,7 +18,7 @@ public function up(): void $table->string('country_iso'); $table->bigInteger('epoch'); $table->string('filename'); - $table->timestamp('last_ai_written_at'); + $table->timestamp('last_ai_written_at')->nullable(); $table->integer('write_counts')->default(0); $table->timestamps(); $table->foreign('category_id')->references('id')->on('categories'); diff --git a/database/migrations/2023_10_01_132038_create_shopee_categories_table.php b/database/migrations/2023_10_01_132038_create_shopee_categories_table.php new file mode 100644 index 0000000..f253c16 --- /dev/null +++ b/database/migrations/2023_10_01_132038_create_shopee_categories_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('seller'); + $table->foreignId('category_id'); + $table->timestamp('last_ai_written_at')->nullable(); + $table->integer('write_counts')->default(0); + $table->timestamps(); + $table->foreign('category_id')->references('id')->on('categories'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('shopee_seller_categories'); + } +}; diff --git a/database/seeders/ShopeeTechCategorySeeder.php b/database/seeders/ShopeeTechCategorySeeder.php new file mode 100644 index 0000000..34f8f01 --- /dev/null +++ b/database/seeders/ShopeeTechCategorySeeder.php @@ -0,0 +1,533 @@ +where('name','Technology')->first(); + + foreach ($shopee_sellers as $seller) + { + $shopee_seller_category = new ShopeeSellerCategory; + $shopee_seller_category->seller = $seller; + $shopee_seller_category->category_id = $category->id; + $shopee_seller_category->save(); + } + } +}