queue.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. require_once 'runtime.php';
  3. require_once 'func.php';
  4. ini_set('max_execution_time', '0');
  5. class Queue extends Suco_Controller_Action
  6. {
  7. public function init()
  8. {
  9. $this->_request = Suco_Application::instance()->getRequest();
  10. $this->_response = Suco_Application::instance()->getResponse();
  11. }
  12. // public function doAutorun()
  13. // {
  14. // echo '<pre>';
  15. // $this->doGoogleRank();
  16. // // $this->doAllintitle();
  17. // echo '</pre>';
  18. // echo '<script>window.location = \'queue.php?act=autorun&time='.(time()).'\'</script>';
  19. // }
  20. public function doAllintitle()
  21. {
  22. try {
  23. $keywords = M('Project_Keyword')->alias('pk')
  24. ->leftJoin(M('Project')->getTableName().' AS p', 'p.id = pk.project_id')
  25. ->columns('pk.*, p.domain')
  26. ->where('pk.allintitle = -1')
  27. ->where('p.allow_googlerank = 1')
  28. ->order('sync_time ASC, id ASC')
  29. ->paginator(1, 1)
  30. ->fetchRows();
  31. $ct = M('Project_Keyword')->alias('pk')
  32. ->leftJoin(M('Project')->getTableName().' AS p', 'p.id = pk.project_id')
  33. ->where('p.allow_googlerank = 1')
  34. ->columns('
  35. COUNT(*) AS total,
  36. SUM(CASE WHEN allintitle != -1 THEN 1 ELSE 0 END) as complete
  37. ')->fetchRow();
  38. } catch (Suco_Exception $e) {
  39. echo $e->getMessage();
  40. }
  41. echo date('Y/m/d H:i:s') . ' AllInTitle ['.$ct['complete'].'/'.$ct['total'].'] - ';
  42. if (!count($keywords)) {
  43. // sleep(5);
  44. echo ("The Queue is empty. \r\n");
  45. return;
  46. }
  47. foreach($keywords as $keyword) {
  48. try {
  49. $g = M('Project_Keyword')->getAllInTitle($keyword['keyword']);
  50. echo $keyword['keyword']. ' '.$g.' result(s)';
  51. $keyword->allintitle = $g;
  52. $keyword->save();
  53. } catch (Suco_Exception $e) {
  54. echo $e->getMessage();
  55. }
  56. echo "\r\n";
  57. }
  58. }
  59. public function doGoogleRank()
  60. {
  61. $sleep = 10000;
  62. $tomorrow = strtotime(date('Y/m/d', strtotime('+1 days')));
  63. try {
  64. $keywords = M('Project_Keyword')->alias('pk')
  65. ->leftJoin(M('Project')->getTableName().' AS p', 'p.id = pk.project_id')
  66. ->columns('pk.*, p.domain')
  67. ->where('pk.sync_time < ? AND p.allow_googlerank = 1', $tomorrow)
  68. ->order('pk.sync_time ASC, pk.id ASC')
  69. ->paginator(1, 1)
  70. ->fetchRows();
  71. $ct = M('Project_Keyword')->alias('pk')
  72. ->leftJoin(M('Project')->getTableName().' AS p', 'p.id = pk.project_id')
  73. ->where('p.allow_googlerank = 1')
  74. ->columns('
  75. COUNT(*) AS total,
  76. SUM(CASE WHEN pk.sync_time >= '.$tomorrow.' THEN 1 ELSE 0 END) as complete
  77. ')->fetchRow();
  78. } catch (Suco_Exception $e) {
  79. echo $e->getMessage();
  80. }
  81. echo date('Y/m/d H:i:s') . ' GoogleRank ['.$ct['complete'].'/'.$ct['total'].'] - ';
  82. if (!count($keywords)) {
  83. // sleep(300);
  84. echo ("The Queue is empty. \r\n");
  85. return;
  86. }
  87. foreach($keywords as $row) {
  88. try {
  89. if (!$row->project->google_domain) {
  90. throw new App_Exception('Google地区设置错误');
  91. }
  92. // if (($row['google_rank'] <= 10 && strtotime('+3 days', $row['update_time']) >= time())
  93. // || ($row['google_rank'] <= 20 && strtotime('+2 days', $row['update_time']) >= time())
  94. // || ($row['google_rank'] <= 50 && strtotime('+1 days', $row['update_time']) >= time())
  95. // || $row['google_rank'] >= 9999
  96. // || !$row['update_time']) {
  97. $g = M('Project_Keyword')->getGoogleRank($row['domain'], $row['keyword'], 1, $row->project->google_domain);
  98. $g['keyword'] = $row['keyword'];
  99. echo http_build_query($g);
  100. $row->resules = $g['results'];
  101. $row->update_time = time();
  102. $row->sync_time = strtotime('+1 days');
  103. $row->google_rank = $g['rank'];
  104. $row->target_url = $g['url'];
  105. $row->save();
  106. // } else {
  107. // echo '跳过规则';
  108. // }
  109. // if ($g['rank'] <= 10) {
  110. // $sync = strtotime('+3 days');
  111. // } elseif ($g['rank'] <= 20) {
  112. // $sync = strtotime('+2 days');
  113. // } elseif ($g['rank'] <= 50) {
  114. // $sync = strtotime('+1 days');
  115. // } else {
  116. // $sync = strtotime('+7 days');
  117. // }
  118. M('Project_Keyword_History')->insert(array(
  119. 'keyword_id' => $row->id,
  120. 'project_id' => $row['project_id'],
  121. 'google_rank' => $row->google_rank,
  122. 'resules' => $row->resules,
  123. 'top10_url' => $g['top10_url'],
  124. 'create_time' => time()
  125. ));
  126. if ($ct['complete']%10==0) { //每查50次重启一下路由
  127. $sleep = 1000*60*1.5;
  128. // M('Project_Keyword')->restartRouter();
  129. echo " ==== [Restart the router]";
  130. // sleep(120);
  131. }
  132. if ($row->project->exists()) {
  133. $row->project->sync_time = time();
  134. $ct = M('Project_Keyword')->select('
  135. SUM(CASE WHEN google_rank <= 1 THEN 1 ELSE 0 END) as top1,
  136. SUM(CASE WHEN google_rank <= 1 THEN search_num ELSE 0 END) as top1_search,
  137. SUM(CASE WHEN google_rank <= 3 THEN 1 ELSE 0 END) as top3,
  138. SUM(CASE WHEN google_rank <= 3 THEN search_num ELSE 0 END) as top3_search,
  139. SUM(CASE WHEN google_rank <= 10 THEN 1 ELSE 0 END) as top10,
  140. SUM(CASE WHEN google_rank <= 10 THEN search_num ELSE 0 END) as top10_search,
  141. SUM(CASE WHEN google_rank <= 20 THEN 1 ELSE 0 END) as top20,
  142. SUM(CASE WHEN google_rank <= 20 THEN search_num ELSE 0 END) as top20_search,
  143. SUM(CASE WHEN google_rank <= 30 THEN 1 ELSE 0 END) as top30,
  144. SUM(CASE WHEN google_rank <= 30 THEN search_num ELSE 0 END) as top30_search,
  145. SUM(CASE WHEN google_rank <= 50 THEN 1 ELSE 0 END) as top50,
  146. SUM(CASE WHEN google_rank <= 50 THEN search_num ELSE 0 END) as top50_search
  147. ')
  148. ->where('google_rank != 9999 AND project_id = ?', (int)$row->project->id)
  149. ->fetchRow();
  150. $history = M('Project_History')->select()
  151. ->where('project_id = ? AND create_time >= ?', array($row->project->id, strtotime('today')))
  152. ->fetchRow();
  153. $data = $ct->toArray();
  154. $data['project_id'] = $row->project->id;
  155. if (!$history->exists()) {
  156. M('Project_History')->insert($data);
  157. } else {
  158. M('Project_History')->updateById($data, $history->id);
  159. }
  160. if ($row->project->reach_num > 0
  161. && !$row->project->reach_time
  162. && $ct['top10'] >= $row->project->reach_num) {
  163. $row->reach_time = time();
  164. }
  165. $row->project->save();
  166. // echo Suco_Db::dump();
  167. }
  168. } catch (Suco_Exception $e) {
  169. echo $e->getMessage();
  170. }
  171. echo "\r\n";
  172. echo "<script>
  173. var interval = setInterval(function(){
  174. window.location.reload()
  175. // console.log('here');
  176. clearInterval(interval);
  177. }, {$sleep});
  178. </script>";
  179. }
  180. }
  181. public function doEmail()
  182. {
  183. $projects = M('Project')->select()
  184. ->where('is_enabled = 1')
  185. ->fetchRows();
  186. $spider = new Suco_Spider();
  187. $context = stream_context_create(array(
  188. 'http' => array(
  189. 'timeout' => 1000
  190. )
  191. ));
  192. echo "<pre>";
  193. echo date("Y/m/d H:i:s");
  194. echo "\r\n";
  195. foreach($projects as $project) {
  196. if (!$project['api_gateway']) {
  197. continue;
  198. }
  199. $url = $project['api_gateway'].'queue/mail';
  200. try {
  201. echo $url .' - ';
  202. $ret = $spider->connect($url);
  203. if ($ret) {
  204. echo $ret;
  205. }
  206. } catch (Suco_Exception $e) {
  207. echo $e->getMessage();
  208. }
  209. echo "\r\n";
  210. // sleep(1);
  211. }
  212. // echo '<script>window.location = \'/crontab/autorun.php?n='.time().'\';</script>';
  213. }
  214. public function doReport()
  215. {
  216. $projects = M('Project')->select()
  217. ->where('is_enabled = 1')
  218. ->fetchRows();
  219. $spider = new Suco_Spider();
  220. $context = stream_context_create(array(
  221. 'http' => array(
  222. 'timeout' => 1000
  223. )
  224. ));
  225. echo "<pre>";
  226. echo date("Y/m/d H:i:s");
  227. echo "\r\n";
  228. foreach($projects as $project) {
  229. if (!$project['api_gateway']) {
  230. continue;
  231. }
  232. $url = $project['api_gateway'].'queue/report';
  233. try {
  234. echo $url .' - ';
  235. $ret = $spider->connect($url);
  236. if ($ret) {
  237. echo $ret;
  238. }
  239. } catch (Suco_Exception $e) {
  240. echo $e->getMessage();
  241. }
  242. echo "\r\n";
  243. // sleep(1);
  244. }
  245. // echo '<script>window.location = \'/crontab/autorun.php?n='.time().'\';</script>';
  246. }
  247. }
  248. if ($_GET['act']) {
  249. $action = $_GET['act'];
  250. $control = new Queue();
  251. $control->dispatch($action);
  252. }