Get.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace Spider\Lib;
  3. use Dever;
  4. class Get
  5. {
  6. public function wechat_api()
  7. {
  8. $time = time();
  9. $config = Dever::db('spider/wechat')->get(array('where_zdate' => $time));
  10. if ($config) {
  11. foreach ($config as $k => $v) {
  12. //$this->get($v['id'], $v['name'], $v['wechat']);
  13. Dever::daemon('lib/get.get?id=' . $v['id'], 'spider');
  14. if (!$v['zdate'] || $v['zdate'] < 100000) {
  15. $v['zdate'] = time();
  16. }
  17. Dever::db('spider/wechat')->update(array('where_id' => $v['id'], 'zdate' => $v['zdate'] + ($v['pdate']*3600)));
  18. }
  19. }
  20. return 'ok';
  21. }
  22. public function up($id, $name, $data)
  23. {
  24. $audit = Dever::param('audit', $data);
  25. $info = Dever::db('spider/data')->one($id);
  26. $wechat = Dever::db('spider/wechat')->one($info['wechat_id']);
  27. # 查询文章表里有没有
  28. $article = Dever::db('content/article')->one(array('wechat_data_id' => $id));
  29. if ($audit == 2) {
  30. $insert['cate_id'] = $wechat['cate_id'];
  31. $insert['author_id'] = $wechat['author_id'];
  32. $insert['name'] = $info['name'];
  33. $insert['pdate'] = $info['pdate'];
  34. $insert['pic_cover'] = $info['cover'];
  35. $insert['content'] = $info['content'];
  36. $insert['audit'] = 2;
  37. $insert['wechat'] = 2;
  38. $insert['wechat_data_id'] = $info['id'];
  39. if (!$article) {
  40. Dever::db('content/article')->insert($insert);
  41. } else {
  42. $insert['where_id'] = $article['id'];
  43. Dever::db('content/article')->update($insert);
  44. }
  45. } else {
  46. if ($article) {
  47. $where['where_id'] = $article['id'];
  48. $where['audit'] = 1;
  49. Dever::db('content/article')->update($where);
  50. }
  51. }
  52. }
  53. public function get()
  54. {
  55. $id = Dever::input('id');
  56. $info = Dever::db('spider/wechat')->one($id);
  57. $name = $info['name'];
  58. $wechat = $info['wechat'];
  59. $url = 'https://weixin.sogou.com/weixin?type=1&query='.$name.'&ie=utf8&s_from=input&_sug_=n&_sug_type_=';
  60. $content = $this->content($name, $url);
  61. preg_match_all('/<a target="_blank" uigs="account_name_0" href="(.*?)">(.*?)<\/a>/i', $content, $matches);
  62. if (isset($matches[1][0])) {
  63. $url = $matches[1][0];
  64. $url = str_replace('&amp;', '&', $url);
  65. $content = $this->content($name, $url);
  66. # 获取微信号 <p class="profile_account">微信号: tesexiaozhenzmg</p>
  67. if (!$wechat) {
  68. preg_match_all('/<p class="profile_account">微信号: (.*?)<\/p>/i', $content, $matches);
  69. if (isset($matches[1][0]) && $matches[1][0]) {
  70. $wechat = $matches[1][0];
  71. Dever::db('spider/wechat')->update(array('where_id' => $id, 'wechat' => $wechat));
  72. }
  73. }
  74. # 获取微信前10条内容
  75. preg_match_all('/var msgList = {(.*?)};/i', $content, $matches);
  76. if (isset($matches[1][0]) && $matches[1][0])
  77. {
  78. $string = '{'.$matches[1][0].'}';
  79. $array = Dever::json_decode($string);
  80. if (isset($array['list'])) {
  81. foreach ($array['list'] as $k => $v) {
  82. $this->getArticle($id, $name, $v['app_msg_ext_info']);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. private function getArticle($id, $name, $data)
  89. {
  90. $host = 'https://mp.weixin.qq.com';
  91. $url = $host . $data['content_url'];
  92. $url = str_replace('&amp;', '&', $url);
  93. $content = $this->content($name, $url);
  94. preg_match_all('/<div class="rich_media_content " id="js_content">([\s\S]*?)<\/div>/i', $content, $matches);
  95. $result['content'] = '';
  96. if (isset($matches[1][0]) && $matches[1][0]) {
  97. $result['content'] = $this->convertContent($matches[1][0]);
  98. }
  99. $result['title'] = $data['title'];
  100. $result['cover'] = $this->pic($data['cover']);
  101. $result['copyright'] = $data['copyright_stat'] == 11 ? 1 : 2;
  102. $result['pdate'] = '';
  103. # 获取时间
  104. preg_match_all('/var ct = "(.*?)";/i', $content, $matches);
  105. if ($matches[1][0] && $matches[1][0]) {
  106. $result['pdate'] = $matches[1][0];
  107. }
  108. $where['wechat_id'] = $id;
  109. $where['name'] = $result['title'];
  110. $info = Dever::db('spider/data')->one($where);
  111. $where['cover'] = $result['cover'];
  112. $where['content'] = $result['content'];
  113. $where['copyright'] = $result['copyright'];
  114. $where['pdate'] = $result['pdate'];
  115. if (!$info) {
  116. Dever::db('spider/data')->insert($where);
  117. } else {
  118. $where['where_id'] = $info['id'];
  119. Dever::db('spider/data')->update($where);
  120. }
  121. }
  122. private function convertContent($content)
  123. {
  124. $content = trim($content);
  125. preg_match_all('/data-src="(.*?)"/i', $content, $matches);
  126. if (isset($matches[1]) && $matches[1]) {
  127. foreach ($matches[1] as $k => $v) {
  128. $pic = $this->pic($v);
  129. $content = str_replace($v, $pic, $content);
  130. }
  131. }
  132. $content = str_replace('data-src', 'src', $content);
  133. return $content;
  134. }
  135. private function pic($pic)
  136. {
  137. $data = Dever::load('upload/save.copy?key=1&file=' . urlencode($pic));
  138. if ($data && isset($data['url'])) {
  139. return $data['url'];
  140. } else {
  141. return '';
  142. }
  143. }
  144. private function header()
  145. {
  146. $ip = $this->ip();
  147. $header = array();
  148. $header['Accept'] = 'image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*';
  149. $header['Connection'] = 'Keep-Alive';
  150. $header['Pragma'] = 'no-cache';
  151. $header['Accept-Language'] = 'zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3';
  152. $header['User-Agent'] = $this->agent();
  153. $header['CLIENT-IP'] = $ip;
  154. $header['X-FORWARDED-FOR'] = $ip;
  155. return $header;
  156. }
  157. private function agent()
  158. {
  159. $agent = [
  160. 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
  161. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)',
  162. 'Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
  163. 'Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)',
  164. 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)',
  165. 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)',
  166. 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)',
  167. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)',
  168. 'Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527 (KHTML, like Gecko, Safari/419.3) Arora/0.6',
  169. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1',
  170. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0',
  171. 'Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5',
  172. 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6',
  173. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
  174. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20',
  175. 'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52'];
  176. return $agent[rand(0,count($agent) - 1)];;
  177. }
  178. private function ip()
  179. {
  180. $ip_long = array(
  181. array('607649792', '608174079'), //36.56.0.0-36.63.255.255
  182. array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255
  183. array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255
  184. array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255
  185. array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255
  186. array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255
  187. array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255
  188. array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255
  189. array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255
  190. array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255
  191. );
  192. $rand_key = mt_rand(0, 9);
  193. $ip = long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
  194. return $ip;
  195. }
  196. private function content($name, $url)
  197. {
  198. $key = md5($name) . '_' . md5($url);
  199. $time = time();
  200. $date = explode('-', date('Y-m-d', $time));
  201. $file = Dever::path(Dever::data() . 'tmp/', $date[0] . '/' . $date[1] . '/' . $date[2] . '/' . $key);
  202. $num = 3600;
  203. if (!is_file($file) || (is_file($file) && filemtime($file) - $time > $num)) {
  204. $header = $this->header();
  205. $content = Dever::curl($url, false, 'get', false, $header, $header['User-Agent'], 'http://weixin.sogou.com/');
  206. file_put_contents($file, $content);
  207. } else {
  208. $content = file_get_contents($file);
  209. }
  210. if (!$content) {
  211. Dever::alert('内容已失效');
  212. }
  213. return $content;
  214. }
  215. }