GeoQuestionRes.php
10.9 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* @remark :
* @name :GeoQuestionResController.php
* @author :lyh
* @method :post
* @time :2025/7/3 15:13
*/
namespace App\Console\Commands\Geo;
use App\Models\Geo\GeoPlatform;
use App\Models\Geo\GeoQuestion;
use App\Models\Geo\GeoQuestionLog;
use App\Models\Geo\GeoQuestionResult;
use App\Models\Project\Project;
use App\Services\Geo\GeoService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class GeoQuestionRes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'geo_question_result';
/**
* The console command description.
*
* @var string
*/
protected $description = 'geo设置请求获取结果';
/**
* @return bool
*/
public function handle()
{
while (true) {
$task_id = $this->getTaskId();
if (empty($task_id)) {
sleep(300);
continue;
}
echo date('Y-m-d H:i:s').'执行的任务id:'.$task_id.PHP_EOL;
$geoQuestionModel = new GeoQuestion();
$taskInfo = $geoQuestionModel->read(['id'=>$task_id]);
if ($taskInfo === false) {
$this->output('当前任务详情为空!');
continue;
}
$projectModel = new Project();
$projectInfo = $projectModel->read(['id' => $taskInfo['project_id']],['geo_status', 'geo_frequency']);
if ($projectInfo === false) {
$this->output('未获取到项目详情!');
$geoQuestionModel->edit(['status'=>$geoQuestionModel::STATUS_CLOSE],['id'=>$task_id]);
continue;
}
if(empty($taskInfo['question']) || empty($taskInfo['keywords']) || empty($taskInfo['url'])){
$this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!');
$geoQuestionModel->edit(['status'=>$geoQuestionModel::STATUS_CLOSE],['id'=>$task_id]);
continue;
}
$geoPlatformModel = new GeoPlatform();
$platformsArr = $geoPlatformModel->selectField(['status' => GeoPlatform::STATUS_ON],'en_name');
if (empty($platformsArr)) {
$this->output('未设置AI模型!');
continue;
}
$geo_service = new GeoService();
$geoResultModel = new GeoQuestionResult();
$geoLogModel = new GeoQuestionLog();
foreach ($taskInfo['question'] as $question) {
foreach ($platformsArr as $platform) {
$data = [];
$error_num = 0;
// 设置重试, 有的平台不一定能正常获取到数据
GET_RESULT:
$error_num++;
try {
echo '执行次数:'.$error_num.PHP_EOL;
if ($error_num >= 5) {
continue;
}
echo '执行平台:'.$platform.PHP_EOL;
if ($platform == 'Google AI Overview') {
// overview 数据结构不确定, 需要单独处理数据
$data = $geo_service->getGooglePlatformResult($question);
$result = $this->dealGoogleData($data);
} else {
$result = $geo_service->getAiPlatformResult($question, $platform);
}
if (empty($result['text'])){
goto GET_RESULT;
}
} catch (\Exception $e) {
$this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $question . ', error: ' . $e->getMessage());
goto GET_RESULT;
}
// 命中文案
$hit_data[] = $result['text'];
if(!empty($result['annotations'])){
$url = array_column(array_column($result['annotations'], 'url_citation'), 'url');
$title = array_column(array_column($result['annotations'], 'url_citation'), 'title');;
$hit_data = array_merge($url, $title, $hit_data);
}
// 命中关键词和路由
$hit_keyword = $hit_url = [];
$hit = 0;
if (!empty($taskInfo['keywords'])) {
$hit_keyword = $this->getKeywords($taskInfo['keywords'],$hit_data);
if (!empty($hit_keyword)) {
$hit++;
}
}
if (!empty($taskInfo['url'])) {
$hit_url = $this->getUrl($taskInfo['url'],$hit_data);
if (!empty($hit_url)) {
$hit++;
}
}
// 保存数据结果
$geo_result = $geoResultModel->read(['project_id' => $taskInfo['project_id'], 'question_id' => $task_id, 'platform' => $platform, 'question' => $question],['id']);
$save_data = [
'project_id' => $taskInfo['project_id'],
'question_id' => $task_id,
'type' => $taskInfo['type'] ?? $geoQuestionModel::TYPE_BRAND,
'platform' => $platform,
'question' => $question,
'keywords' => json_encode($hit_keyword,true),//命中的关键词
'url' => json_encode($hit_url,true),//命中的网址
'text' => json_encode($result,true),
'hit' => $hit
];
if($geo_result === false){
$geoResultModel->addReturnId($save_data);
}else{
$geoResultModel->edit($save_data, ['id' => $geo_result['id']]);
}
$save_data['text'] = json_encode(!empty($data) ? $data : $result,true);
$geoLogModel->addReturnId($save_data);
echo '执行结束:'.$platform.PHP_EOL;
}
}
$next_time = date('Y-m-d', strtotime('+' . ($projectInfo['geo_frequency'] ?? 3) . ' days'));
$geoQuestionModel->edit(['current_time'=>date('Y-m-d'),'next_time'=>$next_time],['id'=>$task_id]);
}
return true;
}
/**
* @remark :获取命中的url
* @name :getUrl
* @author :lyh
* @method :post
* @time :2025/7/3 16:38
*/
public function getUrl($urlArr = [],$result_annotations = []){
$url = [];
if(!empty($urlArr)){
$str = implode(',',$result_annotations);
foreach ($urlArr as $u_item){
if (str_contains($str, $u_item)) {
$url[] = $u_item;
}
}
}
return array_values(array_unique($url));
}
/**
* @remark :获取命中的关键词
* @name :getKeywords
* @author :lyh
* @method :post
* @time :2025/7/3 16:26
*/
public function getKeywords($keywordArr = [],$result_text = []){
$keywords = [];
if(!empty($keywordArr) && !empty($result_text)){
$str = implode(',',$result_text);
foreach ($keywordArr as $k_item){
if (str_contains($str, $k_item)) {
$keywords[] = $k_item;
}
}
}
return $keywords;
}
/**
* 整合Google平台数据
* @param $data
* @return array
*/
public function dealGoogleData($data)
{
$result = [
'code' => 200,
'model' => 'Google AI Overview',
'text' => '',
];
$texts = [];
if(!empty($data['data']['text_parts']) && is_array($data['data']['text_parts'])){
foreach ($data['data']['text_parts'] as $item){
if(empty($item['text'])){
continue;
}
switch ($item['type']){
case 'paragraph':
if(isset($item['text']) && !empty($item['text'])){
array_push($texts, $item['text']);
}
break;
case 'title':
if(isset($item['text']) && !empty($item['text'])) {
array_unshift($texts, $item['text']);
}
break;
case 'list':
if(!empty($item['list'])){
foreach ($item['list'] as $sonItem){
if(isset($sonItem['text']) && !empty($sonItem['text'])) {
array_push($texts, $sonItem['text']);
}
if(isset($item['title']) && !empty($item['title'])) {
array_push($texts, $sonItem['title']);
}
}
}
break;
default:
break;
}
}
}
if(!empty($data['data']['reference_links']) && is_array($data['data']['reference_links'])){
foreach ($data['data']['reference_links'] as $link) {
if (isset($link['text']) && !empty($link['text']) && isset($link['link']) && !empty($link['link'])) {
$result['annotations'][] = [
'type' => 'url_citation',
'url_citation' => [
'url' => $link['link'],
'title' => $link['title']
],
];
}
}
}
$text = implode(PHP_EOL, $texts);
$result['text'] = $text;
return $result;
}
/**
* 获取待执行任务ID
* @return mixed
*/
public function getTaskId(){
$key = 'geo_task_list';
$task_id = Redis::rpop($key);
if(empty($task_id)){
$questionModel = new GeoQuestion();
$ids = $questionModel->selectField(['status'=>$questionModel::STATUS_OPEN,'next_time'=>['<=',date('Y-m-d')]],'id');
if(!empty($ids)){
foreach ($ids as $id) {
Redis::lpush($key, $id);
}
}
$task_id = Redis::rpop($key);
}
return $task_id;
}
/**
* 输出日志
* @param $message
* @return bool
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
return true;
}
}