Add (keyword model)

This commit is contained in:
2023-11-23 00:42:38 +08:00
parent f7d4a982e0
commit 7239b7e9ae
7 changed files with 218 additions and 12 deletions

View File

@@ -6,6 +6,7 @@
use App\Http\Controllers\Controller;
use App\Jobs\Tasks\BrowseRSSLatestNewsTask;
use App\Jobs\Tasks\CrawlRssPostTask;
use App\Jobs\Tasks\ParseRssPostMetadataTask;
use App\Models\Post;
use App\Notifications\PostWasPublished;
use Illuminate\Http\Request;
@@ -15,6 +16,22 @@
class TestController extends Controller
{
public function prm(Request $request)
{
$id = $request->input('id');
if (is_empty($id)) {
return "Missing 'id'.";
}
ParseRssPostMetadataTask::handle($id);
return 'ok';
}
public function crawlTask(Request $request)
{
$id = $request->input('id');

View File

@@ -5,6 +5,7 @@
use App\Helpers\FirstParty\OpenAI\OpenAI;
use App\Models\Category;
use App\Models\RssPost;
use App\Models\RssPostKeyword;
use App\Models\ServiceCostUsage;
class ParseRssPostMetadataTask
@@ -37,7 +38,10 @@ public static function handle(int $rss_post_id)
}
}
$words_to_add_in_body = [];
$words_to_add_in_keyword_list = [];
$words_to_save = [];
$first_keyword_found = false;
if ((isset($post_meta_response->output)) && (! is_null($post_meta_response->output))) {
@@ -50,22 +54,56 @@ public static function handle(int $rss_post_id)
}
}
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 $key => $word) {
$word = trim($word);
$words_to_save[] = (object) [
'is_main' => ($key == 0) ? true : false,
'type' => 'entity',
'value' => $word,
'value_lowercased' => strtolower($word),
];
$words_to_add_in_keyword_list[] = $word;
}
}
}
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;
$word = trim($word);
foreach($words_to_save as $saved_word)
{
if (strtolower($word) == $saved_word->value_lowercased)
{
continue 2;
}
}
$words_to_save[] = (object) [
'is_main' => !$first_keyword_found,
'type' => 'keyword',
'value' => $word,
'value_lowercased' => strtolower($word),
];
$words_to_add_in_keyword_list[] = $word;
if ($first_keyword_found == false) {
$first_keyword_found = true;
}
foreach ($post_meta_response->output->entities as $word) {
$words_to_add_in_body[] = $word;
}
}
}
@@ -104,10 +142,42 @@ public static function handle(int $rss_post_id)
$rss_post->category_id = $category->id;
}
$rss_post->keyword_list = implode(',', $words_to_add_in_body);
$rss_post->keyword_list = implode(',', $words_to_add_in_keyword_list);
$rss_post->status = 'published';
$rss_post->save();
if($rss_post->save())
{
$deleted_rpk = RssPostKeyword::where('rss_post_id', $rss_post->id)->delete();
//dump($words_to_save);
foreach ($words_to_save as $word_to_save)
{
// * @property int $id
// * @property int $rss_post_id
// * @property string $type
// * @property bool $is_main
// * @property string $value
// * @property string $value_lowercased
// * @property Carbon|null $created_at
// * @property Carbon|null $updated_at
// $words_to_save[] = (object) [
// 'type' => 'keyword',
// 'value' => $word,
// 'value_lowercased' => strtolower($word),
// ];
$new_rpk = new RssPostKeyword;
$new_rpk->rss_post_id = $rss_post->id;
$new_rpk->type = $word_to_save->type;
$new_rpk->is_main = $word_to_save->is_main;
$new_rpk->value = $word_to_save->value;
$new_rpk->value_lowercased = $word_to_save->value_lowercased;
$new_rpk->save();
}
}
}
}

View File

@@ -27,6 +27,7 @@
* @property string|null $metadata
* @property string|null $bites
* @property string|null $keyword_list
* @property bool $keyword_saved
* @property string|null $impact
* @property string $impact_level
* @property Carbon $published_at
@@ -45,6 +46,7 @@ class RssPost extends Model implements Feedable
'metadata' => 'object',
'keywords' => 'array',
'entities' => 'array',
'keyword_saved' => 'boolean',
];
protected $fillable = [
@@ -64,6 +66,7 @@ class RssPost extends Model implements Feedable
'impact_level',
'published_at',
'status',
'keyword_saved',
];
public function category()

View File

@@ -0,0 +1,49 @@
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class RssPostKeyword
*
* @property int $id
* @property int $rss_post_id
* @property string $type
* @property bool $is_main
* @property string $value
* @property string $value_lowercased
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @property RssPost $rss_post
*
* @package App\Models
*/
class RssPostKeyword extends Model
{
protected $table = 'rss_post_keywords';
protected $casts = [
'rss_post_id' => 'int',
'is_main' => 'bool'
];
protected $fillable = [
'rss_post_id',
'type',
'is_main',
'value',
'value_lowercased'
];
public function rss_post()
{
return $this->belongsTo(RssPost::class);
}
}

View File

@@ -0,0 +1,38 @@
<?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::create('rss_post_keywords', function (Blueprint $table) {
$table->id();
$table->foreignId('rss_post_id');
$table->enum('type',['keyword','entity']);
$table->boolean('is_main')->default(false);
$table->string('value');
$table->string('value_lowercased');
$table->timestamps();
$table->foreign('rss_post_id')->references('id')->on('rss_posts');
$table->index('value');
$table->index('value_lowercased');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('rss_post_keywords');
}
};

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('rss_posts', function (Blueprint $table) {
$table->boolean('keyword_saved')->default(false);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('rss_posts', function (Blueprint $table) {
$table->dropColumn('keyword_saved');
});
}
};

View File

@@ -56,6 +56,7 @@
return 'ok';
});
Route::get('/prm', [App\Http\Controllers\Tests\TestController::class, 'prm']);
Route::get('/opml', [App\Http\Controllers\Tests\TestController::class, 'opml']);