作者 zhl

geo 脚本

... ... @@ -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;
}
}
... ...
... ... @@ -25,6 +25,12 @@ class GeoQuestion extends Base
public $frequency = [1,2,3,4,5,6,7,8,9,10];//类型
const TYPE_BRAND = 1;
const TYPE_MARKET = 2;
const STATUS_CLOSE = 0;
const STATUS_OPEN = 1;
/**
* @remark :geo提交网址获取器
* @name :getUrlAttribute
... ... @@ -71,16 +77,13 @@ class GeoQuestion extends Base
}
/**
* @remark :品牌类型
* @name :brandType
* @author :lyh
* @method :post
* @time :2025/7/3 9:43
* 品牌类型
* @return array
*/
public function brandType(){
return [
1=>'品牌数据',
2=>'营销数据'
self::TYPE_BRAND => '品牌数据',
self::TYPE_MARKET => '营销数据'
];
}
}
... ...
... ... @@ -14,16 +14,16 @@ class GeoService
public $api_key = '7yn!We6$&NnVA38bpGy*A@4TQ5iYLJcW';
public $api_url = 'https://api.cmer.com/';
/**
* @remark :请求的方法
* @name :requestAction
* @author :lyh
* @method :post
* @time :2025/7/3 14:26
* 获取AI平台数据
* @param $content
* @param $platform
* @return mixed|string
*/
public function setWebSearchChatAction($content,$platform){
$route = 'v1/websearch_chat';
$url = $this->api_url.$route;
public function getAiPlatformResult($content,$platform)
{
$url = $this->api_url . 'v1/websearch_chat';
$header = [
'accept: application/json',
'X-CmerApi-Host: llm-chat.p.cmer.com',
... ... @@ -37,10 +37,33 @@ class GeoService
'role'=>'user'
],
],
'platform'=>$platform,
'security_check'=>true
'platform' => $platform,
'security_check' => true
];
$data = http_post($url,json_encode($message,true),$header);
return $data;
}
/**
* 获取Google数据
* @param $search
* @param int $lum_json 默认1 不只是什么参数
* @return mixed|string
*/
public function getGooglePlatformResult($search, $lum_json = 1)
{
$url = 'http://43.153.56.18:58000/google_ai_summary';
$header = [
'accept: application/json',
'x-api-key: 6EipgPsy3Q7Q9M2jCx',
'Content-Type: application/json'
];
$param = [
'q' => $search,
'lum_json' => $lum_json
];
$url = $url . '?' . http_build_query($param);
return http_get($url, $header);
}
}
... ...