海南旅游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.

365 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
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. if ($this->exists_update == false && CollectProduct::where(['unique_id' => $id, 'site' => 1])->exists()) {
  106. continue;
  107. }
  108. $inc_sales = $http->post('https://m.mafengwo.cn/poi/poi/inc_sales', ['poiid' => $id]);
  109. preg_match('#<strong><em>¥</em>(\d+)</strong>#', $inc_sales['html'] ?? '', $match_price);
  110. preg_match('#<h1>(.*?)</h1>#', $res, $match_title);
  111. preg_match_all('#<div class="swiper-slide">[\s\S]*?<img src="(.*?)"#', $res, $match_pictures);
  112. preg_match('#蜂蜂点评<br/><strong>(\d+)条#', $res, $match_sale);
  113. preg_match('#"row h1" data-jump="ticket">\s*([\w\W]*?)\s*</div>\s*<div class="row h1"#s', $res, $match_know);
  114. preg_match('#<div class="desc" style="display: none;">\s*([\w\W]*?)\s*</div>#s', $res, $match_content);
  115. preg_match_all('#时间:</dt>\s*<dd>\s*(.*?)\s*</dd>#', $res, $match_open_time);
  116. //扩展字段
  117. preg_match('#<p class="t2">地址:(.*?)<i></i></p>#', $res, $match_address);
  118. $pos_start = strpos($res, '<div class="keywords">');
  119. $pos_end = strpos($res, '</div>', $pos_start);
  120. preg_match_all('#<span>([^<>]*?)</span>#s', substr($res, $pos_start, $pos_end - $pos_start), $match_project);
  121. if (isset($match_project[1]) && is_array($match_project[1])) {
  122. $extends['field_2_project'] = array_map(function($v) {
  123. return [
  124. 'name' => $v,
  125. 'num' => '',
  126. 'price' => '',
  127. ];
  128. }, $match_project[1]);
  129. }
  130. $extends['field_2_address'] = $match_address[1] ?? '';
  131. $extends['field_2_open_time'][0]['node'] = '营业时间';
  132. $extends['field_2_open_time'][0]['summer'] = '上岛时间';
  133. $extends['field_2_open_time'][0]['winter'] = '下岛时间';
  134. if (isset($match_open_time[1]) && is_array($match_open_time[1])) {
  135. $extends['field_2_open_time'][1]['node'] = $match_open_time[1][0] ?? '';
  136. $extends['field_2_open_time'][1]['summer'] = $match_open_time[1][1] ?? '';
  137. $extends['field_2_open_time'][1]['winter'] = $match_open_time[1][2] ?? '';
  138. }
  139. CollectProduct::updateOrCreate(['unique_id' => $id, 'site' => 1], [
  140. 'unique_id' => $id,
  141. 'site' => 1,
  142. 'type' => 2, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  143. 'title' => $match_title[1] ?? '',
  144. 'price' => $match_price[1] ?? 0,
  145. 'original_price' => ($match_price[1] ?? 0) * 1.58,
  146. 'pictures' => $match_pictures[1] ?? [],
  147. 'stock' => mt_rand(1000, 9999),
  148. 'sale' => $match_sale[1] ?? 0,
  149. 'status' => -2, //-2下架
  150. 'know' => $match_know[1] ?? '',
  151. 'content' => $match_content[1] ?? '',
  152. 'extends' => $extends,
  153. 'longitude' => 0,
  154. 'latitude' => 0,
  155. 'address' => $match_address[1] ?? '',
  156. ]);
  157. $this->line("采集 $url 完毕" . PHP_EOL);
  158. }
  159. }
  160. $this->line('第 ' . ($i + 1) . ' 页采集结束' . PHP_EOL);
  161. }
  162. }
  163. //酒店采集
  164. private function hotel()
  165. {
  166. $http = Http::withOptions(['verify' => false]);
  167. $mddid = 10030; //10030==三亚
  168. for ($i=0; $i<10; $i++) {
  169. $this->line('开始采集酒店:第 ' . ($i + 1) . ' 页');
  170. $data = $http->get('https://m.mafengwo.cn/rest/hotel/hotels/', [
  171. 'filter' => [
  172. 'mddid' => $mddid
  173. ],
  174. 'page' => [
  175. 'mode' => 'sequential',
  176. 'boundary' => $i * 20, //分页参数
  177. 'num' => 20
  178. ],
  179. ]);
  180. if (empty($data['data']['list'])) {
  181. continue;
  182. }
  183. foreach ($data['data']['list'] as $v) {
  184. if (empty($v['id'])) continue;
  185. if ($this->exists_update == false && CollectProduct::where(['unique_id' => $v['id'], 'site' => 1])->exists()) {
  186. continue;
  187. }
  188. $this->line('采集详情:' . $v['id']);
  189. //基本信息
  190. $params = [
  191. '_ts' => time() . '123',
  192. 'hotel_id' => (string)$v['id'],
  193. 'lat' => '',
  194. 'lng' => '',
  195. 'rmdd_id' => (string)$mddid,
  196. ];
  197. $params['_sn'] = $this->_sn($params);
  198. $data = $http->get('https://m.mafengwo.cn/hservice/detail/info/base_info', $params);
  199. if (empty($data['data']['info'])) {
  200. continue;
  201. }
  202. $base_info = $data['data']['info'];
  203. //酒店详情
  204. $data = $http->get('https://m.mafengwo.cn/hservice/detail/info/guide_info', ['hotel_id' => $v['id']]);
  205. $guide_info = $data['data']['info'] ?? [];
  206. //旅游须知
  207. $know = "<p>入住时间:" . (!empty($guide_info['check_in']['title']) ? $guide_info['check_in']['title'] : '') . "</p>";
  208. $know .= "<p>离店时间:" . (!empty($guide_info['check_out']['title']) ? $guide_info['check_out']['title'] : '') . "</p>";
  209. $know .= '<p>' . array_reduce($base_info['facility_sort'] ?? [], fn($v1, $v2) => $v1 . $v2['title'] ?? '') . '</p>';
  210. //扩展字段
  211. $extends['field_1_tags'] = array_map(fn($v) => $v['title'] ?? '', $guide_info['facility'] ?? []);
  212. $extends['field_1_name'] = $base_info['name'];
  213. $extends['field_1_address'] = $base_info['address'];
  214. $extends['field_1_latitude'] = $base_info['lat'];
  215. $extends['field_1_longitude'] = $base_info['lng'];
  216. CollectProduct::updateOrCreate(['unique_id' => $v['id'], 'site' => 1], [
  217. 'unique_id' => $v['id'],
  218. 'site' => 1,
  219. 'type' => 1, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  220. 'title' => mb_substr($base_info['name'] ?? '' . $base_info['level'] ?? '', 0, 255),
  221. 'price' => mt_rand(150, 350),
  222. 'original_price' => mt_rand(350, 550),
  223. 'pictures' => array_map(fn($v) => $v['url'] ?? '', $base_info['album'] ?? []) ?? [],
  224. 'stock' => mt_rand(1000, 9999),
  225. 'sale' => $base_info['num_collect'] ?? 0,
  226. 'status' => -2, //-2下架
  227. 'know' => $know,
  228. 'content' => $guide_info['intro'] ?? '',
  229. 'extends' => $extends,
  230. 'longitude' => $base_info['lng'] ?? 0,
  231. 'latitude' => $base_info['lat'] ?? 0,
  232. 'address' => $base_info['address'] ?? '',
  233. ]);
  234. $this->line("{$v['id']} 采集结束" . PHP_EOL);
  235. }
  236. $this->line('第 ' . ($i + 1) . ' 页采集结束' . PHP_EOL);
  237. }
  238. }
  239. //酒店详情计算_sn
  240. private function _sn($params): string
  241. {
  242. ksort($params);
  243. return substr(md5(json_encode($params) . 'c9d6618dbc657b41a66eb0af952906f1'), 2, 10);
  244. }
  245. //旅游线路采集
  246. private function tourist_line()
  247. {
  248. $http = Http::withOptions(['verify' => false]);
  249. for($i=0; $i<10; $i++) {
  250. $this->line('开始采集旅游线路:第 ' . ($i + 1) . ' 页');
  251. $data = $http->get('https://m.mafengwo.cn/sales/ajax.php', [
  252. 'sF' => 'search_new_list',
  253. 'offset' => $i * 10, //分页参数
  254. ]);
  255. if (empty($data['data'])) {
  256. continue;
  257. }
  258. $data = $data['data'];
  259. preg_match_all('/<a href="\/sales\/(\d+)\.html"/', $data, $matches);
  260. if (empty($matches[1])) continue;
  261. foreach ($matches[1] as $id) {
  262. if ($this->exists_update == false && CollectProduct::where(['unique_id' => $id, 'site' => 1])->exists()) {
  263. continue;
  264. }
  265. $this->line('开始采集:' . $id);
  266. $info = $http->get('https://m.mafengwo.cn/sales/detail/index/info?id=' . $id);
  267. //旅游须知
  268. $know = $info['data']['list']['content'][0]['content'] ?? [];
  269. if (isset($info['data']['list']['content'][0]['content']) && is_array($info['data']['list']['content'][0]['content'])) {
  270. $know = current(array_filter($info['data']['list']['content'][0]['content'], fn($v) => isset($v['name']) && $v['name'] == '购买须知'));
  271. if (isset($know['content']) && is_array($know['content'])) {
  272. $know = array_reduce(
  273. $know['content'], fn($v1, $v2) => $v1 .
  274. (isset($v2['name']) && is_string($v2['name']) ? "<h3>{$v2['name']}</h3>" : '') .
  275. (isset($v2['content']) && is_string($v2['content']) ? $v2['content'] : '')
  276. );
  277. }
  278. }
  279. //产品详情
  280. $content = '';
  281. if (isset($info['data']['list']['content'][0]['content']) && is_array($info['data']['list']['content'][0]['content'])) {
  282. $content = current(array_filter($info['data']['list']['content'][0]['content'], fn($v) => isset($v['key']) && $v['key'] == 'introduce'));
  283. if (isset($content['content']) && is_array($content['content'])) {
  284. $content = current(array_filter($content['content'], fn($v) => isset($v['key']) && $v['key'] == 'introduction'));
  285. $content = is_string($content['content']) ? $content['content'] : '';
  286. } else {
  287. $content = '';
  288. }
  289. }
  290. //扩展字段
  291. $extends = [];
  292. if (isset($info['data']['list']['base']['tags']) && is_array($info['data']['list']['base']['tags'])) {
  293. foreach ($info['data']['list']['base']['tags'] as $tag) {
  294. $extends['field_0_project'][] = ['name' => $tag, 'num' => '', 'price' => ''];
  295. }
  296. }
  297. CollectProduct::updateOrCreate(['unique_id' => $id, 'site' => 1], [
  298. 'unique_id' => $id,
  299. 'site' => 1,
  300. 'type' => 0, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  301. 'title' => mb_substr($info['data']['list']['base']['title'] ?? '', 0, 255),
  302. 'price' => $info['data']['list']['base']['price_zhanshi'] ?? 0,
  303. 'original_price' => ($info['data']['list']['base']['price_zhanshi'] ?? 0) * 1.58,
  304. 'pictures' => $info['data']['list']['base']['imgList'] ?? [],
  305. 'stock' => mt_rand(1000, 9999),
  306. 'sale' => $info['data']['list']['base']['sold']['num'] ?? 0,
  307. 'status' => -2, //-2下架
  308. 'know' => $know,
  309. 'content' => $content,
  310. 'extends' => $extends,
  311. 'longitude' => 0,
  312. 'latitude' => 0,
  313. 'address' => '',
  314. ]);
  315. $this->line($id . ' 采集完毕!' . PHP_EOL);
  316. }
  317. }
  318. }
  319. }