作者 赵彬吉

AI Blog自动发布

<?php
namespace App\Console\Commands\Ai;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogOpenLog;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use App\Models\Project\Project;
use App\Models\Project\ProjectKeyword;
use App\Models\WebSetting\WebSetting;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* 自动发布AI博客任务
* Class AiBlogAutoPublish
* @package App\Console\Commands\Ai
* @author zbj
* @date 2025/3/6
*/
class AiBlogAutoPublish extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ai_blog_auto_publish {action}';
/**
* The console command description.
*
* @var string
*/
protected $description = '自动发布AI Blog';
/**
* @return bool
* @author zbj
* @date 2025/3/6
*/
public function handle()
{
$action = $this->argument('action');
if($action == 'auto_publish'){
$this->auto_publish();
}
if($action == 'auto_open'){
$this->auto_open();
}
}
public function auto_publish()
{
$this->output('开始自动发布博客文章');
$projects = Project::where('is_ai_blog', 1)->get();
foreach ($projects as $project) {
$this->output("项目{$project->id}开始自动发布");
$next_auto_date = AiBlogTaskModel::where('project_id', $project->id)->whereNotNull('next_auto_date')->orderBy('id', 'desc')->value('next_auto_date');
if($next_auto_date && $next_auto_date > date('Y-m-d')){
$this->output("项目{$project->id}未到执行时间" . $next_auto_date);
continue;
}
//核心关键词+网站关键词
$main_keywords = ProjectKeyword::where('project_id', $project->id)->value('main_keyword');
$main_keywords = explode("\r\n", $main_keywords);
ProjectServer::useProject($project->id);
$site_keywords = WebSetting::where('project_id', $project->id)->value('keyword');
DB::disconnect('custom_mysql');
$site_keywords = explode(",", $site_keywords);
$keywords = array_filter(array_merge($main_keywords, $site_keywords));
$keywords = array_map('trim', $keywords);
if (empty($keywords)) {
$this->output("项目{$project->id}未获取到关键词");
}
$last_task = AiBlogTaskModel::where('project_id', $project->id)->orderBy('id', 'desc')->first();
//如果没有发布过AI blog任务, 第一次提交3个任务
if (!$last_task) {
for ($i = 0; $i < 3; $i++) {
$this->createTask($keywords, $project->id);
}
} else {
$this->createTask($keywords, $project->id);
}
}
}
public function createTask($keywords, $project_id){
$keyword = $keywords[array_rand($keywords)];
$aiBlogService = new AiBlogService($project_id);
$result = $aiBlogService->setRoute($keyword)->createTask($keyword);
if ($result['status'] == 200) {
$aiBlogTaskModel = new AiBlogTaskModel();
$next_auto_date = date('Y-m-d', strtotime('+' . mt_rand(3,6) . 'days')); //每3-6天自动发布
$aiBlogTaskModel->addReturnId(['project_id' => $project_id, 'type' => 2, 'task_id' => $result['data']['task_id'], 'status' => 1, 'next_auto_date' => $next_auto_date]);
ProjectServer::useProject($project_id);
$aiBlogModel = new AiBlog();
$start = strtotime('10:00:00');
$end = strtotime('16:00:00');
$randomTimestamp = mt_rand($start, $end);
$created_at = date("Y-m-d H:i:s", $randomTimestamp);
$aiBlogModel->addReturnId(['keyword' => $keyword, 'status' => 1, 'task_id' => $result['data']['task_id'], 'project_id' => $project_id, 'created_at' => $created_at]);
DB::disconnect('custom_mysql');
$this->output("任务创建成功");
} else {
$this->output('任务创建失败:' . json_encode($result));
}
}
/**
* 上线的推广项目自动开启
* @author zbj
* @date 2025/3/7
*/
public function auto_open()
{
$this->output('上线的推广项目自动开启');
$projects = Project::whereIn('type', [Project::TYPE_TWO, Project::TYPE_FOUR])
->whereNotNull('uptime')->where('is_ai_blog', 0)
->get();
foreach ($projects as $project) {
//未开启过 自动开启
if (!AiBlogOpenLog::isOpened($project->id)) {
//开启
$project->is_ai_blog = 1;
$project->save();
//创建AI博客项目
(new ProjectLogic())->setAiBlog($project->id, $project->main_lang_id, 1, $project->title);
//开启日志
AiBlogOpenLog::addLog($project->id);
$this->output('自动开启项目:' . $project->id);
}
}
}
/**
* 输出message
* @param $message
*/
public function output($message)
{
Log::channel('ai_blog')->info($message);
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
}
}
... ...
... ... @@ -42,7 +42,7 @@ class AiBlogTask extends Command
public function handle(){
$aiBlogTaskModel = new AiBlogTaskModel();
while (true){
$list = $aiBlogTaskModel->list(['status'=>1,'type'=>2],'id',['*'],'asc',1000);
$list = $aiBlogTaskModel->list(['status'=>1,'type'=>2, 'created_at' => ['<', date('Y-m-d H:i:s')]],'id',['*'],'asc',1000);
if(empty($list)){
sleep(300);
continue;
... ...
... ... @@ -41,6 +41,9 @@ class Kernel extends ConsoleKernel
$schedule->command('sync_inquiry_text')->dailyAt('09:00')->withoutOverlapping(1);
//FB询盘费用同步
$schedule->command('sync_ad_cost')->everyThirtyMinutes()->withoutOverlapping(1);
// $schedule->command('ai_blog_auto_publish auto_open')->everyMinute()->withoutOverlapping(1);
$schedule->command('ai_blog_auto_publish auto_publish')->dailyAt('07:00')->withoutOverlapping(1);
}
/**
... ...
<?php
namespace App\Models\Ai;
use App\Models\Base;
/**
* AI Blog开启日志
* Class AiBlogOpenLog
* @package App\Models\Ai
* @author zbj
* @date 2025/3/7
*/
class AiBlogOpenLog extends Base
{
protected $table = 'gl_ai_blog_open_log';
public static function addLog($project_id, $manage_id = 0, $status = 1){
$log = new self();
$log->project_id = $project_id;
$log->manage_id = $manage_id;
$log->status = $status;
$log->save();
}
/**
* 是否有开启记录
* @author zbj
* @date 2025/3/7
*/
public static function isOpened($project_id){
$model = self::where('project_id', $project_id)->where('status', 1)->first();
return (bool)$model;
}
}
... ...
... ... @@ -110,7 +110,7 @@ class Base extends Model
*/
public function addReturnId($data){
$data = $this->filterRequestData($data);
$data['created_at'] = date('Y-m-d H:i:s');
$data['created_at'] = $data['created_at'] ?? date('Y-m-d H:i:s');
$data['updated_at'] = $data['created_at'];
return $this->insertGetId($data);
}
... ...
... ... @@ -9,6 +9,10 @@
namespace App\Services;
use App\Helper\Translate;
use App\Models\Project\ProjectAiSetting;
use Illuminate\Database\Eloquent\Model;
class AiBlogService
{
public $url = 'https://ai-extend.ai.cc/';
... ... @@ -21,6 +25,23 @@ class AiBlogService
public $task_id = '';//任务id
public $author_id = '';//作者id
public function __construct($project_id = 0)
{
if($project_id){
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
$this->mch_id = $aiSettingInfo['mch_id'];
$this->key = $aiSettingInfo['key'];
}
}
public function setRoute($keyword)
{
$this->route = generateRoute(Translate::tran($keyword, 'en'));
return $this;
}
/**
* @remark :创建项目
* @name :createProject
... ...
... ... @@ -191,6 +191,12 @@ return [
'level' => 'debug',
'days' => 14,
],
'ai_blog' => [
'driver' => 'daily',
'path' => storage_path('logs/ai_blog/laravel.log'),
'level' => 'debug',
'days' => 30,
],
],
//操作日志
'operator_log' =>[
... ...