CheckZhangDuSiteStatus.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/11/20
  6. * Time: 下午5:26
  7. */
  8. namespace App\Console\Commands;
  9. use App\Modules\Statistic\Services\SendStatsEmailService;
  10. use DB;
  11. use GuzzleHttp\Client;
  12. use Illuminate\Console\Command;
  13. use Log;
  14. /**
  15. * 检测掌读的站点信息
  16. * Class CheckZhangDuSiteStatus
  17. * @package App\Console\Commands
  18. */
  19. class CheckZhangDuSiteStatus extends Command
  20. {
  21. /**
  22. * 执行命令 检测掌读的站点信息
  23. *
  24. * The name and signature of the console command.
  25. *
  26. * @var string
  27. */
  28. protected $signature = 'check_zhang_du_site_status';
  29. /**
  30. * The console command description.
  31. *
  32. * @var string
  33. */
  34. protected $description = '检测掌读的站点信息';
  35. public $tag = 'check_zhang_du_site_status';
  36. public $table_name = 'novel_platform_sites_zhangdu';
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. Log::info($this->tag . 'start command');
  45. $this->loadSiteIndexInfo();
  46. Log::info($this->tag . 'end command');
  47. }
  48. function loadSiteIndexInfo()
  49. {
  50. $client = new Client(['timeout' => 3.0,]);
  51. $top_site_arr = ['.com', '.cn', '.net'];
  52. $results = [];
  53. $count = 0;
  54. $header = ['User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A404 MicroMessenger/6.7.4(0x1607042c) NetType/WIFI Language/zh_CN'];
  55. $lastIndex = DB::table($this->table_name)->max('site');
  56. $index = $lastIndex + 1;
  57. while ($index <= $lastIndex + 1000) {
  58. foreach ($top_site_arr as $item) {
  59. try {
  60. $request_url = 'https://s' . $index . '.zhangdu520' . $item;
  61. $response = $client->request('get', $request_url, [
  62. 'headers' => $header,
  63. 'curl' => [
  64. CURLOPT_SSL_VERIFYPEER => false
  65. ]
  66. ]);
  67. $resultCode = $response->getStatusCode();
  68. if ($resultCode == 200 || $resultCode == 302) {
  69. $count++;
  70. $results[] = ['url' => $request_url, 'site' => $index];
  71. $this->saveToDB($index, $request_url);
  72. break;
  73. }
  74. } catch (\Exception $e) {
  75. Log::info($e->getMessage());
  76. }
  77. }
  78. if ($index % 10000 == 0) print_r($index);
  79. $index++;
  80. }
  81. if ($results && count($results) > 0) {
  82. $this->sendEmail($results);
  83. }
  84. }
  85. function saveToDB($site, $url)
  86. {
  87. $params = [
  88. 'url' => $url,
  89. 'site' => $site,
  90. 'platform_name' => '掌读',
  91. 'created_at' => date("Y-m-d H:i:s"),
  92. 'updated_at' => date("Y-m-d H:i:s")];
  93. DB::table($this->table_name)->insert($params);
  94. }
  95. function sendEmail($results)
  96. {
  97. Log::info($this->tag . ' start sendEmail');
  98. $to_user = array(
  99. ['address' => 'zhangzg@iqiyoo.com', 'name' => '张总'],
  100. ['address' => 'zhaojp@yqsd.net', 'name' => '赵君平'],
  101. ['address' => 'songdb@iqiyoo.com', 'name' => 'songdb'],
  102. ['address' => 'sijj@yqsd.net', 'name' => 'sijj'],
  103. ['address' => 'qincp@iqiyoo.com', 'name' => '阿才'],
  104. );
  105. $content = "<table border='1' cellpadding='10' cellspacing='0'><tr><td align='center'>序号</td><td align='center'>站点id</td><td align='center'>访问的url</td></tr>";
  106. $index = 0;
  107. foreach ($results as $item) {
  108. $index++;
  109. $content .= "<tr><td align='center'>{$index}</td><td align='center'>{$item['site']}</td><td align='center'>{$item['url']}</td></tr>";
  110. }
  111. $content .= "</table>";
  112. SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => date("Y-m-d", time()) . "掌读昨天新增站点" . $index . "个", 'body' => $content]);
  113. }
  114. }