作者 lyh

GXgeo设置

... ... @@ -46,50 +46,54 @@ class GeoQuestionRes extends Command
sleep(300);
continue;
}
$task = GeoQuestion::where(['id' => $task_id, 'status' => GeoQuestion::STATUS_OPEN])->where('next_time', '<=', date('Y-m-d'))->first();
if (empty($task)) {
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;
}
$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();
$projectModel = new Project();
$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;
}
if ((empty($task->question) || FALSE == is_array($task->question)) || (empty($task->keywords) && empty($task->url))) {
if(empty($taskInfo['question']) || empty($taskInfo['keywords']) || empty($taskInfo['url'])){
$this->output('task id: ' . $task_id . ', error: 任务数据缺失, continue!');
$task->status = GeoQuestion::STATUS_CLOSE;
$task->save();
$geoQuestionModel->edit(['status'=>$geoQuestionModel::STATUS_CLOSE],['id'=>$task_id]);
continue;
}
$platforms = GeoPlatform::where(['status' => GeoPlatform::STATUS_ON])->get();
if ($platforms->isEmpty) {
$geoPlatformModel = new GeoPlatform();
$platformsArr = $geoPlatformModel->selectField(['status' => GeoPlatform::STATUS_ON],'en_name');
if (empty($platformsArr)) {
$this->output('未设置AI模型!');
continue;
}
$geo_service = new GeoService();
$geoResultModel = new GeoQuestionResult();
$geoLogModel = new GeoQuestionLog();
foreach ($task->question as $question) {
foreach ($taskInfo['question'] as $question) {
$error_num = 0;
foreach ($platforms as $platform) {
foreach ($platformsArr as $platform) {
// 设置重试, 有的平台不一定能正常获取到数据
GET_RESULT:
$error_num++;
try {
if ($platform->en_name == 'Google AI Overview') {
if ($platform == 'Google AI Overview') {
// overview 数据结构不确定, 需要单独处理数据
$data = $geo_service->getGooglePlatformResult($question);
$result = $this->dealGoogleData($data);
} else {
$result = $geo_service->getAiPlatformResult($question, $platform->en_name);
$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: ' . $question . ', error: ' . $e->getMessage());
if ($error_num < 5) {
if ($error_num < 2) {
goto GET_RESULT;
}
continue;
... ... @@ -105,25 +109,25 @@ class GeoQuestionRes extends Command
// 命中关键词和路由
$hit_keyword = $hit_url = [];
$hit = 0;
if ($task->keywords) {
$pattern = '/(' . implode('|', array_map('preg_quote', $task->keywords)) . ')/i';
if ($taskInfo['keywords']) {
$pattern = '/(' . implode('|', array_map('preg_quote', $taskInfo['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 ($taskInfo['url']) {
$pattern = '/(' . implode('|', array_map('preg_quote', $taskInfo['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();
$geo_result = $geoResultModel->read(['project_id' => $taskInfo['project_id'], 'question_id' => $task_id, 'platform' => $platform, 'question' => $question],['id']);
$save_data = [
'project_id' => $task->project_id,
'question_id' => $task->id,
'project_id' => $taskInfo['project_id'],
'question_id' => $task_id,
'type' => $task->type ?? GeoQuestion::TYPE_BRAND,
'platform' => $platform,
'question' => $question,
... ... @@ -132,7 +136,7 @@ class GeoQuestionRes extends Command
'text' => json_encode($result,true),
'hit' => $hit
];
if(empty($geo_result)){
if($geo_result === false){
$geoResultModel->addReturnId($save_data);
}else{
$geoResultModel->edit($save_data, ['id' => $geo_result->id]);
... ... @@ -141,9 +145,8 @@ class GeoQuestionRes extends Command
$geoLogModel->addReturnId($data);
}
}
$task->current_time = date('Y-m-d');
$task->next_time = date('Y-m-d', strtotime('+' . $project->geo_frequency . ' days'));
$task->save();
$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;
}
... ... @@ -208,7 +211,7 @@ class GeoQuestionRes extends Command
$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($key, $id);
... ...