Get.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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, 'wechat' => 2));
  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_api()
  54. {
  55. }
  56. public function get()
  57. {
  58. $id = Dever::input('id');
  59. $info = Dever::db('spider/wechat')->one($id);
  60. $name = $info['name'];
  61. $wechat = $info['wechat'];
  62. $url = 'https://weixin.sogou.com/weixin?type=1&query='.$name.'&ie=utf8&s_from=input&_sug_=n&_sug_type_=';
  63. $content = $this->content($name, $url);
  64. preg_match_all('/<a target="_blank" uigs="account_name_0" href="(.*?)">(.*?)<\/a>/i', $content, $matches);
  65. if (isset($matches[1][0])) {
  66. $url = $matches[1][0];
  67. $url = str_replace('&amp;', '&', $url);
  68. $content = $this->content($name, $url);
  69. # 获取微信号 <p class="profile_account">微信号: tesexiaozhenzmg</p>
  70. if (!$wechat) {
  71. preg_match_all('/<p class="profile_account">微信号: (.*?)<\/p>/i', $content, $matches);
  72. if (isset($matches[1][0]) && $matches[1][0]) {
  73. $wechat = $matches[1][0];
  74. Dever::db('spider/wechat')->update(array('where_id' => $id, 'wechat' => $wechat));
  75. }
  76. }
  77. # 获取微信前10条内容
  78. preg_match_all('/var msgList = {(.*?)};/i', $content, $matches);
  79. if (isset($matches[1][0]) && $matches[1][0])
  80. {
  81. $string = '{'.$matches[1][0].'}';
  82. $array = Dever::json_decode($string);
  83. if (isset($array['list'])) {
  84. foreach ($array['list'] as $k => $v) {
  85. $this->getArticle($id, $name, $v['app_msg_ext_info']);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. private function getArticle($id, $name, $data)
  92. {
  93. $host = 'https://mp.weixin.qq.com';
  94. $url = $host . $data['content_url'];
  95. $url = str_replace('&amp;', '&', $url);
  96. $content = $this->content($name, $url);
  97. preg_match_all('/<div class="rich_media_content " id="js_content">([\s\S]*?)<\/div>/i', $content, $matches);
  98. $result['content'] = '';
  99. if (isset($matches[1][0]) && $matches[1][0]) {
  100. $result['content'] = $this->convertContent($matches[1][0]);
  101. }
  102. $result['title'] = $data['title'];
  103. $result['cover'] = $this->pic($data['cover']);
  104. $result['copyright'] = $data['copyright_stat'] == 11 ? 1 : 2;
  105. $result['pdate'] = '';
  106. # 获取时间
  107. preg_match_all('/var ct = "(.*?)";/i', $content, $matches);
  108. if ($matches[1][0] && $matches[1][0]) {
  109. $result['pdate'] = $matches[1][0];
  110. }
  111. $where['wechat_id'] = $id;
  112. $where['name'] = $result['title'];
  113. $info = Dever::db('spider/data')->one($where);
  114. $where['cover'] = $result['cover'];
  115. $where['content'] = $result['content'];
  116. $where['copyright'] = $result['copyright'];
  117. $where['pdate'] = $result['pdate'];
  118. if (!$info) {
  119. Dever::db('spider/data')->insert($where);
  120. } else {
  121. $where['where_id'] = $info['id'];
  122. Dever::db('spider/data')->update($where);
  123. }
  124. }
  125. private function convertContent($content)
  126. {
  127. $content = trim($content);
  128. preg_match_all('/data-src="(.*?)"/i', $content, $matches);
  129. if (isset($matches[1]) && $matches[1]) {
  130. foreach ($matches[1] as $k => $v) {
  131. $pic = $this->pic($v);
  132. $content = str_replace($v, $pic, $content);
  133. }
  134. }
  135. $content = str_replace('data-src', 'src', $content);
  136. return $content;
  137. }
  138. private function pic($pic)
  139. {
  140. $data = Dever::load('upload/save.copy?key=1&file=' . urlencode($pic));
  141. if ($data && isset($data['url'])) {
  142. return $data['url'];
  143. } else {
  144. return '';
  145. }
  146. }
  147. private function header()
  148. {
  149. $ip = $this->ip();
  150. $header = array();
  151. $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, */*';
  152. $header['Connection'] = 'Keep-Alive';
  153. $header['Pragma'] = 'no-cache';
  154. $header['Accept-Language'] = 'zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3';
  155. $header['User-Agent'] = $this->agent();
  156. $header['CLIENT-IP'] = $ip;
  157. $header['X-FORWARDED-FOR'] = $ip;
  158. return $header;
  159. }
  160. private function agent()
  161. {
  162. $agent = [
  163. 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
  164. '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)',
  165. '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)',
  166. 'Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)',
  167. '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)',
  168. '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)',
  169. '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)',
  170. '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)',
  171. 'Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527 (KHTML, like Gecko, Safari/419.3) Arora/0.6',
  172. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1',
  173. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0',
  174. 'Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5',
  175. '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',
  176. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
  177. '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',
  178. 'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52'];
  179. return $agent[rand(0,count($agent) - 1)];;
  180. }
  181. private function ip()
  182. {
  183. $ip_long = array(
  184. array('607649792', '608174079'), //36.56.0.0-36.63.255.255
  185. array('1038614528', '1039007743'), //61.232.0.0-61.237.255.255
  186. array('1783627776', '1784676351'), //106.80.0.0-106.95.255.255
  187. array('2035023872', '2035154943'), //121.76.0.0-121.77.255.255
  188. array('2078801920', '2079064063'), //123.232.0.0-123.235.255.255
  189. array('-1950089216', '-1948778497'), //139.196.0.0-139.215.255.255
  190. array('-1425539072', '-1425014785'), //171.8.0.0-171.15.255.255
  191. array('-1236271104', '-1235419137'), //182.80.0.0-182.92.255.255
  192. array('-770113536', '-768606209'), //210.25.0.0-210.47.255.255
  193. array('-569376768', '-564133889'), //222.16.0.0-222.95.255.255
  194. );
  195. $rand_key = mt_rand(0, 9);
  196. $ip = long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
  197. return $ip;
  198. }
  199. private function content($name, $url)
  200. {
  201. $key = md5($name) . '_' . md5($url);
  202. $time = time();
  203. $date = explode('-', date('Y-m-d', $time));
  204. $file = Dever::path(Dever::data() . 'tmp/', $date[0] . '/' . $date[1] . '/' . $date[2] . '/' . $key);
  205. $num = 3600;
  206. if (!is_file($file) || (is_file($file) && filemtime($file) - $time > $num)) {
  207. $header = $this->header();
  208. $content = Dever::curl($url, false, 'get', false, $header, $header['User-Agent'], 'http://weixin.sogou.com/');
  209. file_put_contents($file, $content);
  210. } else {
  211. $content = file_get_contents($file);
  212. }
  213. if (!$content) {
  214. Dever::alert('内容已失效');
  215. }
  216. return $content;
  217. }
  218. }