海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
12 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\CollectProduct;
  4. use App\Models\Product;
  5. use App\Models\Supplier;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Http;
  8. class Collector extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'collector {--import=} {--limit=}'; //php artisan collector --import=$supplier_id
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '马蜂窝产品采集';
  22. /**
  23. * 如果已经存在是否要更新,true更新,false不更新
  24. * @var bool
  25. */
  26. private bool $exists_update = true;
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. $supplier_id = $this->option('import');
  44. if (!empty($supplier_id)) {
  45. $this->import($supplier_id);
  46. $this->line("供应商 $supplier_id 导入完毕");
  47. } else {
  48. // $this->exists_update = false;
  49. // $this->tourist_line();
  50. // $this->hotel();
  51. $this->scenic();
  52. $this->line('全部采集完毕');
  53. }
  54. return Command::SUCCESS;
  55. }
  56. //导入
  57. private function import($supplier_id)
  58. {
  59. $limit = $this->option('limit');
  60. $cpModel = CollectProduct::query()->orderBy('id', 'desc');
  61. if (!empty($limit)) {
  62. $arr = explode(',', $limit);
  63. $count = count($arr);
  64. if ($count == 1) {
  65. $import_data = $cpModel->limit($arr[0])->get()->toArray();
  66. } else if ($count == 2) {
  67. $import_data = $cpModel->offset($arr[0])->limit($arr[1])->get()->toArray();
  68. } else {
  69. $import_data = $cpModel->get()->toArray();
  70. }
  71. } else {
  72. $import_data = $cpModel->get()->toArray();
  73. }
  74. if (ctype_digit($supplier_id)) {
  75. $ids = [$supplier_id];
  76. } else if ($supplier_id == 'all') {
  77. $ids = Supplier::query()->where('id', '>', 1)->pluck('id');
  78. } else {
  79. return;
  80. }
  81. foreach ($ids as $supplier_id) {
  82. $this->line('正在导入 ' . $supplier_id);
  83. array_walk($import_data, function ($v) use ($supplier_id) {
  84. $v['supplier_id'] = $supplier_id;
  85. unset($v['unique_id'], $v['site']);
  86. Product::query()->updateOrCreate(['supplier_id' => $supplier_id, 'title' => $v['title']], $v);
  87. });
  88. $this->line("导入 $supplier_id 结束");
  89. }
  90. }
  91. //景区采集
  92. private function scenic()
  93. {
  94. $http = Http::withOptions(['verify' => false])->withHeaders(['User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1']);
  95. for ($i=15; $i<20; $i++) {
  96. $this->line('开始采集景区:第 ' . ($i + 1) . ' 页');
  97. $html = $http->get('https://m.mafengwo.cn/jd/10030/gonglve.html?page=' . ($i + 1));
  98. if (preg_match_all('/data-url="(.*?)"/', $html, $matches)) {
  99. if(empty($matches[1])) continue;
  100. foreach ($matches[1] as $url) {
  101. $this->line('开始采集 ' . $url);
  102. $res = $http->get('https://m.mafengwo.cn' . $url);
  103. if (!preg_match('/\/(\d+)\.html/', $url, $match)) continue;
  104. $id = $match[1];
  105. $inc_sales = $http->post('https://m.mafengwo.cn/poi/poi/inc_sales', ['poiid' => $id]);
  106. preg_match('#<strong><em>¥</em>(\d+)</strong>#', $inc_sales['html'] ?? '', $match_price);
  107. preg_match('#<h1>(.*?)</h1>#', $res, $match_title);
  108. preg_match_all('#<div class="swiper-slide">[\s\S]*?<img src="(.*?)"#', $res, $match_pictures);
  109. preg_match('#蜂蜂点评<br/><strong>(\d+)条#', $res, $match_sale);
  110. preg_match('#"row h1" data-jump="ticket">\s*([\w\W]*?)\s*</div>\s*<div class="row h1"#s', $res, $match_know);
  111. preg_match('#<div class="desc" style="display: none;">\s*([\w\W]*?)\s*</div>#s', $res, $match_content);
  112. preg_match_all('#时间:</dt>\s*<dd>\s*(.*?)\s*</dd>#', $res, $match_open_time);
  113. //扩展字段
  114. preg_match('#<p class="t2">地址:(.*?)<i></i></p>#', $res, $match_address);
  115. $pos_start = strpos($res, '<div class="keywords">');
  116. $pos_end = strpos($res, '</div>', $pos_start);
  117. preg_match_all('#<span>([^<>]*?)</span>#s', substr($res, $pos_start, $pos_end - $pos_start), $match_project);
  118. if (isset($match_project[1]) && is_array($match_project[1])) {
  119. $extends['field_2_project'] = array_map(function($v) {
  120. return [
  121. 'name' => $v,
  122. 'num' => '',
  123. 'price' => '',
  124. ];
  125. }, $match_project[1]);
  126. }
  127. $extends['field_2_address'] = $match_address[1] ?? '';
  128. $extends['field_2_open_time'][0]['node'] = '营业时间';
  129. $extends['field_2_open_time'][0]['summer'] = '上岛时间';
  130. $extends['field_2_open_time'][0]['winter'] = '下岛时间';
  131. if (isset($match_open_time[1]) && is_array($match_open_time[1])) {
  132. $extends['field_2_open_time'][1]['node'] = $match_open_time[1][0] ?? '';
  133. $extends['field_2_open_time'][1]['summer'] = $match_open_time[1][1] ?? '';
  134. $extends['field_2_open_time'][1]['winter'] = $match_open_time[1][2] ?? '';
  135. }
  136. CollectProduct::updateOrCreate(['unique_id' => $id, 'site' => 1], [
  137. 'unique_id' => $id,
  138. 'site' => 1,
  139. 'type' => 2, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  140. 'title' => $match_title[1] ?? '',
  141. 'price' => $match_price[1] ?? 0,
  142. 'original_price' => ($match_price[1] ?? 0) * 1.58,
  143. 'pictures' => $match_pictures[1] ?? [],
  144. 'stock' => mt_rand(1000, 9999),
  145. 'sale' => $match_sale[1] ?? 0,
  146. 'status' => -2, //-2下架
  147. 'know' => $match_know[1] ?? '',
  148. 'content' => $match_content[1] ?? '',
  149. 'extends' => $extends,
  150. 'longitude' => 0,
  151. 'latitude' => 0,
  152. 'address' => $match_address[1] ?? '',
  153. ]);
  154. $this->line("采集 $url 完毕" . PHP_EOL);
  155. }
  156. }
  157. $this->line('第 ' . ($i + 1) . ' 页采集结束' . PHP_EOL);
  158. }
  159. }
  160. //酒店采集
  161. private function hotel()
  162. {
  163. $http = Http::withOptions(['verify' => false]);
  164. $mddid = 10030; //10030==三亚
  165. for ($i=0; $i<10; $i++) {
  166. $this->line('开始采集酒店:第 ' . ($i + 1) . ' 页');
  167. $data = $http->get('https://m.mafengwo.cn/rest/hotel/hotels/', [
  168. 'filter' => [
  169. 'mddid' => $mddid
  170. ],
  171. 'page' => [
  172. 'mode' => 'sequential',
  173. 'boundary' => $i * 20, //分页参数
  174. 'num' => 20
  175. ],
  176. ]);
  177. if (empty($data['data']['list'])) {
  178. continue;
  179. }
  180. foreach ($data['data']['list'] as $v) {
  181. if (empty($v['id'])) continue;
  182. if ($this->exists_update == false && CollectProduct::where(['unique_id' => $v['id'], 'site' => 1])->exists()) {
  183. continue;
  184. }
  185. $this->line('采集详情:' . $v['id']);
  186. //基本信息
  187. $params = [
  188. '_ts' => time() . '123',
  189. 'hotel_id' => (string)$v['id'],
  190. 'lat' => '',
  191. 'lng' => '',
  192. 'rmdd_id' => (string)$mddid,
  193. ];
  194. $params['_sn'] = $this->_sn($params);
  195. $data = $http->get('https://m.mafengwo.cn/hservice/detail/info/base_info', $params);
  196. if (empty($data['data']['info'])) {
  197. continue;
  198. }
  199. $base_info = $data['data']['info'];
  200. //酒店详情
  201. $data = $http->get('https://m.mafengwo.cn/hservice/detail/info/guide_info', ['hotel_id' => $v['id']]);
  202. $guide_info = $data['data']['info'] ?? [];
  203. //旅游须知
  204. $know = "<p>入住时间:" . (!empty($guide_info['check_in']['title']) ? $guide_info['check_in']['title'] : '') . "</p>";
  205. $know .= "<p>离店时间:" . (!empty($guide_info['check_out']['title']) ? $guide_info['check_out']['title'] : '') . "</p>";
  206. $know .= '<p>' . array_reduce($base_info['facility_sort'] ?? [], fn($v1, $v2) => $v1 . $v2['title'] ?? '') . '</p>';
  207. //扩展字段
  208. $extends['field_1_tags'] = array_map(fn($v) => $v['title'] ?? '', $guide_info['facility'] ?? []);
  209. $extends['field_1_name'] = $base_info['name'];
  210. $extends['field_1_address'] = $base_info['address'];
  211. $extends['field_1_latitude'] = $base_info['lat'];
  212. $extends['field_1_longitude'] = $base_info['lng'];
  213. CollectProduct::updateOrCreate(['unique_id' => $v['id'], 'site' => 1], [
  214. 'unique_id' => $v['id'],
  215. 'site' => 1,
  216. 'type' => 1, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  217. 'title' => mb_substr($base_info['name'] ?? '' . $base_info['level'] ?? '', 0, 255),
  218. 'price' => mt_rand(150, 350),
  219. 'original_price' => mt_rand(350, 550),
  220. 'pictures' => array_map(fn($v) => $v['url'] ?? '', $base_info['album'] ?? []) ?? [],
  221. 'stock' => mt_rand(1000, 9999),
  222. 'sale' => $base_info['num_collect'] ?? 0,
  223. 'status' => -2, //-2下架
  224. 'know' => $know,
  225. 'content' => $guide_info['intro'] ?? '',
  226. 'extends' => $extends,
  227. 'longitude' => $base_info['lng'] ?? 0,
  228. 'latitude' => $base_info['lat'] ?? 0,
  229. 'address' => $base_info['address'] ?? '',
  230. ]);
  231. $this->line("{$v['id']} 采集结束" . PHP_EOL);
  232. }
  233. $this->line('第 ' . ($i + 1) . ' 页采集结束' . PHP_EOL);
  234. }
  235. }
  236. //酒店详情计算_sn
  237. private function _sn($params): string
  238. {
  239. ksort($params);
  240. return substr(md5(json_encode($params) . 'c9d6618dbc657b41a66eb0af952906f1'), 2, 10);
  241. }
  242. //旅游线路采集
  243. private function tourist_line()
  244. {
  245. $http = Http::withOptions(['verify' => false]);
  246. for($i=0; $i<10; $i++) {
  247. $this->line('开始采集旅游线路:第 ' . ($i + 1) . ' 页');
  248. $data = $http->get('https://m.mafengwo.cn/sales/ajax.php', [
  249. 'sF' => 'search_new_list',
  250. 'offset' => $i * 10, //分页参数
  251. ]);
  252. if (empty($data['data'])) {
  253. continue;
  254. }
  255. $data = $data['data'];
  256. preg_match_all('/<a href="\/sales\/(\d+)\.html"/', $data, $matches);
  257. if (empty($matches[1])) continue;
  258. foreach ($matches[1] as $id) {
  259. if ($this->exists_update == false && CollectProduct::where(['unique_id' => $id, 'site' => 1])->exists()) {
  260. continue;
  261. }
  262. $this->line('开始采集:' . $id);
  263. $info = $http->get('https://m.mafengwo.cn/sales/detail/index/info?id=' . $id);
  264. //旅游须知
  265. $know = $info['data']['list']['content'][0]['content'] ?? [];
  266. if (isset($info['data']['list']['content'][0]['content']) && is_array($info['data']['list']['content'][0]['content'])) {
  267. $know = current(array_filter($info['data']['list']['content'][0]['content'], fn($v) => isset($v['name']) && $v['name'] == '购买须知'));
  268. if (isset($know['content']) && is_array($know['content'])) {
  269. $know = array_reduce(
  270. $know['content'], fn($v1, $v2) => $v1 .
  271. (isset($v2['name']) && is_string($v2['name']) ? "<h3>{$v2['name']}</h3>" : '') .
  272. (isset($v2['content']) && is_string($v2['content']) ? $v2['content'] : '')
  273. );
  274. }
  275. }
  276. //产品详情
  277. $content = '';
  278. if (isset($info['data']['list']['content'][0]['content']) && is_array($info['data']['list']['content'][0]['content'])) {
  279. $content = current(array_filter($info['data']['list']['content'][0]['content'], fn($v) => isset($v['key']) && $v['key'] == 'introduce'));
  280. if (isset($content['content']) && is_array($content['content'])) {
  281. $content = current(array_filter($content['content'], fn($v) => isset($v['key']) && $v['key'] == 'introduction'));
  282. $content = is_string($content['content']) ? $content['content'] : '';
  283. } else {
  284. $content = '';
  285. }
  286. }
  287. //扩展字段
  288. $extends = [];
  289. if (isset($info['data']['list']['base']['tags']) && is_array($info['data']['list']['base']['tags'])) {
  290. foreach ($info['data']['list']['base']['tags'] as $tag) {
  291. $extends['field_0_project'][] = ['name' => $tag, 'num' => '', 'price' => ''];
  292. }
  293. }
  294. CollectProduct::updateOrCreate(['unique_id' => $id, 'site' => 1], [
  295. 'unique_id' => $id,
  296. 'site' => 1,
  297. 'type' => 0, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  298. 'title' => mb_substr($info['data']['list']['base']['title'] ?? '', 0, 255),
  299. 'price' => $info['data']['list']['base']['price_zhanshi'] ?? 0,
  300. 'original_price' => ($info['data']['list']['base']['price_zhanshi'] ?? 0) * 1.58,
  301. 'pictures' => $info['data']['list']['base']['imgList'] ?? [],
  302. 'stock' => mt_rand(1000, 9999),
  303. 'sale' => $info['data']['list']['base']['sold']['num'] ?? 0,
  304. 'status' => -2, //-2下架
  305. 'know' => $know,
  306. 'content' => $content,
  307. 'extends' => $extends,
  308. 'longitude' => 0,
  309. 'latitude' => 0,
  310. 'address' => '',
  311. ]);
  312. $this->line($id . ' 采集完毕!' . PHP_EOL);
  313. }
  314. }
  315. }
  316. }