RankData.php
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace App\Console\Commands\RankData;
use App\Helper\Arr;
use App\Helper\QuanqiusouApi;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\RankData\RankData as GoogleRankModel;
/**
* Class GoogleRank
* @package App\Console\Commands
* @author zbj
* @date 2023/5/6
*/
class RankData extends BaseCommands
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'rank_data';
/**
* The console command description.
*
* @var string
*/
protected $description = '谷歌排名数据';
/**
* @author zbj
* @date 2023/5/6
*/
public function do()
{
$error = 0;
$api = new QuanqiusouApi();
//有排名api编号的项目
$list = DeployOptimize::where('api_no', '>' , 0)->select('api_no','minor_languages','project_id')->get();
//当日所有站点谷歌收录数据
$site_res = $api->getSiteRes();
if(!$site_res){
return false;
}
foreach ($list as $item){
$model = GoogleRankModel::where('project_id', $item['project_id'])->where('lang', '')->first();
if (!$model || $model->updated_date != date('Y-m-d')) {
$res = $api->getGoogleRank($item['api_no']);
if(!$res){
$error++;
continue;
}
//收录数
$indexed_pages_num = $site_res[$item['api_no']];
$this->save_rank($item['project_id'], $res, $indexed_pages_num);
}
//有小语种的
$lang_list = $api->getLangList();
if(!empty($lang_list[$item['api_no']])){
$model = GoogleRankModel::where('project_id', $item['project_id'])->where('lang', '<>', '')->first();
if (!$model || $model->updated_date != date('Y-m-d')) {
$res = $api->getGoogleRank($item['api_no'], 1);
if(!$res){
$error++;
continue;
}
$data = [];
//不同的小语种取出来
foreach ($res as $keyword => $v){
$data[Arr::last($v)['lang']][$keyword] = $v;
}
foreach ($data as $lang => $rank){
$this->save_rank($item['project_id'], $rank, 0, $lang);
}
}
}
}
return !$error;
}
/**
* @param $project_id
* @param int $indexed_pages_num
* @param $data
* @param string $lang
* @author zbj
* @date 2023/5/8
*/
public function save_rank($project_id, $data, int $indexed_pages_num = 0, string $lang = ''){
$without_project_ids = []; //不用处理排名的项目
$first_num = $first_page_num = $first_three_pages_num = $first_five_pages_num = $first_ten_pages_num = 0;
if(!$lang){
foreach ($data as &$ranks){
ksort($ranks);
foreach ($ranks as &$rank){
//处理排名
if(!in_array($project_id, $without_project_ids)){
if($rank['position'] >= 10){
$rank['position'] -= 5;
}
//todo 需要特殊处理排名的项目
}
}
$last = Arr::last($ranks);
//第一名
if($last['position'] == 1){
$first_num ++;
}
//排名第一页
if($last['position'] > 0 && $last['position'] <= 10){
$first_page_num ++;
}
//排名前三页
if($last['position'] > 0 && $last['position'] <= 30){
$first_three_pages_num ++;
}
//排名前五页
if($last['position'] > 0 && $last['position'] <= 50){
$first_five_pages_num ++;
}
//排名前十页
if($last['position'] > 0 && $last['position'] <= 100){
$first_ten_pages_num ++;
}
}
}
$where = [
'project_id' => $project_id,
'lang' => $lang
];
$model = GoogleRankModel::where($where)->first();
if(!$model){
$model = new GoogleRankModel();
}
//关键词达标天数
if($model->updated_date != date('Y-m-d')){
//保证关键词数
$keyword_num = DeployBuild::where('project_id', $project_id)->value('keyword_num');
if($first_page_num >= $keyword_num){
$model->compliance_day = $model->compliance_day + 1;
}
}
$model->project_id = $project_id;
$model->first_num = $first_num;
$model->first_page_num = $first_page_num;
$model->first_three_pages_num = $first_three_pages_num;
$model->first_five_pages_num = $first_five_pages_num;
$model->first_ten_pages_num = $first_ten_pages_num;
$model->indexed_pages_num = $indexed_pages_num;
$model->lang = $lang;
$model->data = $data;
$model->updated_date = date('Y-m-d');
$model->save();
}
}