AiBlogAutoPublish.php
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?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;
}
}