|
@@ -34,7 +34,182 @@ class GeoQuestionRes extends Command |
|
@@ -34,7 +34,182 @@ class GeoQuestionRes extends Command |
|
34
|
*/
|
34
|
*/
|
|
35
|
protected $description = 'geo设置请求获取结果';
|
35
|
protected $description = 'geo设置请求获取结果';
|
|
36
|
|
36
|
|
|
37
|
- public function handle(){
|
37
|
+
|
|
|
|
38
|
+ /**
|
|
|
|
39
|
+ * @return bool
|
|
|
|
40
|
+ */
|
|
|
|
41
|
+ public function handle()
|
|
|
|
42
|
+ {
|
|
|
|
43
|
+ while (true) {
|
|
|
|
44
|
+// $task_id = $this->getTaskId();
|
|
|
|
45
|
+ $task_id = 5;
|
|
|
|
46
|
+ if (empty($task_id)) {
|
|
|
|
47
|
+ sleep(300);
|
|
|
|
48
|
+ continue;
|
|
|
|
49
|
+ }
|
|
|
|
50
|
+ $task = GeoQuestion::where(['id' => $task_id, 'status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->first();
|
|
|
|
51
|
+ if (empty($task)) {
|
|
|
|
52
|
+ continue;
|
|
|
|
53
|
+ }
|
|
|
|
54
|
+
|
|
|
|
55
|
+ $project = Project::select(['geo_status', 'geo_frequency'])->where(['id' => $task->project_id])->first();
|
|
|
|
56
|
+ if (empty($project->get_status)) {
|
|
|
|
57
|
+ $task->status = GeoQuestion::STATUS_CLOSE;
|
|
|
|
58
|
+ $task->save();
|
|
|
|
59
|
+ continue;
|
|
|
|
60
|
+ }
|
|
|
|
61
|
+
|
|
|
|
62
|
+ if ((empty($task->question) || FALSE == is_array($task->question)) || (empty($task->keywords) && empty($task->url))) {
|
|
|
|
63
|
+ $this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!');
|
|
|
|
64
|
+ $task->status = GeoQuestion::STATUS_CLOSE;
|
|
|
|
65
|
+ $task->save();
|
|
|
|
66
|
+ continue;
|
|
|
|
67
|
+ }
|
|
|
|
68
|
+
|
|
|
|
69
|
+ $platforms = GeoPlatform::where(['status' => GeoPlatform::STATUS_ON])->get();
|
|
|
|
70
|
+ if ($platforms->isEmpty) {
|
|
|
|
71
|
+ $this->output('未设置AI模型!');
|
|
|
|
72
|
+ continue;
|
|
|
|
73
|
+ }
|
|
|
|
74
|
+
|
|
|
|
75
|
+ $geo_service = new GeoService();
|
|
|
|
76
|
+ $geoResultModel = new GeoQuestionResult();
|
|
|
|
77
|
+ $geoLogModel = new GeoQuestionLog();
|
|
|
|
78
|
+ foreach ($task->question as $question) {
|
|
|
|
79
|
+ $error_num = 0;
|
|
|
|
80
|
+ foreach ($platforms as $platform) {
|
|
|
|
81
|
+ // 设置重试, 有的平台不一定能正常获取到数据
|
|
|
|
82
|
+ GET_RESULT:
|
|
|
|
83
|
+ $error_num++;
|
|
|
|
84
|
+ try {
|
|
|
|
85
|
+ if ($platform->en_name == 'Google AI Overview') {
|
|
|
|
86
|
+ // overview 数据结构不确定, 需要单独处理数据
|
|
|
|
87
|
+ $data = $geo_service->getGooglePlatformResult($question);
|
|
|
|
88
|
+ $result = $this->dealGoogleData($data);
|
|
|
|
89
|
+ } else {
|
|
|
|
90
|
+ $result = $geo_service->getAiPlatformResult($question, $platform->en_name);
|
|
|
|
91
|
+ }
|
|
|
|
92
|
+ if (empty($result['text']))
|
|
|
|
93
|
+ goto GET_RESULT;
|
|
|
|
94
|
+ } catch (\Exception $e) {
|
|
|
|
95
|
+ $this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $question . ', error: ' . $e->getMessage());
|
|
|
|
96
|
+ if ($error_num < 5) {
|
|
|
|
97
|
+ goto GET_RESULT;
|
|
|
|
98
|
+ }
|
|
|
|
99
|
+ continue;
|
|
|
|
100
|
+ }
|
|
|
|
101
|
+
|
|
|
|
102
|
+ // 命中文案
|
|
|
|
103
|
+ $hit_data[] = $result['text'];
|
|
|
|
104
|
+ if(FALSE == empty($result['annotations'])){
|
|
|
|
105
|
+ $url = array_column(array_column($result['annotations'], 'url_citation'), 'url');
|
|
|
|
106
|
+ $title = array_column(array_column($result['annotations'], 'url_citation'), 'title');;
|
|
|
|
107
|
+ $hit_data = array_merge($url, $title, $hit_data);
|
|
|
|
108
|
+ }
|
|
|
|
109
|
+ $hit_string = implode(',', $hit_data);
|
|
|
|
110
|
+
|
|
|
|
111
|
+ // 命中关键词和路由
|
|
|
|
112
|
+ $hit_keyword = $hit_url = [];
|
|
|
|
113
|
+ $hit = 0;
|
|
|
|
114
|
+ if ($task->keywords) {
|
|
|
|
115
|
+ $pattern = '/(' . implode('|', array_map('preg_quote', $task->keywords)) . ')/i';
|
|
|
|
116
|
+ if (preg_match($pattern, $hit_string, $matches)) {
|
|
|
|
117
|
+ $hit_keyword = $matches[0];
|
|
|
|
118
|
+ $hit++;
|
|
|
|
119
|
+ }
|
|
|
|
120
|
+ }
|
|
|
|
121
|
+ if ($task->url) {
|
|
|
|
122
|
+ $pattern = '/(' . implode('|', array_map('preg_quote', $task->url)) . ')/i';
|
|
|
|
123
|
+ if (preg_match($pattern, $hit_string, $matches)) {
|
|
|
|
124
|
+ $hit_url = $matches[0];
|
|
|
|
125
|
+ $hit++;
|
|
|
|
126
|
+ }
|
|
|
|
127
|
+ }
|
|
|
|
128
|
+
|
|
|
|
129
|
+ // 保存数据结果
|
|
|
|
130
|
+ $geo_result = GeoQuestionResult::where(['project_id' => $task['project_id'], 'question_id' => $task['id'], 'platform' => $platform, 'question' => $question])->first();
|
|
|
|
131
|
+ $save_data = [
|
|
|
|
132
|
+ 'project_id' => $task->project_id,
|
|
|
|
133
|
+ 'question_id' => $task->id,
|
|
|
|
134
|
+ 'type' => $task->type ?? GeoQuestion::TYPE_BRAND,
|
|
|
|
135
|
+ 'platform' => $platform,
|
|
|
|
136
|
+ 'question' => $question,
|
|
|
|
137
|
+ 'keywords' => json_encode($hit_keyword,true),//命中的关键词
|
|
|
|
138
|
+ 'url' => json_encode($hit_url,true),//命中的网址
|
|
|
|
139
|
+ 'text' => json_encode($result,true),
|
|
|
|
140
|
+ 'hit' => $hit
|
|
|
|
141
|
+ ];
|
|
|
|
142
|
+ if(empty($geo_result)){
|
|
|
|
143
|
+ $geoResultModel->addReturnId($save_data);
|
|
|
|
144
|
+ }else{
|
|
|
|
145
|
+ $geoResultModel->edit($save_data, ['id' => $geo_result->id]);
|
|
|
|
146
|
+ }
|
|
|
|
147
|
+ $save_data['text'] = json_encode(FALSE == empty($data) ? $data : $result,true);
|
|
|
|
148
|
+ $geoLogModel->addReturnId($data);
|
|
|
|
149
|
+ }
|
|
|
|
150
|
+ }
|
|
|
|
151
|
+ $task->current_time = date('Y-m-d');
|
|
|
|
152
|
+ $task->next_time = date('Y-m-d', strtotime('+' . $project->geo_frequency . ' days'));
|
|
|
|
153
|
+ $task->save();
|
|
|
|
154
|
+ }
|
|
|
|
155
|
+ return true;
|
|
|
|
156
|
+ }
|
|
|
|
157
|
+
|
|
|
|
158
|
+ /**
|
|
|
|
159
|
+ * 整合Google平台数据
|
|
|
|
160
|
+ * @param $data
|
|
|
|
161
|
+ * @return array
|
|
|
|
162
|
+ */
|
|
|
|
163
|
+ public function dealGoogleData($data)
|
|
|
|
164
|
+ {
|
|
|
|
165
|
+ $result = [
|
|
|
|
166
|
+ 'code' => 200,
|
|
|
|
167
|
+ 'model' => 'Google AI Overview',
|
|
|
|
168
|
+ 'text' => '',
|
|
|
|
169
|
+ ];
|
|
|
|
170
|
+
|
|
|
|
171
|
+ if (FALSE == empty($data['ai_overview']['texts']) && is_array($data['ai_overview']['texts'])) {
|
|
|
|
172
|
+ $texts = [];
|
|
|
|
173
|
+ foreach ($data['ai_overview']['texts'] as $item) {
|
|
|
|
174
|
+ // 提取链接
|
|
|
|
175
|
+ if (FALSE == empty($item['links'])) {
|
|
|
|
176
|
+ foreach ($item['links'] as $link) {
|
|
|
|
177
|
+ if (FALSE == empty($link['text']) && FALSE == empty($link['link'])) {
|
|
|
|
178
|
+ $result['annotations'][] = [
|
|
|
|
179
|
+ 'type' => 'url_citation',
|
|
|
|
180
|
+ 'url_citation' => [
|
|
|
|
181
|
+ 'url' => $link['link'],
|
|
|
|
182
|
+ 'title' => $link['text']
|
|
|
|
183
|
+ ],
|
|
|
|
184
|
+ ];
|
|
|
|
185
|
+ }
|
|
|
|
186
|
+ }
|
|
|
|
187
|
+ }
|
|
|
|
188
|
+
|
|
|
|
189
|
+ // 第一层就有内容
|
|
|
|
190
|
+ if (FALSE == empty($item['snippet'])) {
|
|
|
|
191
|
+ // title 放到数组最前面
|
|
|
|
192
|
+ if (FALSE == empty($item['type']) && $item['type'] == 'title')
|
|
|
|
193
|
+ array_unshift($texts, $item['snippet']);
|
|
|
|
194
|
+ else
|
|
|
|
195
|
+ array_push($texts, $item['snippet']);
|
|
|
|
196
|
+ }
|
|
|
|
197
|
+
|
|
|
|
198
|
+ // list类型
|
|
|
|
199
|
+ if (FALSE == empty($item['type']) && $item['type'] == 'list' && FALSE == empty($item['list']) && is_array($item['list'])) {
|
|
|
|
200
|
+ foreach ($item['list'] as $list) {
|
|
|
|
201
|
+ if (FALSE == empty($list['snippet']))
|
|
|
|
202
|
+ array_push($texts, $list['snippet']);
|
|
|
|
203
|
+ }
|
|
|
|
204
|
+ }
|
|
|
|
205
|
+ }
|
|
|
|
206
|
+ $text = implode(PHP_EOL, $texts);
|
|
|
|
207
|
+ $result['text'] = $text;
|
|
|
|
208
|
+ }
|
|
|
|
209
|
+ return $result;
|
|
|
|
210
|
+ }
|
|
|
|
211
|
+
|
|
|
|
212
|
+ public function handle1(){
|
|
38
|
while (true){
|
213
|
while (true){
|
|
39
|
$task_id = $this->getTaskId();
|
214
|
$task_id = $this->getTaskId();
|
|
40
|
if(empty($task_id)){
|
215
|
if(empty($task_id)){
|
|
@@ -99,7 +274,7 @@ class GeoQuestionRes extends Command |
|
@@ -99,7 +274,7 @@ class GeoQuestionRes extends Command |
|
99
|
'keywords'=>json_encode($keywords ?? [],true),//命中的关键词
|
274
|
'keywords'=>json_encode($keywords ?? [],true),//命中的关键词
|
|
100
|
'text'=>json_encode($result_data ?? [],true),
|
275
|
'text'=>json_encode($result_data ?? [],true),
|
|
101
|
'url'=>json_encode($urls ?? [],true),//命中的网址
|
276
|
'url'=>json_encode($urls ?? [],true),//命中的网址
|
|
102
|
- 'type'=>$info['type'] ?? 1
|
277
|
+ 'type'=>$info['type'] ?? GeoQuestion::TYPE_BRAND
|
|
103
|
];
|
278
|
];
|
|
104
|
if($resultInfo === false){
|
279
|
if($resultInfo === false){
|
|
105
|
$geoResultModel->addReturnId($data);
|
280
|
$geoResultModel->addReturnId($data);
|
|
@@ -157,24 +332,32 @@ class GeoQuestionRes extends Command |
|
@@ -157,24 +332,32 @@ class GeoQuestionRes extends Command |
|
157
|
}
|
332
|
}
|
|
158
|
|
333
|
|
|
159
|
/**
|
334
|
/**
|
|
160
|
- * @remark :拉取任务id
|
|
|
|
161
|
- * @name :getTaskId
|
|
|
|
162
|
- * @author :lyh
|
|
|
|
163
|
- * @method :post
|
|
|
|
164
|
- * @time :2025/7/3 15:15
|
335
|
+ * 获取待执行任务ID
|
|
|
|
336
|
+ * @return mixed
|
|
165
|
*/
|
337
|
*/
|
|
166
|
public function getTaskId(){
|
338
|
public function getTaskId(){
|
|
167
|
- $task_id = Redis::rpop('geo_question_result');
|
|
|
|
168
|
- if(empty($task_id)){
|
|
|
|
169
|
- $questionModel = new GeoQuestion();
|
|
|
|
170
|
- $ids = $questionModel->selectField(['status'=>1,'next_time'=>['<=',date('Y-m-d')]],'id');
|
|
|
|
171
|
- if(!empty($ids)){
|
|
|
|
172
|
- foreach ($ids as $id) {
|
|
|
|
173
|
- Redis::lpush('geo_question_result', $id);
|
|
|
|
174
|
- }
|
339
|
+ $key = 'geo_task_list';
|
|
|
|
340
|
+ $task_id = Redis::rpop($key);
|
|
|
|
341
|
+ if (empty($task_id)) {
|
|
|
|
342
|
+ $ids = GeoQuestion::where(['status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->get('id');
|
|
|
|
343
|
+ if ($ids->isEmpty())
|
|
|
|
344
|
+ return $task_id;
|
|
|
|
345
|
+ foreach ($ids as $item) {
|
|
|
|
346
|
+ Redis::lpush($key, $item->id);
|
|
175
|
}
|
347
|
}
|
|
176
|
- $task_id = Redis::rpop('geo_question_result');
|
348
|
+ $task_id = Redis::rpop($key);
|
|
177
|
}
|
349
|
}
|
|
178
|
return $task_id;
|
350
|
return $task_id;
|
|
179
|
}
|
351
|
}
|
|
|
|
352
|
+
|
|
|
|
353
|
+ /**
|
|
|
|
354
|
+ * 输出日志
|
|
|
|
355
|
+ * @param $message
|
|
|
|
356
|
+ * @return bool
|
|
|
|
357
|
+ */
|
|
|
|
358
|
+ public function output($message)
|
|
|
|
359
|
+ {
|
|
|
|
360
|
+ echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
|
|
|
|
361
|
+ return true;
|
|
|
|
362
|
+ }
|
|
180
|
} |
363
|
} |