118 lines
4.1 KiB
PHP
118 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Tasks;
|
|
|
|
use App\Helpers\FirstParty\OpenAI\OpenAI;
|
|
use App\Models\Category;
|
|
use App\Models\RssPost;
|
|
use App\Models\ServiceCostUsage;
|
|
|
|
class ParseRssPostMetadataTask
|
|
{
|
|
public static function handle(int $rss_post_id)
|
|
{
|
|
$rss_post = RssPost::find($rss_post_id);
|
|
|
|
if (is_null($rss_post)) {
|
|
return;
|
|
}
|
|
|
|
if (in_array($rss_post->status, ['blocked', 'trashed'])) {
|
|
return;
|
|
}
|
|
|
|
if (! is_null($rss_post->metadata)) {
|
|
$post_meta_response = $rss_post->metadata;
|
|
} else {
|
|
$post_meta_response = OpenAI::getRssPostMeta($rss_post->body, 1536, 30);
|
|
|
|
if ((isset($post_meta_response->output)) && (! is_null($post_meta_response->output))) {
|
|
$service_cost_usage = new ServiceCostUsage;
|
|
$service_cost_usage->cost = $post_meta_response->cost;
|
|
$service_cost_usage->name = 'openai-getRssPostMeta';
|
|
$service_cost_usage->reference_1 = 'rss_post';
|
|
$service_cost_usage->reference_2 = strval($rss_post->id);
|
|
$service_cost_usage->output = $post_meta_response;
|
|
$service_cost_usage->save();
|
|
}
|
|
}
|
|
|
|
$words_to_add_in_body = [];
|
|
|
|
if ((isset($post_meta_response->output)) && (! is_null($post_meta_response->output))) {
|
|
|
|
$rss_post->metadata = $post_meta_response;
|
|
|
|
if (isset($post_meta_response->output->title)) {
|
|
if (! is_empty($post_meta_response->output->title)) {
|
|
$rss_post->title = $post_meta_response->output->title;
|
|
$rss_post->slug = ($post_meta_response->output->title);
|
|
}
|
|
}
|
|
|
|
if (isset($post_meta_response->output->keywords)) {
|
|
if (count($post_meta_response->output->keywords) > 0) {
|
|
$rss_post->keywords = $post_meta_response->output->keywords;
|
|
|
|
foreach ($post_meta_response->output->keywords as $word)
|
|
{
|
|
$words_to_add_in_body[] = $word;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isset($post_meta_response->output->entities)) {
|
|
if (count($post_meta_response->output->entities) > 0) {
|
|
$rss_post->entities = $post_meta_response->output->entities;
|
|
|
|
foreach ($post_meta_response->output->entities as $word)
|
|
{
|
|
$words_to_add_in_body[] = $word;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isset($post_meta_response->output->summary)) {
|
|
if (! is_empty($post_meta_response->output->summary)) {
|
|
$rss_post->bites = $post_meta_response->output->summary;
|
|
}
|
|
}
|
|
|
|
if (isset($post_meta_response->output->society_impact)) {
|
|
if (! is_empty($post_meta_response->output->society_impact)) {
|
|
$rss_post->impact = $post_meta_response->output->society_impact;
|
|
}
|
|
}
|
|
|
|
if (isset($post_meta_response->output->society_impact_level)) {
|
|
if (! is_empty($post_meta_response->output->society_impact_level)) {
|
|
$rss_post->impact_level = $post_meta_response->output->society_impact_level;
|
|
}
|
|
}
|
|
|
|
// Category
|
|
$category_name = 'Updates';
|
|
|
|
if ((isset($post_meta_response->output->category)) && (! is_empty($post_meta_response->output->category))) {
|
|
$category_name = $post_meta_response?->output?->category;
|
|
}
|
|
|
|
$category = Category::where('name', $category_name)->first();
|
|
|
|
if (is_null($category)) {
|
|
$category = Category::where('name', 'Updates')->first();
|
|
}
|
|
|
|
$rss_post->category_id = $category->id;
|
|
}
|
|
|
|
$post_body = $rss_post->body;
|
|
$post_body .= implode($words_to_add_in_body);
|
|
$rss_post->body = $post_body;
|
|
|
|
$rss_post->status = 'published';
|
|
$rss_post->save();
|
|
|
|
}
|
|
}
|