123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- require_once 'runtime.php';
- require_once 'func.php';
- ini_set('max_execution_time', '0');
- class Queue extends Suco_Controller_Action
- {
- public function init()
- {
- $this->_request = Suco_Application::instance()->getRequest();
- $this->_response = Suco_Application::instance()->getResponse();
- }
- // public function doAutorun()
- // {
- // echo '<pre>';
- // $this->doGoogleRank();
- // // $this->doAllintitle();
- // echo '</pre>';
- // echo '<script>window.location = \'queue.php?act=autorun&time='.(time()).'\'</script>';
- // }
- 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 "<script>
- var interval = setInterval(function(){
- window.location.reload()
- // console.log('here');
- clearInterval(interval);
- }, {$sleep});
- </script>";
- }
- }
- 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 "<pre>";
- 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 '<script>window.location = \'/crontab/autorun.php?n='.time().'\';</script>';
- }
- 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 "<pre>";
- 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 '<script>window.location = \'/crontab/autorun.php?n='.time().'\';</script>';
- }
- }
- if ($_GET['act']) {
- $action = $_GET['act'];
- $control = new Queue();
- $control->dispatch($action);
- }
|