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.
 
 
user_center/app/Console/Commands/CarModelUpdate.php

216 lines
11 KiB

<?php
namespace App\Console\Commands;
use App\Models\CarBrandT;
use App\Models\CarModelT;
use App\Models\CarSeriesT;
use App\Models\CarSubBrandT;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use simple_html_dom;
class CarModelUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'car-model:update';
/**
* The console command description.
*
* @var string
*/
protected $description = '车辆牌品更新(途虎)';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
*/
public function handle()
{
$begin_time = time();
$this->tuhu();
$end_time = time();
$total_time = $end_time - $begin_time;
$h = floor($total_time/3600);
$m = floor(($total_time - $h * 3600)/60);
$s = $total_time - $h * 3600 - $m * 60;
echo '花费时间: '.$h.'小时'.$m.'分钟'.$s.'秒<br>';
}
private function tuhu(){
$disk = Storage::disk('oss');
$brand_infos = json_decode($this->getHtml('https://item.tuhu.cn/Car/GetCarBrands2', 'https://by.tuhu.cn/'), true);
foreach ($brand_infos as $letter => $brands_info) {
if($letter == 'hot'){
continue;
}
foreach ($brands_info as $brand_info) {
$name = trim(explode('-', $brand_info['Brand'])[1]);
$brand = CarBrandT::where('name',$name)->where('letter',$letter)->first();
if (!$brand) {
$brand = new CarBrandT();
$brand->logo = '';
}
if(!Str::startsWith($brand->logo, 'http://images.banmacar.com')){
file_put_contents(storage_path(). 'logo.png',file_get_contents('https://img1.tuhu.org'. $brand_info['Url']));
$filename = $disk->putFile("png/".date('Ymd'), storage_path(). 'logo.png');
$brand->logo = str_replace('http://bmoc-files.oss-cn-hangzhou.aliyuncs.com', 'http://images.banmacar.com', $disk->getUrl($filename));
}
$brand->name = $name;
$brand->letter = $letter;
$brand->save();
$sub_brand_array = [];
$sub_brand_infos = json_decode($this->getHtml('https://item.tuhu.cn/Car/SelOneBrand?Brand='. urlencode($brand_info['Brand']), 'https://by.tuhu.cn/'),true);
foreach ($sub_brand_infos['OneBrand'] as $sub_brand_info) {
if(!isset($sub_brand_array[$sub_brand_info['BrandType']])){
$sub_brand = CarSubBrandT::where('car_brand_id',$brand->id)
->where('name',$sub_brand_info['BrandType'])->first();
if (!$sub_brand) {
$sub_brand = new CarSubBrandT();
$sub_brand->car_brand_id = $brand->id;
}
$sub_brand->name = $sub_brand_info['BrandType'];
$sub_brand->save();
echo '----'. $sub_brand_info['BrandType']. '<br>' . PHP_EOL;
$sub_brand_array[$sub_brand_info['BrandType']] = $sub_brand->id;
}
$series = CarSeriesT::where('car_sub_brand_id',$sub_brand_array[$sub_brand_info['BrandType']])
->where('name',$sub_brand_info['CarName'])->first();
if (!$series) {
$series = new CarSeriesT();
$series->car_sub_brand_id = $sub_brand_array[$sub_brand_info['BrandType']];
}
$series->name = $sub_brand_info['CarName'];
$series->save();
echo '--------'. $sub_brand_info['CarName']. '<br>' . PHP_EOL;
$displacement_infos = json_decode($this->getHtml('https://item.tuhu.cn/Car/SelectVehicle?VehicleID='. urlencode($sub_brand_info['ProductID']), 'https://by.tuhu.cn/'),true);
foreach ($displacement_infos['PaiLiang'] as $displacement_info) {
$years = json_decode($this->getHtml('https://item.tuhu.cn/Car/SelectVehicle?VehicleID='. urlencode($sub_brand_info['ProductID']) . '&PaiLiang='. urlencode($displacement_info['Value']), 'https://by.tuhu.cn/'),true);
if(!isset($years['Nian'])){
sleep(3);
$years = json_decode($this->getHtml('https://item.tuhu.cn/Car/SelectVehicle?VehicleID='. urlencode($sub_brand_info['ProductID']) . '&PaiLiang='. urlencode($displacement_info['Value']), 'https://by.tuhu.cn/'),true);
}else if(count($years['Nian']) <= 0){
continue;
}
$model_years = '';
foreach ($years['Nian'] as $year) {
$model_years .= $year['Value'] . ',';
}
$model_years = substr($model_years,0,-1);
foreach ($years['Nian'] as $year) {
$model_infos = json_decode($this->getHtml('https://item.tuhu.cn/Car/SelectVehicleSalesName?VehicleID='. urlencode($sub_brand_info['ProductID']) . '&PaiLiang='. urlencode($displacement_info['Value']) . '&Nian='. urlencode($year['Value']), 'https://by.tuhu.cn/'),true);
if(!isset($model_infos['SalesName'])){
sleep(3);
$model_infos = json_decode($this->getHtml('https://item.tuhu.cn/Car/SelectVehicleSalesName?VehicleID='. urlencode($sub_brand_info['ProductID']) . '&PaiLiang='. urlencode($displacement_info['Value']) . '&Nian='. urlencode($year['Value']), 'https://by.tuhu.cn/'),true);
}
foreach ($model_infos['SalesName'] as $model_info) {
$model = CarModelT::where('car_series_id',$series->id)
->where('name',$model_info['Name'])->first();
if (!$model) {
$model = new CarModelT();
$model->car_series_id = $series->id;
}
$model->year = $year['Value'];
$model->tuhu_code = $model_info['TID'];
$model->modelYears = $model_years;
$model->displacement = $displacement_info['Value'];
$model->name = $model_info['Name'];
$model->save();
echo '------------'. $model_info['Name']. '<br>' . PHP_EOL;
}
}
}
}
}
}
echo 'over';
}
private function autohome(){
$brand_html_str = mb_convert_encoding(substr($this->getHtml($this->url . '?' . http_build_query($this->param), 'https://car.autohome.com.cn/'), 18, -2),'utf-8','gbk');
$brand_content_html = new \simple_html_dom();
$brand_content_html->load($brand_html_str);
foreach ($brand_content_html->find('.cartree-letter') as $letter_html) {
$letter = $letter_html->plaintext;
$brand_html = $letter_html->next_sibling()->find('h3');
foreach ($brand_html as $brand_h3) {
$brand_a = $brand_h3->find('a', 0);
$name = $brand_a->plaintext;
$name = substr($name,0,stripos($name,'('));
$brand = CarBrandT::where('name',$name)->where('letter',$letter)->first();
preg_match('/\-(\d+)/',$brand_a->href,$match);
if (!$brand) {
$brand = new CarBrandT();
$brand->uuid = MyLib::uuid();
echo $name. '<br>' . PHP_EOL;
}
$brand->name = $name;
$brand->autohome_id = $match[1];
$brand->letter = $letter;
$series_html_str = mb_convert_encoding(substr($this->getHtml($brand_a->href, 'https://car.autohome.com.cn/'), 18, -2),'utf-8','gbk');
$series_content_html = new \simple_html_dom();
$series_content_html->load($series_html_str);
foreach ($brand_content_html->find('.carbradn-cont .list-dl') as $sub_brand_html) {
$sub_brand_a = $sub_brand_html->find('dt a', 0);
$sub_brand = CarSubBrandT::where('car_brand_id',$brand->id)->where('name',$name)->first();
if (!$sub_brand) {
$sub_brand = new CarSubBrandT();
$sub_brand->uuid = MyLib::uuid();
$sub_brand->car_brand_id = $brand->id;
echo $name. '<br>' . PHP_EOL;
}
$sub_brand->name = $sub_brand_a->plaintext;
$sub_brand->save();
foreach ($sub_brand_html->find('.list-dl-text a') as $series_a) {
$sub_brand_a = $sub_brand_html->find('dt a', 0);
$sub_brand = CarSubBrandT::where('car_brand_id',$brand->id)->where('name',$name)->first();
if (!$sub_brand) {
$sub_brand = new CarSubBrandT();
$sub_brand->uuid = MyLib::uuid();
$sub_brand->car_brand_id = $brand->id;
echo $name. '<br>' . PHP_EOL;
}
$sub_brand->name = $sub_brand_a->plaintext;
$sub_brand->save();
}
}
$brand->logo = $series_content_html->find('.carbradn-pic img',0)->src;
$brand->save();
}
}
}
private function getHtml($url,$last_url)
{
// 构造包头,模拟浏览器请求
$header = array (
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
if ($content == FALSE) {
echo "error:" . curl_error($ch);
}
curl_close($ch);
return $content;
}
}