|
...
|
...
|
@@ -34,7 +34,182 @@ class GeoQuestionRes extends Command |
|
|
|
*/
|
|
|
|
protected $description = 'geo设置请求获取结果';
|
|
|
|
|
|
|
|
public function handle(){
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
// $task_id = $this->getTaskId();
|
|
|
|
$task_id = 5;
|
|
|
|
if (empty($task_id)) {
|
|
|
|
sleep(300);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$task = GeoQuestion::where(['id' => $task_id, 'status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->first();
|
|
|
|
if (empty($task)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$project = Project::select(['geo_status', 'geo_frequency'])->where(['id' => $task->project_id])->first();
|
|
|
|
if (empty($project->get_status)) {
|
|
|
|
$task->status = GeoQuestion::STATUS_CLOSE;
|
|
|
|
$task->save();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((empty($task->question) || FALSE == is_array($task->question)) || (empty($task->keywords) && empty($task->url))) {
|
|
|
|
$this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!');
|
|
|
|
$task->status = GeoQuestion::STATUS_CLOSE;
|
|
|
|
$task->save();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$platforms = GeoPlatform::where(['status' => GeoPlatform::STATUS_ON])->get();
|
|
|
|
if ($platforms->isEmpty) {
|
|
|
|
$this->output('未设置AI模型!');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$geo_service = new GeoService();
|
|
|
|
$geoResultModel = new GeoQuestionResult();
|
|
|
|
$geoLogModel = new GeoQuestionLog();
|
|
|
|
foreach ($task->question as $question) {
|
|
|
|
$error_num = 0;
|
|
|
|
foreach ($platforms as $platform) {
|
|
|
|
// 设置重试, 有的平台不一定能正常获取到数据
|
|
|
|
GET_RESULT:
|
|
|
|
$error_num++;
|
|
|
|
try {
|
|
|
|
if ($platform->en_name == 'Google AI Overview') {
|
|
|
|
// overview 数据结构不确定, 需要单独处理数据
|
|
|
|
$data = $geo_service->getGooglePlatformResult($question);
|
|
|
|
$result = $this->dealGoogleData($data);
|
|
|
|
} else {
|
|
|
|
$result = $geo_service->getAiPlatformResult($question, $platform->en_name);
|
|
|
|
}
|
|
|
|
if (empty($result['text']))
|
|
|
|
goto GET_RESULT;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->output('task id:' . $task_id . ', question: ' . $question . ', platform: ' . $question . ', error: ' . $e->getMessage());
|
|
|
|
if ($error_num < 5) {
|
|
|
|
goto GET_RESULT;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 命中文案
|
|
|
|
$hit_data[] = $result['text'];
|
|
|
|
if(FALSE == 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_string = implode(',', $hit_data);
|
|
|
|
|
|
|
|
// 命中关键词和路由
|
|
|
|
$hit_keyword = $hit_url = [];
|
|
|
|
$hit = 0;
|
|
|
|
if ($task->keywords) {
|
|
|
|
$pattern = '/(' . implode('|', array_map('preg_quote', $task->keywords)) . ')/i';
|
|
|
|
if (preg_match($pattern, $hit_string, $matches)) {
|
|
|
|
$hit_keyword = $matches[0];
|
|
|
|
$hit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($task->url) {
|
|
|
|
$pattern = '/(' . implode('|', array_map('preg_quote', $task->url)) . ')/i';
|
|
|
|
if (preg_match($pattern, $hit_string, $matches)) {
|
|
|
|
$hit_url = $matches[0];
|
|
|
|
$hit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 保存数据结果
|
|
|
|
$geo_result = GeoQuestionResult::where(['project_id' => $task['project_id'], 'question_id' => $task['id'], 'platform' => $platform, 'question' => $question])->first();
|
|
|
|
$save_data = [
|
|
|
|
'project_id' => $task->project_id,
|
|
|
|
'question_id' => $task->id,
|
|
|
|
'type' => $task->type ?? GeoQuestion::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(empty($geo_result)){
|
|
|
|
$geoResultModel->addReturnId($save_data);
|
|
|
|
}else{
|
|
|
|
$geoResultModel->edit($save_data, ['id' => $geo_result->id]);
|
|
|
|
}
|
|
|
|
$save_data['text'] = json_encode(FALSE == empty($data) ? $data : $result,true);
|
|
|
|
$geoLogModel->addReturnId($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$task->current_time = date('Y-m-d');
|
|
|
|
$task->next_time = date('Y-m-d', strtotime('+' . $project->geo_frequency . ' days'));
|
|
|
|
$task->save();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 整合Google平台数据
|
|
|
|
* @param $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dealGoogleData($data)
|
|
|
|
{
|
|
|
|
$result = [
|
|
|
|
'code' => 200,
|
|
|
|
'model' => 'Google AI Overview',
|
|
|
|
'text' => '',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (FALSE == empty($data['ai_overview']['texts']) && is_array($data['ai_overview']['texts'])) {
|
|
|
|
$texts = [];
|
|
|
|
foreach ($data['ai_overview']['texts'] as $item) {
|
|
|
|
// 提取链接
|
|
|
|
if (FALSE == empty($item['links'])) {
|
|
|
|
foreach ($item['links'] as $link) {
|
|
|
|
if (FALSE == empty($link['text']) && FALSE == empty($link['link'])) {
|
|
|
|
$result['annotations'][] = [
|
|
|
|
'type' => 'url_citation',
|
|
|
|
'url_citation' => [
|
|
|
|
'url' => $link['link'],
|
|
|
|
'title' => $link['text']
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 第一层就有内容
|
|
|
|
if (FALSE == empty($item['snippet'])) {
|
|
|
|
// title 放到数组最前面
|
|
|
|
if (FALSE == empty($item['type']) && $item['type'] == 'title')
|
|
|
|
array_unshift($texts, $item['snippet']);
|
|
|
|
else
|
|
|
|
array_push($texts, $item['snippet']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// list类型
|
|
|
|
if (FALSE == empty($item['type']) && $item['type'] == 'list' && FALSE == empty($item['list']) && is_array($item['list'])) {
|
|
|
|
foreach ($item['list'] as $list) {
|
|
|
|
if (FALSE == empty($list['snippet']))
|
|
|
|
array_push($texts, $list['snippet']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$text = implode(PHP_EOL, $texts);
|
|
|
|
$result['text'] = $text;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle1(){
|
|
|
|
while (true){
|
|
|
|
$task_id = $this->getTaskId();
|
|
|
|
if(empty($task_id)){
|
|
...
|
...
|
@@ -99,7 +274,7 @@ class GeoQuestionRes extends Command |
|
|
|
'keywords'=>json_encode($keywords ?? [],true),//命中的关键词
|
|
|
|
'text'=>json_encode($result_data ?? [],true),
|
|
|
|
'url'=>json_encode($urls ?? [],true),//命中的网址
|
|
|
|
'type'=>$info['type'] ?? 1
|
|
|
|
'type'=>$info['type'] ?? GeoQuestion::TYPE_BRAND
|
|
|
|
];
|
|
|
|
if($resultInfo === false){
|
|
|
|
$geoResultModel->addReturnId($data);
|
|
...
|
...
|
@@ -157,24 +332,32 @@ class GeoQuestionRes extends Command |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :拉取任务id
|
|
|
|
* @name :getTaskId
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2025/7/3 15:15
|
|
|
|
* 获取待执行任务ID
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getTaskId(){
|
|
|
|
$task_id = Redis::rpop('geo_question_result');
|
|
|
|
if(empty($task_id)){
|
|
|
|
$questionModel = new GeoQuestion();
|
|
|
|
$ids = $questionModel->selectField(['status'=>1,'next_time'=>['<=',date('Y-m-d')]],'id');
|
|
|
|
if(!empty($ids)){
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
Redis::lpush('geo_question_result', $id);
|
|
|
|
}
|
|
|
|
$key = 'geo_task_list';
|
|
|
|
$task_id = Redis::rpop($key);
|
|
|
|
if (empty($task_id)) {
|
|
|
|
$ids = GeoQuestion::where(['status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->get('id');
|
|
|
|
if ($ids->isEmpty())
|
|
|
|
return $task_id;
|
|
|
|
foreach ($ids as $item) {
|
|
|
|
Redis::lpush($key, $item->id);
|
|
|
|
}
|
|
|
|
$task_id = Redis::rpop('geo_question_result');
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|