Parse.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace Spider\Lib;
  3. use Dever;
  4. use Spider\Lib\Doc\Dom;
  5. class Parse
  6. {
  7. private $url = '';
  8. private $host = '';
  9. private $log;
  10. private $doc = array();
  11. private $data = array();
  12. public function __construct($url, $project, $rule, $param, $col, $set, $push)
  13. {
  14. $doc = Doc::getInstance($url, $rule);
  15. $doc->log(new Log($project));
  16. $data = $doc->get($param);
  17. if ($data) {
  18. if (!is_array($data) && !is_object($data)) {
  19. $state = Dever::json_decode($data);
  20. if ($state) {
  21. $data = $state;
  22. }
  23. }
  24. if ($data) {
  25. if (is_array($data)) {
  26. $domain = parse_url($url);
  27. $host = $domain['scheme'] . '://' . $domain['host'] . '/';
  28. foreach ($data as $k => $v) {
  29. if (is_string($v) && !strstr($v, 'http')) {
  30. $v = $host . ltrim($v, '/');
  31. }
  32. $this->data[$k] = $this->load($doc, $k, $v, $col, $set, $push, $project, $v);
  33. }
  34. } else {
  35. $this->data = $this->load($doc, 0, $data, $col, $set, $push, $project, $url);
  36. }
  37. }
  38. }
  39. $doc->saveLog();
  40. }
  41. public function get()
  42. {
  43. return $this->data;
  44. }
  45. private function load($doc, $index, $data, $col, $set, $push, $project, $source)
  46. {
  47. if (!$col) {
  48. if (Dever::input('test') == 1) {
  49. $doc->outLog();
  50. echo 'error';die;
  51. }
  52. return false;
  53. }
  54. $result = $table = array();
  55. if (isset($col[1])) {
  56. $data = $doc->init($data);
  57. $this->getCol($doc, $col[1], $data, $result, $table, $source);
  58. }
  59. if (isset($col[2])) {
  60. $data = $doc->getCur();
  61. $this->getCol($doc, $col[2], $data, $result, $table, $source);
  62. }
  63. if ($set) {
  64. foreach ($set as $v) {
  65. $value = $this->set($index, $v, $project);
  66. $result[$v['key']] = $value;
  67. if (Dever::input('test') == 1) {
  68. $table[$v['name']] = $value;
  69. }
  70. }
  71. }
  72. if ($push) {
  73. $result['test'] = Dever::input('test');
  74. $this->push($push, $result, $project);
  75. }
  76. if (Dever::input('test') == 1) {
  77. $doc->outLog();
  78. echo Dever::table($table);die;
  79. }
  80. $this->update($result, $project, $source);
  81. return $result;
  82. }
  83. private function getCol($doc, $col, $data, &$result, &$table, $source = false)
  84. {
  85. foreach ($col as $v) {
  86. $callback = false;
  87. if (strpos($v['key'], '.') !== false) {
  88. $temp = explode('.', $v['key']);
  89. $v['key'] = $temp[1];
  90. $callback = $temp[0];
  91. }
  92. if ($source && strstr($v['collect_rule'], '{link}')) {
  93. $v['collect_rule'] = str_replace('{link}', $source, $v['collect_rule']);
  94. }
  95. $value = $doc->rule($data, $col, $v);
  96. if ($value == 'error') {
  97. break;
  98. }
  99. if ($callback) {
  100. if (function_exists($callback)) {
  101. $value = $callback($value);
  102. } else {
  103. $value = Dever::{$callback}($value);
  104. }
  105. }
  106. if ($v['local'] == 1) {
  107. $this->res = $v['res_key'];
  108. $value = $this->local($value, $v['type']);
  109. } elseif ($v['type'] == 2) {
  110. if (is_string($value) && strstr($value, '[')) {
  111. $temp = Dever::json_decode($value);
  112. if ($temp) {
  113. $value = implode(',', $temp);
  114. }
  115. }
  116. }
  117. if ($v['collect_filter_link'] == 1) {
  118. $value = $this->filter($value);
  119. }
  120. if ($value) {
  121. $result[$v['key']] = $value;
  122. if (Dever::input('test') == 1) {
  123. $table[$v['name']] = $value;
  124. }
  125. }
  126. }
  127. }
  128. private function push($push, $data, $project)
  129. {
  130. $push = Dever::split($push);
  131. $data['project_id'] = $project;
  132. foreach ($push as $k => $v) {
  133. if (strstr($v, 'http')) {
  134. Dever::curl($v, $data, 'post');
  135. } else {
  136. Dever::load($v, $data);
  137. }
  138. }
  139. }
  140. private function set($index, $data, $project)
  141. {
  142. if ($data['type'] == 1) {
  143. return $data['value'];
  144. } elseif ($data['type'] == 2) {
  145. $old = 0;
  146. $info = Dever::db('spider/data')->getOne(array('pid' => $project));
  147. if($info) {
  148. $value = json_decode($info['value'], true);
  149. if (isset($value[$data['key']])) {
  150. $old = $value[$data['key']];
  151. }
  152. }
  153. return $data['value'] + $index + $old;
  154. } elseif ($data['type'] == 3) {
  155. $eval = '$value = ' . $data['value'] . ';';
  156. eval($eval);
  157. return $value;
  158. } elseif ($data['type'] == 4) {
  159. $temp = Dever::split($data['value']);
  160. return mt_rand($temp[0], $temp[1]);
  161. } elseif ($data['type'] == 5) {
  162. $temp = Dever::split($data['value']);
  163. $temp[0] = Dever::maketime($temp[0]);
  164. $temp[1] = Dever::maketime($temp[1]);
  165. return mt_rand($temp[0], $temp[1]);
  166. }
  167. }
  168. private function filter($content)
  169. {
  170. $rule = '<(a).+href="(.*?)"(.*?)>(.*?)<\/a>';
  171. $content = preg_replace_callback('/' . $rule . '/i', array($this, 'filter_replace'), $content);
  172. return $content;
  173. }
  174. private function filter_replace($result)
  175. {
  176. if (isset($result[4]) && $result[4]) {
  177. return $result[4];
  178. }
  179. }
  180. private function local($content, $type = 1)
  181. {
  182. if ($type == 1) {
  183. $doc = Dom::init($content);
  184. $pic = Dom::find($doc, '$("img").each().attr("src")');
  185. if ($pic) {
  186. $content = $this->local_replace($pic, $content);
  187. }
  188. $video = Dom::find($doc, '$("video").each().attr("src")');
  189. if ($video) {
  190. $content = $this->local_replace($video, $content);
  191. }
  192. $audio = Dom::find($doc, '$("audio").each().attr("src")');
  193. if ($audio) {
  194. $content = $this->local_replace($audio, $content);
  195. }
  196. /*
  197. $rule = '<(img|video|audio).+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>';
  198. $content = preg_replace_callback('/' . $rule . '/i', array($this, 'local_rule_replace'), $content);
  199. */
  200. } else {
  201. $content = $this->copy($content);
  202. }
  203. return $content;
  204. }
  205. private function local_replace($file, $content)
  206. {
  207. if (is_string($file) && strstr($file, '[')) {
  208. $file = Dever::json_decode($file);
  209. }
  210. if (is_array($file)) {
  211. foreach ($file as $k => $v) {
  212. $content = $this->local_replace($v, $content);
  213. }
  214. } else {
  215. $result = $this->copy($file);
  216. if ($result) {
  217. $content = str_replace($file, $result, $content);
  218. }
  219. }
  220. return $content;
  221. }
  222. private function local_rule_replace($result)
  223. {
  224. if (isset($result[2]) && $result[2]) {
  225. $file = $this->copy($result[2]);
  226. if ($file) {
  227. $result[0] = str_replace($result[2], $file, $result[0]);
  228. return $result[0];
  229. }
  230. }
  231. }
  232. private function copy($file)
  233. {
  234. if (is_string($file) && strstr($file, '[')) {
  235. $temp = Dever::json_decode($file);
  236. if ($temp) {
  237. $file = array();
  238. foreach($temp as $k => $v) {
  239. $f = $this->copy($v);
  240. if ($f) {
  241. $file[] = $f;
  242. }
  243. }
  244. $file = implode(',', $file);
  245. return $file;
  246. }
  247. }
  248. if (strstr($file, ',')) {
  249. $temp = explode(',', $file);
  250. $file = array();
  251. foreach($temp as $k => $v) {
  252. $f = $this->copy($v);
  253. if ($f) {
  254. $file[] = $f;
  255. }
  256. }
  257. $file = implode(',', $file);
  258. return $file;
  259. }
  260. if (!strstr($file, 'http') && strstr($file, '//')) {
  261. $file = 'https:' . $file;
  262. }
  263. $data = Dever::load('upload/save.copy?file=' . $file . '&key=' . $this->res . '&state=1');
  264. if (isset($data['status']) && $data['status'] == -1) {
  265. return '';
  266. } elseif (isset($data['url'])) {
  267. return $data['url'];
  268. } else {
  269. return '';
  270. }
  271. }
  272. private function update($data, $project, $source)
  273. {
  274. if (!$data) {
  275. return;
  276. }
  277. $param['pid'] = $project;
  278. $param['source'] = $source;
  279. $info = Dever::db('spider/data')->one($param);
  280. $param['value'] = json_encode($data, JSON_UNESCAPED_UNICODE);
  281. if ($info) {
  282. $update = $param;
  283. $id = $update['where_id'] = $info['id'];
  284. Dever::db('spider/data')->update($update);
  285. } else {
  286. $update = $param;
  287. $id = Dever::db('spider/data')->insert($update);
  288. }
  289. echo $id;
  290. echo "\r\n";
  291. }
  292. }