|
|
@@ -1,6 +1,7 @@
|
|
|
<?php
|
|
|
namespace app\index\service;
|
|
|
|
|
|
+use app\index\model\GoodsModel;
|
|
|
use think\Config;
|
|
|
|
|
|
class Grab
|
|
|
@@ -33,32 +34,79 @@ class Grab
|
|
|
* 抓取数据
|
|
|
* @param string $resource
|
|
|
*/
|
|
|
- public function grabData($resource=''){
|
|
|
- var_dump($this->urls);
|
|
|
+ public function grabData($resource='', $refresh = false){
|
|
|
+ set_time_limit(0);
|
|
|
$urls = isset($this->urls[$resource])? $this->urls[$resource] : '';
|
|
|
$url = isset($urls['url'])? trim($urls['url']) : '';
|
|
|
- echo $url."\n";
|
|
|
if($url){
|
|
|
- $cookie = '';
|
|
|
- if(file_exists("./logs/{$resource}.cookie")){
|
|
|
- $cookie = file_get_contents("./logs/{$resource}.cookie");
|
|
|
+ $goodsList = $productIds = [];
|
|
|
+ $result = grabRequest($url, '');
|
|
|
+ $objects = isset($result['objects'])? $result['objects'] : [];
|
|
|
+ PRedis::set('grabs:'.$resource.':results:'.date('YmdH'), $result, 3600);
|
|
|
+ foreach($objects as $goods){
|
|
|
+ // 预售参数
|
|
|
+ $goodInfo = [];
|
|
|
+ $productInfo = isset($goods['productInfo'])? $goods['productInfo'] : [];
|
|
|
+ $goods = isset($productInfo[0])? $productInfo[0] : [];
|
|
|
+ $merchProduct = isset($goods['merchProduct'])? $goods['merchProduct'] : [];
|
|
|
+ $productId = isset($merchProduct['id'])? trim($merchProduct['id']) : '';
|
|
|
+
|
|
|
+ // 验证商品是否已经更新
|
|
|
+ if(!$productId || PRedis::get('grabs:'.$resource.':checkGoods:'.$productId)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $productIds[] = $productId;
|
|
|
+ $startDate = isset($merchProduct['commerceStartDate'])? trim($merchProduct['commerceStartDate']) : '';
|
|
|
+ $goodInfo['product_id'] = $productId;
|
|
|
+ $goodInfo['start_date'] = $startDate? date('Y-m-d H:i:s', strtotime($startDate)) : '';
|
|
|
+ $publishDate = isset($merchProduct['commercePublishDate'])? trim($merchProduct['commercePublishDate']) : '';
|
|
|
+ $goodInfo['publish_date'] = $publishDate? date('Y-m-d H:i:s', strtotime($publishDate)) : '';
|
|
|
+ $goodInfo['publish_type'] = isset($merchProduct['publishType'])? trim($merchProduct['publishType']) : '';
|
|
|
+ $status = isset($merchProduct['status'])? trim($merchProduct['status']) : '';
|
|
|
+ $goodInfo['status'] = strtolower($status) == 'active'? 1 : 3;
|
|
|
+
|
|
|
+ // 价格参数
|
|
|
+ $merchPrice = isset($goods['merchPrice'])? $goods['merchPrice'] : [];
|
|
|
+ $goodInfo['msrp'] = isset($merchPrice['msrp'])? floatval($merchPrice['msrp']) : 0.00;
|
|
|
+ $goodInfo['full_price'] = isset($merchPrice['fullPrice'])? floatval($merchPrice['fullPrice']) : 0.00;
|
|
|
+ $goodInfo['current_price'] = isset($merchPrice['currentPrice'])? floatval($merchPrice['currentPrice']) : 0.00;
|
|
|
+ $goodInfo['currency'] = isset($merchPrice['currency'])? trim($merchPrice['currency']) : 'CNY';
|
|
|
+ $goodInfo['discounted'] = isset($merchPrice['discounted'])? intval($merchPrice['discounted']) : 0;
|
|
|
+ $goodInfo['country'] = isset($merchPrice['country'])? $merchPrice['country'] : 'CN';
|
|
|
+
|
|
|
+
|
|
|
+ // 可用状态
|
|
|
+ $availability = isset($goods['availability'])? $goods['availability'] : [];
|
|
|
+ $goodInfo['available'] = isset($availability['available'])? intval($availability['available']) : 0;
|
|
|
+
|
|
|
+ // 商品信息
|
|
|
+ $productContent = isset($goods['productContent'])? $goods['productContent'] : [];
|
|
|
+ $goodInfo['colors_txt'] = isset($productContent['colorDescription'])? $productContent['colorDescription'] : '';
|
|
|
+ $colors = isset($productContent['colors'])? $productContent['colors'] : [];
|
|
|
+ $goodInfo['colors'] = is_array($colors) && $colors? serialize($colors) : '';
|
|
|
+ $goodInfo['slug'] = isset($productContent['slug'])? $productContent['slug'] : '';
|
|
|
+ $goodInfo['full_title'] = isset($productContent['fullTitle'])? $productContent['fullTitle'] : '';
|
|
|
+ $goodInfo['title'] = isset($productContent['title'])? $productContent['title'] : '';
|
|
|
+ $goodInfo['subtitle'] = isset($productContent['subtitle'])? $productContent['subtitle'] : '';
|
|
|
+ $goodInfo['description'] = isset($productContent['description'])? $productContent['description'] : '';
|
|
|
+ $goodInfo['pdpGeneral'] = isset($productContent['pdpGeneral'])? $productContent['pdpGeneral'] : '';
|
|
|
+
|
|
|
+ // 相册数据
|
|
|
+ $imgUrls = isset($goods['imageUrls'])? $goods['imageUrls'] : [];
|
|
|
+ $goodInfo['thumb'] = isset($imgUrls['productImageUrl'])? $imgUrls['productImageUrl'] : '';
|
|
|
+
|
|
|
+ $goodsList[] = $goodInfo;
|
|
|
+ PRedis::rpush("queens:grabs_{$resource}:goods_".date('YmdH'), json_encode($goodInfo, 256));
|
|
|
+ PRedis::expire("queens:grabs_{$resource}:goods_".date('YmdH'), 3 * 3600);
|
|
|
+ PRedis::set('grabs:'.$resource.':checkGoods:'.$productId, $goodInfo, 3*24*3600);
|
|
|
}
|
|
|
- $header = [
|
|
|
- "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
|
|
|
- "Host:www.nike.com",
|
|
|
- "Upgrade-Insecure-Requests:1",
|
|
|
- "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
|
|
|
- "Content-Type:text/html; charset=utf-8",
|
|
|
-// "Cookie:{$cookie}",
|
|
|
- ];
|
|
|
-// $content = file_get_contents($url);
|
|
|
- $content = file_get_contents($url);
|
|
|
-// $content = grabRequest($url, '','');
|
|
|
- file_put_contents('./logs/'.$resource.'_'.date('YmdHis').'.html', $content);
|
|
|
|
|
|
- }
|
|
|
+ PRedis::set('grabs:'.$resource.':goods:'.date('YmdH'), $goodsList, 7*24*3600);
|
|
|
|
|
|
+ }
|
|
|
|
|
|
+ return $productIds;
|
|
|
}
|
|
|
|
|
|
}
|