From f0fd78bbf91f8c5db84ecb940b1160adad2745a0 Mon Sep 17 00:00:00 2001 From: lyt Date: Wed, 8 Nov 2017 11:57:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E9=94=AE=E8=AF=8D=20=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=8E=92=E5=90=8D=E5=A2=9E=E5=8A=A0=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=A7=BB=E5=8A=A8=E7=AB=AF=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/controllers/keyworlds.php | 114 ++++++++++++++++---------- 1 file changed, 69 insertions(+), 45 deletions(-) diff --git a/application/controllers/keyworlds.php b/application/controllers/keyworlds.php index 695d57d5..61769f65 100644 --- a/application/controllers/keyworlds.php +++ b/application/controllers/keyworlds.php @@ -283,6 +283,10 @@ class Keyworlds extends CI_Controller { $source_flag = 'source_m'; } + $today_0 = strtotime(date('Ymd000000')); + $today_24 = strtotime(date('Ymd235959')); + $last_rank_today = $this->Keywordsrank_model->get_rank_list($kw_id,1,$today_0,$today_24); + $orignal = array(); //获取排名详细情况,排名超过150的停止搜索 for ($i = 1; $i <= 15; $i++) { @@ -291,11 +295,11 @@ class Keyworlds extends CI_Controller { $orignal[] = $rank['orignal']; // 仅更新一个字段 if ($client_flag === 'm') { - $rank['rank'] = $last_rank[0]->kwr_rank; - $rank['source'] = $last_rank[0]->kwr_source; + $rank['rank'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_rank : null; + $rank['source'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_source : null; } else { - $rank['rank_m'] = null; - $rank['source_m'] = null; + $rank['rank_m'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_mobile_rank : null; + $rank['source_m'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_mobile_source : null; } if (intval($rank[$rank_flag]) != 0) { if ($rank[$rank_flag] > 150) @@ -308,43 +312,55 @@ class Keyworlds extends CI_Controller { break; } } + $data[] = array( + 'name' => 'rank', + 'value' => ($rank['rank'] . " / " . $rank["rank_m"]), + 'real' => $rank_flag . " : " . $rank[$rank_flag] + // ,'tmp' => intval($last_rank[0]->kwr_datetime) . "-" . intval($today) . $client_flag + // ,'orignal' => $orignal + // ,"site" => $site_url + ); + if ($rank[$rank_flag] < 0) { + echo json_encode($data); + return false; + } //保存排名数据 - if (isset($last_rank[0]->kwr_datetime) and $last_rank[0]->kwr_datetime > strtotime(date('Y-m-d'))) { - if ($rank[$rank_flag] != -1 and $rank[$rank_flag] != -2 and $this->Keywordsrank_model->update_rank($last_rank[0]->kwr_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'])) { - $this->Keywords_model->update_time($kw_id); - } + if ( isset($last_rank_today[0]) && ! empty($last_rank_today[0])) { + $this->Keywordsrank_model->update_rank($last_rank_today[0]->kwr_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']); + $this->Keywords_model->update_time($kw_id); } else { - if ($rank[$rank_flag] != -1 and $rank[$rank_flag] != -2 and $this->Keywordsrank_model->add_rank($kw_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines'])) { + $add = $this->Keywordsrank_model->add_rank($kw_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']); + // rank 变化 + if ($add && $client_flag === 'PC') { + $this->load->model('InfoSMS_model'); + $last_rank_pc = isset($last_rank_pc[0]) ? $last_rank_pc[0]->kwr_rank : $rank['rank']; + $this->InfoSMS_model->readed_for_info($kw_id, 'keywords_rank'); + $this->InfoSMS_model->add('keywords_rank', $kw_id, $rank['rank'], $last_rank_pc, (string) ($rank['rank'] - $last_rank_pc)); $this->Keywords_model->update_time($kw_id); } } - $data[] = array( - 'name' => 'rank', - 'value' => ($rank['rank'] . " / " . $rank["rank_m"]), - 'real' => $rank_flag . " : " . $rank[$rank_flag], - 'tmp' => intval($last_rank[0]->kwr_datetime) . "-" . intval($today) . $client_flag, - // 'orignal' => $orignal, - "site" => $site_url - ); echo json_encode($data); return TRUE; } //自动爬取排名 - public function get_auto_rank($engines = '', $fix_site_code = '') { + public function get_auto_rank($engines = '', $fix_site_code = '', $client='PC') { set_time_limit(60); - $rank = array(); - $orignal = array(); + $rank = array(); + $orignal = array(); + $worldsList = array(); $site_config = $this->config->item('site'); - $engines = trim($engines); + $engines = trim($engines); //取出需要更新排名的关键词 // 先取时间最久没更新的 - $worldsList = $this->Keywords_model->get_keyworld_list_old(1, $engines, $fix_site_code); $today = strtotime(date('Y-m-d')); - $client_flag = "PC"; + $client_flag = $client ? $client : "PC"; $rank_flag = 'rank'; $source_flag = 'source'; - if (empty($worldsList)) { + if ($client_flag == 'PC') { + $worldsList = $this->Keywords_model->get_keyworld_list_old(1, $engines, $fix_site_code); + } + if (empty($worldsList) || $client_flag === 'm') { $worldsList = $this->Keywords_model->get_keyworld_list(1, $engines, $fix_site_code); //如果没有需要更新的则返回 if (empty($worldsList)) { @@ -357,6 +373,11 @@ class Keyworlds extends CI_Controller { $last_rank = $this->Keywordsrank_model->get_rank_list($worldsList[0]->kw_id); } $last_rank_pc = $this->Keywordsrank_model->get_rank_list($worldsList[0]->kw_id); + + $today_0 = strtotime(date('Ymd000000')); + $today_24 = strtotime(date('Ymd235959')); + $last_rank_today = $this->Keywordsrank_model->get_rank_list($worldsList[0]->kw_id,1,$today_0,$today_24); + //获取排名并保存 foreach ($worldsList as $v) { $site_config = $site_config[$v->kw_sitecode]; @@ -371,11 +392,11 @@ class Keyworlds extends CI_Controller { } $orignal[] = $rank['orignal']; if ($client_flag === 'm') { - $rank['rank'] = $last_rank[0]->kwr_rank; - $rank['source'] = $last_rank[0]->kwr_source; + $rank['rank'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_rank : null; + $rank['source'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_source : null; } else { - $rank['rank_m'] = null; - $rank['source_m'] = null; + $rank['rank_m'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_mobile_rank : null; + $rank['source_m'] = isset($last_rank_today[0]) ? $last_rank_today[0]->kwr_mobile_source : null; } if ($rank[$rank_flag] != 0) { if (intval($rank[$rank_flag]) > 150) @@ -388,23 +409,24 @@ class Keyworlds extends CI_Controller { break; } } - if ($rank[$rank_flag] != -1 - and $rank[$rank_flag] != -2 - and $client_flag === 'PC' - and $this->Keywordsrank_model->add_rank($v->kw_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']) - ) { - $this->load->model('InfoSMS_model'); - $last_rank_pc = isset($last_rank_pc[0]) ? $last_rank_pc[0]->kwr_rank : $rank['rank']; - $this->InfoSMS_model->readed_for_info($v->kw_id, 'keywords_rank'); - $this->InfoSMS_model->add('keywords_rank', $v->kw_id, $rank['rank'], $last_rank_pc, (string) ($rank['rank'] - $last_rank_pc)); - if ($engines != 'yandex') { - $this->Keywords_model->update_time($v->kw_id); - } - } else if ($rank[$rank_flag] != -1 - and $rank[$rank_flag] != -2 - and $client_flag === 'm') { - $this->Keywordsrank_model->update_rank($last_rank[0]->kwr_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']); + if ( $rank[$rank_flag] < 0 ) { + break; + } + if ( isset($last_rank_today[0]) && ! empty($last_rank_today[0])) { + $this->Keywordsrank_model->update_rank($last_rank_today[0]->kwr_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']); $this->Keywords_model->update_time($v->kw_id); + } else { + $add = $this->Keywordsrank_model->add_rank($v->kw_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines']); + // rank 变化 + if ($add && $client_flag === 'PC') { + $this->load->model('InfoSMS_model'); + $last_rank_pc = isset($last_rank_pc[0]) ? $last_rank_pc[0]->kwr_rank : $rank['rank']; + $this->InfoSMS_model->readed_for_info($v->kw_id, 'keywords_rank'); + $this->InfoSMS_model->add('keywords_rank', $v->kw_id, $rank['rank'], $last_rank_pc, (string) ($rank['rank'] - $last_rank_pc)); + if ($engines != 'yandex') { + $this->Keywords_model->update_time($v->kw_id); + } + } } } var_dump("rank $client_flag " . $rank[$rank_flag]); @@ -459,12 +481,12 @@ class Keyworlds extends CI_Controller { $source_flag = 'source_m'; } $response = $this->curl($url,$client_flag); - $rank["orignal"] = $response; if ($response) { //分析google搜索结果,计算排名 if (false !== stripos($response, '

302 Moved

') || false !== stripos($response, 'recaptcha')) { $rank['rank'] = -2; + $rank["orignal"] = $response; } elseif (false !== stripos($response, $site_url)) { preg_match_all($match_fun, $response, $store); foreach ($store[1] as $k => $v) { @@ -482,9 +504,11 @@ class Keyworlds extends CI_Controller { } } } else { + $rank["orignal"] = $response; // log_message('error','google search response not found CH'); } } else { + $rank["orignal"] = $response; $rank[$rank_flag] = -1; } return $rank;