Spider.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. namespace Collection\Lib;
  3. use Dever;
  4. use Dever\Support\Img as Handle;
  5. class Spider
  6. {
  7. public function test_api()
  8. {
  9. Dever::setInput('info_id', 1);
  10. Dever::setInput('time_name', '美女图');
  11. $data = array
  12. (
  13. 'project_id' => 12,
  14. 'cdate' => '2021-12-01',
  15. 'name' => '吴倩COSMO武汉姑娘大片感受武汉日夜烟火',
  16. 'pic' => 'https://img.lianzhixiu.com/uploads/allimg/202010/9999/b5d3945c79.jpg,https://img.lianzhixiu.com/uploads/allimg/202010/9999/f9eb58379d.jpg',
  17. 'name' => 'jk制服国产女人美眉黑丝诱惑不露脸套图',
  18. 'pic' => 'https://img.lianzhixiu.com/uploads/allimg/202012/9999/1fd41507e0.jpg,https://img.lianzhixiu.com/uploads/allimg/202012/9999/4532772faa.jpg',
  19. 'cdate' => '2021-12-03',
  20. 'name' => '阳光下的漂亮美眉白丝美腿图片',
  21. 'pic' => 'https://img.lianzhixiu.com/uploads/allimg/202102/9999/b5d97a42d9.jpg,https://img.lianzhixiu.com/uploads/allimg/202102/9999/f64dc83f0d.jpg',
  22. 'cdate' => '2021-11-03',
  23. 'name' => '丸子头小姐姐美腿玉足居家写真',
  24. 'pic' => 'https://img.lianzhixiu.com/uploads/allimg/202003/9999/43eefe151d.jpg,https://img.lianzhixiu.com/uploads/allimg/202003/9999/31020deb3d.jpg',
  25. );
  26. $this->pic($data);
  27. }
  28. # 从采集的数据导入 一天采集一次,一次使用12个
  29. public function import_api()
  30. {
  31. $project_id = Dever::input('project_id', 1);
  32. $info_id = Dever::input('info_id', 1);
  33. $info = Dever::db('collection/info')->find($info_id);
  34. if (!$info) {
  35. Dever::alert('错误的配置');
  36. }
  37. $project = Dever::db('spider/project')->find($project_id);
  38. if (!$project) {
  39. Dever::alert('错误的配置');
  40. }
  41. $num = 9;
  42. $where['use'] = 1;
  43. $where['limit'] = '0,' . $num;
  44. $data = Dever::db('spider/data')->getAll($where);
  45. $name = $project['config'] ? $project['config'] : $project['name'];
  46. $year = date('Y');
  47. $type = 11;
  48. if ($project['config']) {
  49. parse_str($project['config'], $config);
  50. if (isset($config['name'])) {
  51. $name = $config['name'];
  52. }
  53. if (isset($config['year'])) {
  54. $year = $config['year'];
  55. }
  56. if (isset($config['type'])) {
  57. $type = $config['type'];
  58. }
  59. }
  60. $update = array();
  61. $update['pic_bg'] = array();
  62. foreach ($data as $k => $v) {
  63. $value = Dever::json_decode($v['value']);
  64. if (isset($value['name']) && isset($value['pic'])) {
  65. $result = Dever::load('collection/lib/create.up', $info_id, $v['id'], $name, $value['name'], $year, $type, $value['pic'], '', $num);
  66. if ($result) {
  67. $update['pic_cover'] = $result['cover_center'];
  68. $update['pic_bg'][] = $result['cover'];
  69. }
  70. }
  71. Dever::db('spider/data')->update(array('where_id' => $v['id'], 'use' => 2));
  72. }
  73. $update['pic_bg'] = implode(',', $update['pic_bg']);
  74. $update['where_id'] = $info_id;
  75. Dever::db('collection/info')->update($update);
  76. return 'ok';
  77. # 导入到唯美中
  78. # 每年12期,
  79. $info_id = Dever::input('info_id', 3);
  80. $source_id = $where['pid'] = Dever::input('pid', 14);
  81. $name = '测试';
  82. $desc = '测试测试';
  83. $year = date('Y');
  84. $type = 11;
  85. $data = 'https://img.lianzhixiu.com/uploads/allimg/202003/9999/43eefe151d.jpg,https://img.lianzhixiu.com/uploads/allimg/202003/9999/31020deb3d.jpg';
  86. $result = Dever::load('collection/lib/create.up_commit', $info_id, $source_id, $name, $desc, $year, $type, $data, $cover = '');
  87. print_r($result);die;
  88. echo 1;die;
  89. $where['use'] = 1;
  90. $where['limit'] = '0,15';
  91. $data = Dever::db('spider/data')->getAll($where);
  92. $main_name = '收集者';
  93. $page_name = '唯美';
  94. $page_desc = '';
  95. $date = explode('-', date('Y-m-d'));
  96. $param = array();
  97. if ($data) {
  98. print_r($data);die;
  99. foreach ($data as $k => $v) {
  100. $value = Dever::array_decode($v['value']);
  101. if ($value && is_array($value)) {
  102. //Dever::db('spider/data')->update(array('where_id' => $v['id'], 'use' => 2));
  103. foreach ($value as $k1 => $v1) {
  104. $page_desc = $v1['name'];
  105. $pic = $v1['pic'];
  106. $this->upPic($id, $source_id, $date, $main_name, $page_name, $page_desc, $pic, $param);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. # 接收采集的图片数据
  113. public function pic($data)
  114. {
  115. return;
  116. $id = Dever::input('info_id');
  117. $main_name = Dever::input('main_name', '图片收集序列');
  118. $page_name = Dever::input('page_name');
  119. $page_desc = Dever::input('page_desc');
  120. $day_name_type = Dever::input('day_name_type', 1);
  121. if (!$id || !$main_name || !$page_name || !$page_desc) {
  122. return;
  123. }
  124. if (!isset($data['pic'])) {
  125. return;
  126. }
  127. if (!$data['pic']) {
  128. return;
  129. }
  130. $pic = explode(',', $data['pic']);
  131. $source_id = $data['project_id'];
  132. $date = array();
  133. if (isset($data['cdate'])) {
  134. $date = explode('-', $data['cdate']);
  135. } else {
  136. if (isset($data['year']) && $data['year']) {
  137. $date[0] = $data['year'];
  138. }
  139. if (isset($data['month']) && $data['month']) {
  140. $date[1] = $data['month'];
  141. }
  142. if (isset($data['day']) && $data['day']) {
  143. $date[2] = $data['day'];
  144. }
  145. }
  146. $this->upPic($id, $source_id, $date, $main_name, $page_name, $page_desc, $pic, $data, $day_name_type);
  147. }
  148. public function upPic($id, $source_id, $date, $main_name, $page_name, $page_desc, $pic, $data, $day_name_type = 1)
  149. {
  150. if (isset($date[0]) && isset($date[1]) && isset($date[2])) {
  151. $year = intval($date[0]);
  152. if (!$year) {
  153. return;
  154. }
  155. $month = intval($date[1]);
  156. if (!$month) {
  157. return;
  158. }
  159. $day = intval($date[2]);
  160. if (!$day) {
  161. return;
  162. }
  163. if ($month < 4) {
  164. $page_name .= '第1季';
  165. $page_reorder = 4;
  166. } elseif ($month >= 4 && $month <= 6) {
  167. $page_name .= '第2季';
  168. $page_reorder = 3;
  169. } elseif ($month >= 7 && $month <= 9) {
  170. $page_name .= '第3季';
  171. $page_reorder = 2;
  172. } elseif ($month > 9) {
  173. $page_name .= '第4季';
  174. $page_reorder = 1;
  175. }
  176. # 按年生成子时光
  177. $year_name = $year . '年';
  178. # 按月生成子章节
  179. $month_name = $month . '月';
  180. # 生成内容名称
  181. if ($day_name_type == 2 && isset($data['name']) && $data['name']) {
  182. $day_name = $data['name'];
  183. } else {
  184. $day_name = '第' . $day . '天';
  185. }
  186. # 每期最多12个,多了有点卡顿,后续要优化的
  187. $max = 12;
  188. /*
  189. if ($day <= 10) {
  190. $month_name .= '上旬';
  191. } elseif ($day > 10 && $day <= 20) {
  192. $month_name .= '中旬';
  193. } elseif ($day > 20) {
  194. $month_name .= '下旬';
  195. }
  196. */
  197. $reorder = (-1*($month - 12))*10 + 100;
  198. # 按项目生成主时光
  199. $times_id_parent = $this->createTimes($id, $source_id, $main_name);
  200. if ($times_id_parent) {
  201. $times_id = $this->createTimes($id, $source_id, $year_name, $year, $times_id_parent);
  202. if ($times_id) {
  203. $parent_page_id = $this->createPage($id, $source_id, $page_name, -1, $times_id_parent, $times_id, '', '', $page_reorder);
  204. if ($parent_page_id) {
  205. $cover = $pic[0];
  206. $cover = Dever::load('upload/view')->get($cover, 't', 1);
  207. $cover = Dever::load('upload/view')->get($cover, 'c', 1);
  208. $page_id = $this->createPage($id, $source_id, $month_name, $parent_page_id, $times_id_parent, $times_id, $page_desc, $cover, $reorder, $max);
  209. if ($page_id) {
  210. $content_id = $this->createContent($id, $source_id, $page_id, 11, $day_name, $cover);
  211. if ($content_id) {
  212. $this->createPic($id, $content_id, $pic);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. # 生成时光
  221. public function createTimes($info_id, $source_id, $name, $year = '', $parent = -1)
  222. {
  223. $table = 'collection/times';
  224. $where['info_id'] = $info_id;
  225. $where['source_id'] = $source_id;
  226. $where['times_id'] = $parent;
  227. $where['name'] = $name;
  228. $param = array();
  229. if ($year) {
  230. $param['year'] = $year;
  231. }
  232. return Dever::upinto($table, $where, $param);
  233. }
  234. # 生成章节
  235. public function createPage($info_id, $source_id, $name, $parent = -1, $times_id_parent, $times_id, $desc = '', $pic = '', $reorder = 1, $max = 12, $num = 1)
  236. {
  237. $table = 'collection/page';
  238. $where['info_id'] = $info_id;
  239. $where['source_id'] = $source_id;
  240. $where['name'] = $name;
  241. $where['page_id'] = $parent;
  242. $where['times_id_parent'] = $times_id_parent;
  243. $where['times_id'] = $times_id;
  244. if ($desc) {
  245. $param['desc'] = $desc;
  246. }
  247. if ($pic) {
  248. $param['pic'] = $pic;
  249. }
  250. if ($reorder) {
  251. $param['reorder'] = $reorder;
  252. }
  253. if ($parent > 0) {
  254. $where['clear'] = true;
  255. $where['name'] .= '第'.$num.'期';
  256. $info = Dever::db($table)->one($where);
  257. if (!$info) {
  258. $param = array_merge($where, $param);
  259. $id = Dever::db($table)->insert($param);
  260. } else {
  261. $total = Dever::db('collection/content')->total(array('info_id' => $info_id, 'page_id' => $info['id'], 'clear' => true));
  262. if ($total >= $max) {
  263. $num = $num+1;
  264. $reorder += $num;
  265. $id = $this->createPage($info_id, $source_id, $name, $parent, $times_id_parent, $times_id, $desc, $pic, $reorder, $max, $num);
  266. } else {
  267. $id = $info['id'];
  268. }
  269. }
  270. } else {
  271. $id = Dever::upinto($table, $where, $param);
  272. }
  273. return $id;
  274. }
  275. # 生成内容
  276. public function createContent($info_id, $source_id, $page_id, $type, $name, $pic)
  277. {
  278. $table = 'collection/content';
  279. $where['info_id'] = $info_id;
  280. $where['source_id'] = $source_id;
  281. $where['name'] = $name;
  282. $where['page_id'] = $page_id;
  283. $where['type'] = $type;
  284. if ($pic) {
  285. $param['pic'] = $pic;
  286. }
  287. return Dever::upinto($table, $where, $param);
  288. }
  289. # 自动创建章节
  290. public function getContentNum($info_id)
  291. {
  292. $table = 'collection/content';
  293. $where['info_id'] = $info_id;
  294. $info = Dever::db($table)->getNew($where);
  295. $max = 15;
  296. $result = array();
  297. //唯美 第1章(时光)12季(主章节),1季12章(子章节),1章15篇(内容)
  298. if ($info) {
  299. if ($info['reorder'] >= $max) {
  300. $info['reorder'] = 1;
  301. # 篇数加1
  302. } else {
  303. $result['reorder'] = $info['reorder'] + 1;
  304. $result['page_id'] = $info['page_id'];
  305. $result['content_id'] = $this->createContent($id, $source_id, $result['page_id'], 11, $day_name, $cover);
  306. }
  307. return $info['reorder'] + 1;
  308. }
  309. return 1;
  310. }
  311. # 生成图片
  312. public function createPic($id, $content_id, $pic)
  313. {
  314. $table = 'content/pic';
  315. $where['info_id'] = $id;
  316. $where['content_id'] = $content_id;
  317. foreach ($pic as $k => $v) {
  318. $where['pic_key'] = md5($v);
  319. $param['pic'] = $v;
  320. $v = Dever::curl($v . '?imageInfo');
  321. $v = Dever::json_decode($v);
  322. if (isset($v['width']) && $v['width'] > 0) {
  323. Dever::upinto($table, $where, $param);
  324. Dever::load('collection/lib/content')->updateTypeId($id, '', $where);
  325. }
  326. }
  327. }
  328. }