_request = Suco_Application::instance()->getRequest(); $this->_response = Suco_Application::instance()->getResponse(); } // public function doAutorun() // { // echo '
'; // $this->doGoogleRank(); // // $this->doAllintitle(); // echo ''; // echo ''; // } public function doAllintitle() { try { $keywords = M('Project_Keyword')->alias('pk') ->leftJoin(M('Project')->getTableName().' AS p', 'p.id = pk.project_id') ->columns('pk.*, p.domain') ->where('pk.allintitle = -1') ->where('p.allow_googlerank = 1') ->order('sync_time ASC, id ASC') ->paginator(1, 1) ->fetchRows(); $ct = M('Project_Keyword')->alias('pk') ->leftJoin(M('Project')->getTableName().' AS p', 'p.id = pk.project_id') ->where('p.allow_googlerank = 1') ->columns(' COUNT(*) AS total, SUM(CASE WHEN allintitle != -1 THEN 1 ELSE 0 END) as complete ')->fetchRow(); } catch (Suco_Exception $e) { echo $e->getMessage(); } echo date('Y/m/d H:i:s') . ' AllInTitle ['.$ct['complete'].'/'.$ct['total'].'] - '; if (!count($keywords)) { // sleep(5); echo ("The Queue is empty. \r\n"); return; } foreach($keywords as $keyword) { try { $g = M('Project_Keyword')->getAllInTitle($keyword['keyword']); echo $keyword['keyword']. ' '.$g.' result(s)'; $keyword->allintitle = $g; $keyword->save(); } catch (Suco_Exception $e) { echo $e->getMessage(); } echo "\r\n"; } } public function doGoogleRank() { $sleep = 10000; $tomorrow = strtotime(date('Y/m/d', strtotime('+1 days'))); try { $keywords = M('Project_Keyword')->alias('pk') ->leftJoin(M('Project')->getTableName().' AS p', 'p.id = pk.project_id') ->columns('pk.*, p.domain') ->where('pk.sync_time < ? AND p.allow_googlerank = 1', $tomorrow) ->order('pk.sync_time ASC, pk.id ASC') ->paginator(1, 1) ->fetchRows(); $ct = M('Project_Keyword')->alias('pk') ->leftJoin(M('Project')->getTableName().' AS p', 'p.id = pk.project_id') ->where('p.allow_googlerank = 1') ->columns(' COUNT(*) AS total, SUM(CASE WHEN pk.sync_time >= '.$tomorrow.' THEN 1 ELSE 0 END) as complete ')->fetchRow(); } catch (Suco_Exception $e) { echo $e->getMessage(); } echo date('Y/m/d H:i:s') . ' GoogleRank ['.$ct['complete'].'/'.$ct['total'].'] - '; if (!count($keywords)) { // sleep(300); echo ("The Queue is empty. \r\n"); return; } foreach($keywords as $row) { try { if (!$row->project->google_domain) { throw new App_Exception('Google地区设置错误'); } // if (($row['google_rank'] <= 10 && strtotime('+3 days', $row['update_time']) >= time()) // || ($row['google_rank'] <= 20 && strtotime('+2 days', $row['update_time']) >= time()) // || ($row['google_rank'] <= 50 && strtotime('+1 days', $row['update_time']) >= time()) // || $row['google_rank'] >= 9999 // || !$row['update_time']) { $g = M('Project_Keyword')->getGoogleRank($row['domain'], $row['keyword'], 1, $row->project->google_domain); $g['keyword'] = $row['keyword']; echo http_build_query($g); $row->resules = $g['results']; $row->update_time = time(); $row->sync_time = strtotime('+1 days'); $row->google_rank = $g['rank']; $row->target_url = $g['url']; $row->save(); // } else { // echo '跳过规则'; // } // if ($g['rank'] <= 10) { // $sync = strtotime('+3 days'); // } elseif ($g['rank'] <= 20) { // $sync = strtotime('+2 days'); // } elseif ($g['rank'] <= 50) { // $sync = strtotime('+1 days'); // } else { // $sync = strtotime('+7 days'); // } M('Project_Keyword_History')->insert(array( 'keyword_id' => $row->id, 'project_id' => $row['project_id'], 'google_rank' => $row->google_rank, 'resules' => $row->resules, 'top10_url' => $g['top10_url'], 'create_time' => time() )); if ($ct['complete']%10==0) { //每查50次重启一下路由 $sleep = 1000*60*1.5; // M('Project_Keyword')->restartRouter(); echo " ==== [Restart the router]"; // sleep(120); } if ($row->project->exists()) { $row->project->sync_time = time(); $ct = M('Project_Keyword')->select(' SUM(CASE WHEN google_rank <= 1 THEN 1 ELSE 0 END) as top1, SUM(CASE WHEN google_rank <= 1 THEN search_num ELSE 0 END) as top1_search, SUM(CASE WHEN google_rank <= 3 THEN 1 ELSE 0 END) as top3, SUM(CASE WHEN google_rank <= 3 THEN search_num ELSE 0 END) as top3_search, SUM(CASE WHEN google_rank <= 10 THEN 1 ELSE 0 END) as top10, SUM(CASE WHEN google_rank <= 10 THEN search_num ELSE 0 END) as top10_search, SUM(CASE WHEN google_rank <= 20 THEN 1 ELSE 0 END) as top20, SUM(CASE WHEN google_rank <= 20 THEN search_num ELSE 0 END) as top20_search, SUM(CASE WHEN google_rank <= 30 THEN 1 ELSE 0 END) as top30, SUM(CASE WHEN google_rank <= 30 THEN search_num ELSE 0 END) as top30_search, SUM(CASE WHEN google_rank <= 50 THEN 1 ELSE 0 END) as top50, SUM(CASE WHEN google_rank <= 50 THEN search_num ELSE 0 END) as top50_search ') ->where('google_rank != 9999 AND project_id = ?', (int)$row->project->id) ->fetchRow(); $history = M('Project_History')->select() ->where('project_id = ? AND create_time >= ?', array($row->project->id, strtotime('today'))) ->fetchRow(); $data = $ct->toArray(); $data['project_id'] = $row->project->id; if (!$history->exists()) { M('Project_History')->insert($data); } else { M('Project_History')->updateById($data, $history->id); } if ($row->project->reach_num > 0 && !$row->project->reach_time && $ct['top10'] >= $row->project->reach_num) { $row->reach_time = time(); } $row->project->save(); // echo Suco_Db::dump(); } } catch (Suco_Exception $e) { echo $e->getMessage(); } echo "\r\n"; echo ""; } } public function doEmail() { $projects = M('Project')->select() ->where('is_enabled = 1') ->fetchRows(); $spider = new Suco_Spider(); $context = stream_context_create(array( 'http' => array( 'timeout' => 1000 ) )); echo "
"; echo date("Y/m/d H:i:s"); echo "\r\n"; foreach($projects as $project) { if (!$project['api_gateway']) { continue; } $url = $project['api_gateway'].'queue/mail'; try { echo $url .' - '; $ret = $spider->connect($url); if ($ret) { echo $ret; } } catch (Suco_Exception $e) { echo $e->getMessage(); } echo "\r\n"; // sleep(1); } // echo ''; } public function doReport() { $projects = M('Project')->select() ->where('is_enabled = 1') ->fetchRows(); $spider = new Suco_Spider(); $context = stream_context_create(array( 'http' => array( 'timeout' => 1000 ) )); echo ""; echo date("Y/m/d H:i:s"); echo "\r\n"; foreach($projects as $project) { if (!$project['api_gateway']) { continue; } $url = $project['api_gateway'].'queue/report'; try { echo $url .' - '; $ret = $spider->connect($url); if ($ret) { echo $ret; } } catch (Suco_Exception $e) { echo $e->getMessage(); } echo "\r\n"; // sleep(1); } // echo ''; } } if ($_GET['act']) { $action = $_GET['act']; $control = new Queue(); $control->dispatch($action); }