作者 zhl

geo 脚本

@@ -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 }
@@ -25,6 +25,12 @@ class GeoQuestion extends Base @@ -25,6 +25,12 @@ class GeoQuestion extends Base
25 25
26 public $frequency = [1,2,3,4,5,6,7,8,9,10];//类型 26 public $frequency = [1,2,3,4,5,6,7,8,9,10];//类型
27 27
  28 + const TYPE_BRAND = 1;
  29 + const TYPE_MARKET = 2;
  30 +
  31 + const STATUS_CLOSE = 0;
  32 + const STATUS_OPEN = 1;
  33 +
28 /** 34 /**
29 * @remark :geo提交网址获取器 35 * @remark :geo提交网址获取器
30 * @name :getUrlAttribute 36 * @name :getUrlAttribute
@@ -71,16 +77,13 @@ class GeoQuestion extends Base @@ -71,16 +77,13 @@ class GeoQuestion extends Base
71 } 77 }
72 78
73 /** 79 /**
74 - * @remark :品牌类型  
75 - * @name :brandType  
76 - * @author :lyh  
77 - * @method :post  
78 - * @time :2025/7/3 9:43 80 + * 品牌类型
  81 + * @return array
79 */ 82 */
80 public function brandType(){ 83 public function brandType(){
81 return [ 84 return [
82 - 1=>'品牌数据',  
83 - 2=>'营销数据' 85 + self::TYPE_BRAND => '品牌数据',
  86 + self::TYPE_MARKET => '营销数据'
84 ]; 87 ];
85 } 88 }
86 } 89 }
@@ -14,16 +14,16 @@ class GeoService @@ -14,16 +14,16 @@ class GeoService
14 public $api_key = '7yn!We6$&NnVA38bpGy*A@4TQ5iYLJcW'; 14 public $api_key = '7yn!We6$&NnVA38bpGy*A@4TQ5iYLJcW';
15 15
16 public $api_url = 'https://api.cmer.com/'; 16 public $api_url = 'https://api.cmer.com/';
  17 +
17 /** 18 /**
18 - * @remark :请求的方法  
19 - * @name :requestAction  
20 - * @author :lyh  
21 - * @method :post  
22 - * @time :2025/7/3 14:26 19 + * 获取AI平台数据
  20 + * @param $content
  21 + * @param $platform
  22 + * @return mixed|string
23 */ 23 */
24 - public function setWebSearchChatAction($content,$platform){  
25 - $route = 'v1/websearch_chat';  
26 - $url = $this->api_url.$route; 24 + public function getAiPlatformResult($content,$platform)
  25 + {
  26 + $url = $this->api_url . 'v1/websearch_chat';
27 $header = [ 27 $header = [
28 'accept: application/json', 28 'accept: application/json',
29 'X-CmerApi-Host: llm-chat.p.cmer.com', 29 'X-CmerApi-Host: llm-chat.p.cmer.com',
@@ -37,10 +37,33 @@ class GeoService @@ -37,10 +37,33 @@ class GeoService
37 'role'=>'user' 37 'role'=>'user'
38 ], 38 ],
39 ], 39 ],
40 - 'platform'=>$platform,  
41 - 'security_check'=>true 40 + 'platform' => $platform,
  41 + 'security_check' => true
42 ]; 42 ];
43 $data = http_post($url,json_encode($message,true),$header); 43 $data = http_post($url,json_encode($message,true),$header);
44 return $data; 44 return $data;
45 } 45 }
  46 +
  47 + /**
  48 + * 获取Google数据
  49 + * @param $search
  50 + * @param int $lum_json 默认1 不只是什么参数
  51 + * @return mixed|string
  52 + */
  53 + public function getGooglePlatformResult($search, $lum_json = 1)
  54 + {
  55 + $url = 'http://43.153.56.18:58000/google_ai_summary';
  56 + $header = [
  57 + 'accept: application/json',
  58 + 'x-api-key: 6EipgPsy3Q7Q9M2jCx',
  59 + 'Content-Type: application/json'
  60 + ];
  61 + $param = [
  62 + 'q' => $search,
  63 + 'lum_json' => $lum_json
  64 + ];
  65 +
  66 + $url = $url . '?' . http_build_query($param);
  67 + return http_get($url, $header);
  68 + }
46 } 69 }