|
...
|
...
|
@@ -34,87 +34,127 @@ 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)){
|
|
|
|
echo '无数据'.PHP_EOL;
|
|
|
|
sleep(30);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
$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]);
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
$questionArr = $info['question'];
|
|
|
|
if(empty($questionArr)){
|
|
|
|
echo date('Y-m-d H:i:s').'当前任务不存在问题。'.PHP_EOL;
|
|
|
|
$questionModel->edit(['status'=>0],['id'=>$task_id]);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
//获取平台信息
|
|
|
|
$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;
|
|
|
|
$geoPlatformModel = new GeoPlatform();
|
|
|
|
$platformsArr = $geoPlatformModel->selectField(['status' => GeoPlatform::STATUS_ON],'en_name');
|
|
|
|
if (empty($platformsArr)) {
|
|
|
|
$this->output('未设置AI模型!');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$geoService = new GeoService();
|
|
|
|
$keywordArr = $info['keywords'] ?? [];
|
|
|
|
$urlArr = $info['url'] ?? [];
|
|
|
|
$geo_service = new GeoService();
|
|
|
|
$geoResultModel = new GeoQuestionResult();
|
|
|
|
foreach ($questionArr as $q_item){
|
|
|
|
foreach ($platformArr as $p_item){
|
|
|
|
$keywords = [];//命中的关键词
|
|
|
|
$urls = [];//命中的网址
|
|
|
|
$geoLogModel = new GeoQuestionLog();
|
|
|
|
foreach ($taskInfo['question'] as $question) {
|
|
|
|
foreach ($platformsArr as $platform) {
|
|
|
|
$data = $hit_data = [];
|
|
|
|
$error_num = 0;
|
|
|
|
// 设置重试, 有的平台不一定能正常获取到数据
|
|
|
|
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){
|
|
|
|
$keywords = $this->getKeywords($keywordArr,$result_data['text'] ?? []);
|
|
|
|
$urls = $this->getUrl($urlArr,$result_data['annotations'] ?? []);
|
|
|
|
}
|
|
|
|
}catch (\Exception $e){
|
|
|
|
echo $e->getMessage().PHP_EOL;
|
|
|
|
echo '执行次数:'.$error_num.PHP_EOL;
|
|
|
|
if ($error_num >= 5) {
|
|
|
|
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
|
|
|
|
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: ' . $platform . ', 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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo 'MZ-url'.json_encode($hit_url).PHP_EOL;
|
|
|
|
// 保存数据结果
|
|
|
|
$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,
|
|
|
|
'created_at'=>date('Y-m-d H:i:s'),
|
|
|
|
'updated_at'=>date('Y-m-d H:i:s'),
|
|
|
|
];
|
|
|
|
if($resultInfo === false){
|
|
|
|
$geoResultModel->addReturnId($data);
|
|
|
|
// echo '当前数据INFO:'.json_encode($save_data,true).PHP_EOL;
|
|
|
|
if($geo_result === false){
|
|
|
|
$id= $geoResultModel->insertGetId($save_data);
|
|
|
|
echo '当前数据id:'.$id.PHP_EOL;
|
|
|
|
}else{
|
|
|
|
$geoResultModel->edit($data,['id'=>$resultInfo['id']]);
|
|
|
|
}
|
|
|
|
$data_log = [
|
|
|
|
'project_id'=>$info['project_id'],
|
|
|
|
'question_id'=>$info['id'],
|
|
|
|
'platform'=>$p_item,
|
|
|
|
'question'=>$q_item,
|
|
|
|
'text'=>json_encode($result_data ?? [],true),
|
|
|
|
'type'=>$info['type'] ?? 1
|
|
|
|
];
|
|
|
|
$geoLogModel = new GeoQuestionLog();
|
|
|
|
$geoLogModel->addReturnId($data_log);
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
//更新下次执行时间
|
|
|
|
$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']]);
|
|
|
|
}
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -124,25 +164,16 @@ class GeoQuestionRes extends Command |
|
|
|
* @method :post
|
|
|
|
* @time :2025/7/3 16:38
|
|
|
|
*/
|
|
|
|
public function getUrl($urlArr = [],$result_annotations = [],$result_text = []){
|
|
|
|
public function getUrl($urlArr = [],$result_annotations = []){
|
|
|
|
$url = [];
|
|
|
|
if(!empty($urlArr)){
|
|
|
|
$str = implode(',',$result_annotations);
|
|
|
|
foreach ($urlArr as $u_item){
|
|
|
|
if(!empty($result_text)){
|
|
|
|
if (str_contains($result_text, $u_item)) {
|
|
|
|
$url[] = $u_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!empty($result_annotations)){
|
|
|
|
foreach ($result_annotations as $a_item){
|
|
|
|
echo 'url'.$a_item['url_citation']['url'].PHP_EOL.'当前的url:'.$u_item;
|
|
|
|
if (str_contains($a_item['url_citation']['url'], $u_item)) {
|
|
|
|
if (str_contains($str, $u_item)) {
|
|
|
|
$url[] = $u_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_values(array_unique($url));
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -156,8 +187,9 @@ class GeoQuestionRes extends Command |
|
|
|
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($result_text, $k_item)) {
|
|
|
|
if (str_contains($str, $k_item)) {
|
|
|
|
$keywords[] = $k_item;
|
|
|
|
}
|
|
|
|
}
|
|
...
|
...
|
@@ -166,24 +198,94 @@ class GeoQuestionRes extends Command |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :拉取任务id
|
|
|
|
* @name :getTaskId
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2025/7/3 15:15
|
|
|
|
* 整合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){
|
|
|
|
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['title']) && !empty($link['title']) && 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(){
|
|
|
|
$task_id = Redis::rpop('geo_question_result');
|
|
|
|
$key = 'geo_task_list';
|
|
|
|
$task_id = Redis::rpop($key);
|
|
|
|
if(empty($task_id)){
|
|
|
|
$questionModel = new GeoQuestion();
|
|
|
|
$ids = $questionModel->selectField(['status'=>1,'next_time'=>['<=',date('Y-m-d')]],'id');
|
|
|
|
$ids = $questionModel->selectField(['status'=>$questionModel::STATUS_OPEN,'next_time'=>['<=',date('Y-m-d')]],'id');
|
|
|
|
if(!empty($ids)){
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
Redis::lpush('geo_question_result', $id);
|
|
|
|
Redis::lpush($key, $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;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|