Add (serp ai gen)

Add (scheduler)
This commit is contained in:
2023-09-26 01:47:14 +08:00
parent e63231b65e
commit c27ec696d1
17 changed files with 1474 additions and 51 deletions

View File

@@ -2,6 +2,8 @@
namespace App\Console;
use App\Jobs\AISerpGenArticleJob;
use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -12,7 +14,21 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
// AI Gen Scheduler
$launched_date = Carbon::parse(config('platform.global.launched_epoch'));
$days_since_launch = now()->diffInDays($launched_date) + 1;
$posts_to_generate = get_exponential_posts_gen_by_day($days_since_launch);
$mins_betwween_posts = floor((24 * 60) / $posts_to_generate);
$schedule->call(function () {
AISerpGenArticleJob::dispatch()->onQueue('default')->onConnection('default');
})->everyMinutes($mins_betwween_posts)->when(function () use ($mins_betwween_posts) {
return now()->minute % $mins_betwween_posts === 0;
});
}
/**

View File

@@ -11,3 +11,23 @@ function get_slack_channel_by_env($slack_channel = 'slack')
}
}
}
if (! function_exists('get_exponential_posts_gen_by_day')) {
// Function to calculate the value for a given day
function get_exponential_posts_gen_by_day($day, $period_days = 45)
{
$min_value = 4;
$max_value = 150;
$growthRate = log($max_value / $min_value) / $period_days; // Calculate the growth rate
$value = round($min_value * exp($growthRate * $day));
if ($value > $max_value) {
return $max_value;
}
return $value;
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Jobs;
use App\Jobs\Tasks\ParseNewsSerpDomainsTask;
use App\Models\Category;
use App\Models\SerpUrl;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class AISerpGenArticleJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
$category = Category::orderBy('serp_at', 'asc')
->orWhereNull('serp_at')
->first();
$news_serp_result = GetNewsSerpTask::handle($category, 'US');
if (is_null($news_serp_result)) {
Log::error(json_encode($category->toArray()));
throw Exception('Failed to execute GetNewsSerpTask');
}
// We only take 1 record at a time
if (ParseNewsSerpDomainsTask::handle($news_serp_result)) {
$serp_url = SerpUrl::latest()->first();
if (is_null($serp_url)) {
Log::error(json_encode($serp_url->toArray()));
throw Exception('Failed to execute ParseNewsSerpDomainsTask');
}
return GenerateArticleJob::dispatch($serp_url)->onQueue('default')->onConnection('default');
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Jobs;
use App\Models\Post;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class PublishIndexPostJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $post;
/**
* Create a new job instance.
*/
public function __construct(Post $post)
{
$this->post = $post;
}
/**
* Execute the job.
*/
public function handle(): void
{
if (! is_null($this->post)) {
PublishIndexPostTask::handle($this->post);
}
}
}

View File

@@ -4,6 +4,7 @@
use App\Helpers\FirstParty\OSSUploader\OSSUploader;
use App\Helpers\ThirdParty\DFS\SettingSerpLiveAdvanced;
use App\Jobs\PublishIndexPostJob;
use App\Models\NewsSerpResult;
use DFSClientV3\DFSClient;
use Exception;
@@ -239,6 +240,8 @@ public static function handle($post)
$post->status = 'publish';
$post->save();
PublishIndexPostJob::dispatch($post)->onQueue('default')->onConnection('default');
return $post;
}

View File

@@ -3,6 +3,7 @@
namespace App\Jobs\Tasks;
use App\Helpers\FirstParty\OpenAI\OpenAI;
use App\Jobs\GenerateArticleFeaturedImageJob;
use App\Models\Author;
use App\Models\Post;
use App\Models\PostCategory;
@@ -71,6 +72,8 @@ public static function handle(SerpUrl $serp_url)
$post_category->category_id = $serp_url->category->id;
if ($post_category->save()) {
GenerateArticleFeaturedImageJob::dispatch($post)->onQueue('default')->onConnection('default');
return self::saveAndReturnSerpProcessStatus($serp_url, 1);
} else {
return self::saveAndReturnSerpProcessStatus($serp_url, -5);

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Jobs\Tasks;
use App\Models\Post;
use Exception;
use IndexNow;
use LaravelGoogleIndexing;
class PublishIndexPostTask
{
public static function handle(Post $post)
{
$post->published_at = now();
if ($post->save()) {
if (app()->environment() == 'production') {
$post_url = route('front.post', ['slug' => $post->slug]);
try {
IndexNow::submit($post_url);
} catch (Exception) {
}
try {
LaravelGoogleIndexing::create()->update($post_url);
} catch (Exception) {
}
}
}
}
}

View File

@@ -36,6 +36,7 @@ class Category extends Model
'_lft' => 'int',
'_rgt' => 'int',
'parent_id' => 'int',
'serp_at' => 'datetime',
];
protected $fillable = [
@@ -46,6 +47,7 @@ class Category extends Model
'_lft',
'_rgt',
'parent_id',
'serp_at',
];
protected static function boot()

View File

@@ -11,6 +11,7 @@
"php": "^8.1",
"artesaos/seotools": "^1.2",
"dipeshsukhia/laravel-html-minify": "^3.3",
"famdirksen/laravel-google-indexing": "^0.5.0",
"fivefilters/readability.php": "^1.0",
"graham-campbell/markdown": "^15.0",
"guzzlehttp/guzzle": "^7.2",
@@ -18,6 +19,7 @@
"intervention/image": "^2.7",
"jovix/dataforseo-clientv3": "^1.1",
"kalnoy/nestedset": "^6.0",
"laravel-freelancer-nl/laravel-index-now": "^1.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8",

1294
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -194,6 +194,7 @@
'SEO' => Artesaos\SEOTools\Facades\SEOTools::class,
'Image' => Intervention\Image\Facades\Image::class,
'Markdown' => GrahamCampbell\Markdown\Facades\Markdown::class,
'IndexNow' => LaravelFreelancerNL\LaravelIndexNow\Facades\IndexNow::class,
])->toArray(),

13
config/index-now.php Normal file
View File

@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
return [
'delay' => env('INDEXNOW_SUBMIT_DELAY', 600),
'host' => env('APP_URL', 'localhost'),
'key' => env('INDEXNOW_KEY', ''),
'key-location' => env('INDEXNOW_KEY_LOCATION', ''),
'log-failed-submits' => env('INDEXNOW_LOG_FAILED_SUBMITS', true),
'production-env' => env('INDEXNOW_PRODUCTION_ENV', 'production'),
'search-engine' => env('INDEXNOW_SEARCH_ENGINE', 'api.indexnow.org'),
];

View File

@@ -0,0 +1,11 @@
<?php
return [
'google' => [
'auth_config' => storage_path('echoscoop-90d335332507.json'),
'scopes' => [
\Google_Service_Indexing::INDEXING,
],
],
];

View File

@@ -0,0 +1,7 @@
<?php
return [
'launched_epoch' => '1695513600', // 24-09-2023 00:00:00 GMT +0
];

View File

@@ -0,0 +1 @@
07654e9d-367b-4d24-9bc0-2bc8672e271f

View File

@@ -1,6 +1,7 @@
<?php
use App\Helpers\FirstParty\OpenAI\OpenAI;
use App\Jobs\AISerpGenArticleJob;
use App\Jobs\GenerateArticleFeaturedImageJob;
use App\Jobs\GenerateArticleJob;
use App\Jobs\Tasks\GetNewsSerpTask;
@@ -24,6 +25,18 @@
|
*/
Route::get('/serp-ai-gen', function (Request $request) {
AISerpGenArticleJob::dispatch()->onQueue('default')->onConnection('default');
});
Route::get('/exponential', function (Request $request) {
$post_counts = get_exponential_posts_gen_by_day($request->input('day', 1));
dump('Day: '.$request->input('day', 1));
dump('Post Counts: '.$post_counts);
});
Route::get('/step-1', function (Request $request) {
$category = Category::find($request->input('id'));
$news_serp_result = GetNewsSerpTask::handle($category, 'US');

View File

@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "echoscoop",
"private_key_id": "90d335332507dfc685eb29f9ff2192f3b4187efa",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC0wgUXF3/LGxYL\n1+xMEu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C+yQUHJPGGnyvmofCpdY692wNPUjHX9EEKZeRmLfQW9CVTPbbN\n+va2FWVYzVZtobmxL3nKqscal05I6jQpvZPITsRaQkbHy/89m5M3yfRPn++H4Mm6\navTznvH2e8Ko+zTMJaqajnfFpV8Ynwb4tGjycaFXTsAIyRKFGCx86WUkKwKBgAHU\n0WOVKi3+GF/rcib+x4oAj8zrBqsOvXFcOgGHIVUbTdTcTd2nb3Kwi5QQmGLnzpol\nyaMQOUTVoM4vN5A8HvMCTF6LVPopf8ggMGWp/b8Sb07/u21mTBexxaM3Bf1lXUB3\nD1xKadrT95eg3r+0ulTlxvG/846U2Jjnce9PCdqxAoGAd/by0om43YdCIEhb8zAa\nF4UeZFNHTDEoB1Sfy6VXnNvzxaAAU33jpDI/msL/9s57S2QwXJ2sPKg2FJlsIjFF\n1/7gIuGP/R0+Ydn9nFfeOJEhiyQ2boR/9xnmLAbZ9sKOCOI1jqEe0baPzauKxg+k\ndYKjhRfU0S1KEP9EqokQlIM=\n-----END PRIVATE KEY-----\n",
"client_email": "echoscoop@echoscoop.iam.gserviceaccount.com",
"client_id": "106961019233346264626",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/echoscoop%40echoscoop.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}