|
...
|
...
|
@@ -34,147 +34,198 @@ class GeoQuestionRes extends Command |
|
|
|
*/
|
|
|
|
protected $description = 'geo设置请求获取结果';
|
|
|
|
|
|
|
|
public function handle(){
|
|
|
|
while (true){
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
$task_id = $this->getTaskId();
|
|
|
|
if(empty($task_id)){
|
|
|
|
sleep(30);
|
|
|
|
if (empty($task_id)) {
|
|
|
|
sleep(300);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
echo date('Y-m-d H:i:s').'->task_id:'.$task_id.PHP_EOL;
|
|
|
|
$questionModel = new GeoQuestion();//问题
|
|
|
|
$info = $questionModel->read(['id'=>$task_id]);
|
|
|
|
//获取当前项目的执行频率
|
|
|
|
$projectModel = new Project();
|
|
|
|
$projectInfo = $projectModel->read(['id'=>$info['project_id']],['geo_status','geo_frequency']);
|
|
|
|
if($projectInfo['geo_status'] == 0){
|
|
|
|
$questionModel->edit(['status'=>0],['id'=>$task_id]);
|
|
|
|
$task = GeoQuestion::where(['id' => $task_id, 'status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->first();
|
|
|
|
if (empty($task)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$questionArr = $info['question'];
|
|
|
|
if(empty($questionArr)){
|
|
|
|
echo date('Y-m-d H:i:s').'当前任务不存在问题。'.PHP_EOL;
|
|
|
|
$questionModel->edit(['status'=>0],['id'=>$task_id]);
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
//获取平台信息
|
|
|
|
$platformModel = new GeoPlatform();//平台
|
|
|
|
$platformArr = $platformModel->selectField(['status'=>$platformModel::STATUS_ON],'en_name');
|
|
|
|
if(empty($platformArr)){
|
|
|
|
echo date('Y-m-d H:i:s').'请求平台为空。'.PHP_EOL;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
$geoService = new GeoService();
|
|
|
|
$keywordArr = $info['keywords'] ?? [];
|
|
|
|
$urlArr = $info['url'] ?? [];
|
|
|
|
$platforms = GeoPlatform::where(['status' => GeoPlatform::STATUS_ON])->get();
|
|
|
|
if ($platforms->isEmpty) {
|
|
|
|
$this->output('未设置AI模型!');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$geo_service = new GeoService();
|
|
|
|
$geoResultModel = new GeoQuestionResult();
|
|
|
|
foreach ($questionArr as $q_item){
|
|
|
|
foreach ($platformArr as $p_item){
|
|
|
|
$keywords = [];//命中的关键词
|
|
|
|
$urls = [];//命中的网址
|
|
|
|
$geoLogModel = new GeoQuestionLog();
|
|
|
|
foreach ($task->question as $question) {
|
|
|
|
$error_num = 0;
|
|
|
|
foreach ($platforms as $platform) {
|
|
|
|
// 设置重试, 有的平台不一定能正常获取到数据
|
|
|
|
GET_RESULT:
|
|
|
|
$error_num++;
|
|
|
|
try {
|
|
|
|
$result_data = $geoService->setWebSearchChatAction($q_item,$p_item);
|
|
|
|
echo 'success:'.$result_data['code'].PHP_EOL;
|
|
|
|
if(isset($result_data) && $result_data['code'] == 200){
|
|
|
|
$result_data_annotations[] = $result_data['text'];
|
|
|
|
if(!empty($result_data['annotations'])){
|
|
|
|
$url = array_column(array_column($result_data['annotations'], 'url_citation'), 'url');
|
|
|
|
$title = array_column(array_column($result_data['annotations'], 'url_citation'), 'title');;
|
|
|
|
$result_data_annotations = array_merge($url,$title,$result_data_annotations);
|
|
|
|
}
|
|
|
|
$keywords = $this->getKeywords($keywordArr,$result_data_annotations ?? []);
|
|
|
|
$urls = $this->getUrl($urlArr,$result_data_annotations ?? []);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}catch (\Exception $e){
|
|
|
|
echo $e->getMessage().PHP_EOL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//查询当前是否已有执行保存记录
|
|
|
|
$resultInfo = $geoResultModel->read(['project_id'=>$info['project_id'],'question_id'=>$info['id'],'platform'=>$p_item,'question'=>$q_item],['id']);
|
|
|
|
//保存一条结果记录
|
|
|
|
$data = [
|
|
|
|
'project_id'=>$info['project_id'],
|
|
|
|
'question_id'=>$info['id'],
|
|
|
|
'platform'=>$p_item,
|
|
|
|
'question'=>$q_item,
|
|
|
|
'keywords'=>json_encode($keywords ?? [],true),//命中的关键词
|
|
|
|
'text'=>json_encode($result_data ?? [],true),
|
|
|
|
'url'=>json_encode($urls ?? [],true),//命中的网址
|
|
|
|
'type'=>$info['type'] ?? 1
|
|
|
|
// 命中文案
|
|
|
|
$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($resultInfo === false){
|
|
|
|
$geoResultModel->addReturnId($data);
|
|
|
|
if(empty($geo_result)){
|
|
|
|
$geoResultModel->addReturnId($save_data);
|
|
|
|
}else{
|
|
|
|
$geoResultModel->edit($data,['id'=>$resultInfo['id']]);
|
|
|
|
$geoResultModel->edit($save_data, ['id' => $geo_result->id]);
|
|
|
|
}
|
|
|
|
//日志记录
|
|
|
|
$geoLogModel = new GeoQuestionLog();
|
|
|
|
$save_data['text'] = json_encode(FALSE == empty($data) ? $data : $result,true);
|
|
|
|
$geoLogModel->addReturnId($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//更新下次执行时间
|
|
|
|
$questionModel->edit(['current_time'=>date('Y-m-d'),'next_time'=>date('Y-m-d', strtotime(date('Y-m-d') . ' +'.(int)$projectInfo['geo_frequency'].' days'))],['id'=>$info['id']]);
|
|
|
|
$task->current_time = date('Y-m-d');
|
|
|
|
$task->next_time = date('Y-m-d', strtotime('+' . $project->geo_frequency . ' days'));
|
|
|
|
$task->save();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :获取命中的url
|
|
|
|
* @name :getUrl
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2025/7/3 16:38
|
|
|
|
* 整合Google平台数据
|
|
|
|
* @param $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
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 array_values(array_unique($url));
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :获取命中的关键词
|
|
|
|
* @name :getKeywords
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2025/7/3 16:26
|
|
|
|
* 获取待执行任务ID
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
public function getTaskId(){
|
|
|
|
$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($key);
|
|
|
|
}
|
|
|
|
return $keywords;
|
|
|
|
return $task_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :拉取任务id
|
|
|
|
* @name :getTaskId
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2025/7/3 15:15
|
|
|
|
* 输出日志
|
|
|
|
* @param $message
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$task_id = Redis::rpop('geo_question_result');
|
|
|
|
}
|
|
|
|
return $task_id;
|
|
|
|
public function output($message)
|
|
|
|
{
|
|
|
|
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|