Commit f828e4f9 authored by hj's avatar hj

更新提交

parent 533d1c59
......@@ -49,13 +49,37 @@ class CheMenuApiService extends PlatformAbstractService
sleep(60);
$this->saveCategory($categoryName, $parentCategoryName, $webPath);
}
}else{
dd(123);
}
$this->requestError($exception);
}
}
public function saveProduct($productContent)
{
try{
$response = $this->getPostClient($this->yybaoApiUrl.'/webpage/chemenu/saveProduct',
[
'productName' => $productContent['productName'],
'iupac' => $productContent['IUPAC'],
'image' => $productContent['image'],
'casInfo' => json_encode($productContent['casInfo']),
'productList' => json_encode($productContent['productList'])
], []);
return $this->returnResponse($response);
}catch(\Throwable $exception){
dd($exception->getMessage());
if(preg_match('/\{(?<response>[\s\S]*?)\}/i', $exception->getMessage(), $response)){
$response = json_decode('{'.$response['response'].'}', true);
if($response && $response['status'] == 429){
sleep(60);
$this->saveProduct($productContent);
}
}
$this->requestError($exception);
}
}
public function returnResponse($apiResponse): bool
{
$response = json_decode($apiResponse, true);
......
......@@ -4,6 +4,7 @@ namespace App\Services\ThirdPlatform;
use App\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Services\ThirdPlatform\Api\CheMenuApiService;
use App\Support\Facades\SimpleLogs;
use App\Support\Traits\Helpers;
class CheMenuService
......@@ -80,83 +81,86 @@ class CheMenuService
}
}
}catch(\Throwable $exception){
throw $exception;
SimpleLogs::writeLog($exception->getMessage(), 'chemenu error', 'error');
}
}
public function productPage($url)
{
$pageUrl = $url['url'];
$responseHtml = $this->apiService->getPage($pageUrl);
//file_put_contents('./test3.html', $responseHtml);
if($responseHtml){
$productPageContent = [];
if(preg_match('/<div[\s]*?class=[\\\'|\"]products-content[\\\'|\"]>/i', $responseHtml)){
$imageDivRegx = '/<div[\s]*?class=[\\\'|\"]product-image[\\\'|\"]>[\s\S]*?<img[\s]*?class=[\\\'|\"]big_img[\\\'|\"][\s]*?src[\s\S]*?[\\\'|\"](?<image>[\s\S]+?)[\\\'|\"]/i';
preg_match($imageDivRegx, $responseHtml, $image);
if($image && $image['image']){
$productPageContent['image'] = $image['image'];
}
try{
$pageUrl = $url['url'];
$responseHtml = $this->apiService->getPage($pageUrl);
//file_put_contents('./test3.html', $responseHtml);
if($responseHtml){
$productPageContent = [];
if(preg_match('/<div[\s]*?class=[\\\'|\"]products-content[\\\'|\"]>/i', $responseHtml)){
$imageDivRegx = '/<div[\s]*?class=[\\\'|\"]product-image[\\\'|\"]>[\s\S]*?<img[\s]*?class=[\\\'|\"]big_img[\\\'|\"][\s]*?src[\s\S]*?[\\\'|\"](?<image>[\s\S]+?)[\\\'|\"]/i';
preg_match($imageDivRegx, $responseHtml, $image);
if($image && $image['image']){
$productPageContent['image'] = $image['image'];
}
$productNameDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-12 col-sm-12[\\\'|\"]>[\s]*?<p[\s]*?class=[\\\'|\"]padding-tb-15[\\\'|\"]>[\s]*?<b>(?<name>[\s\S]+?)<\/b>(?<nameContent>[\s\S]+?)<\/p>/i';
preg_match_all($productNameDivRegx, $responseHtml, $productName);
if($productName){
foreach($productName['nameContent'] as $key => $name){
$strName = $this->stripHtml($name);
if(substr($strName, 0, 1) == ':'){
$strName = str_replace(':', '', $strName);
switch($key){
case 0:
$productPageContent['productName'] = $strName;break;
case 1:
$productPageContent['IUPAC'] = $strName;break;
$productNameDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-12 col-sm-12[\\\'|\"]>[\s]*?<p[\s]*?class=[\\\'|\"]padding-tb-15[\\\'|\"]>[\s]*?<b>(?<name>[\s\S]+?)<\/b>(?<nameContent>[\s\S]+?)<\/p>/i';
preg_match_all($productNameDivRegx, $responseHtml, $productName);
if($productName){
foreach($productName['nameContent'] as $key => $name){
$strName = $this->stripHtml($name);
if(substr($strName, 0, 1) == ':'){
$strName = str_replace(':', '', $strName);
switch($key){
case 0:
$productPageContent['productName'] = $strName;break;
case 1:
$productPageContent['IUPAC'] = $strName;break;
}
}
}
}
}
$chemicalInfoDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-6 col-sm-12[\\\'|\"]>(?<chemicalInfoDiv>[\s\S]+?)<\/div>/i';
preg_match_all($chemicalInfoDivRegx, $responseHtml, $chemicalInfoDiv);
if($chemicalInfoDiv){
foreach($chemicalInfoDiv['chemicalInfoDiv'] as $div){
$div = $this->stripHtml($div);
$infoList = explode("\r\n",$div);
foreach($infoList as $info){
$info = trim($info);
if(empty($info)) continue;
try{
list($name, $content) = explode(':', $info);
}catch(\Throwable $exception){
list($name, $content) = explode(':', $info);
$chemicalInfoDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-6 col-sm-12[\\\'|\"]>(?<chemicalInfoDiv>[\s\S]+?)<\/div>/i';
preg_match_all($chemicalInfoDivRegx, $responseHtml, $chemicalInfoDiv);
if($chemicalInfoDiv){
foreach($chemicalInfoDiv['chemicalInfoDiv'] as $div){
$div = $this->stripHtml($div);
$infoList = explode("\r\n",$div);
foreach($infoList as $info){
$info = trim($info);
if(empty($info)) continue;
try{
list($name, $content) = explode(':', $info);
}catch(\Throwable $exception){
list($name, $content) = explode(':', $info);
}
$productPageContent['casInfo'][$name] = $content;
}
$productPageContent['casInfo'][] = [$name =>$content];
}
}
}
$productPageContent['casInfo'] = array_unique($productPageContent['casInfo'],SORT_REGULAR);
//$productPageContent['casInfo'] = array_unique($productPageContent['casInfo'],SORT_REGULAR);
$productListDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-12 col-lg-12 col-sm-12 product-details-size[\\\'|\"]>[\s\S]*?<tbody>(?<productList>[\s\S]*?)<\/tbody>/i';
if(preg_match_all($productListDivRegx, $responseHtml, $productList)){
if(!empty($productList['productList'])){
foreach($productList['productList'] as $product){
$product = $this->stripTabTag($product);
preg_match_all('/<td[\s\S]*?>(?<content>[\s\S]*?)<\/td>/i', $product, $productContentTd);
$productPageContent['productList'] = [];
if($productContentTd['content']){
$productContent = $productContentTd['content'];
$productContent = array_chunk($productContent, 5);
foreach($productContent as $content){
if(empty($content[0])) continue;
$productPageContent['productList'][] = ['productUnit' => $content[0], 'stock' => $content[1]];
$productListDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-12 col-lg-12 col-sm-12 product-details-size[\\\'|\"]>[\s\S]*?<tbody>(?<productList>[\s\S]*?)<\/tbody>/i';
if(preg_match_all($productListDivRegx, $responseHtml, $productList)){
if(!empty($productList['productList'])){
foreach($productList['productList'] as $product){
$product = $this->stripTabTag($product);
preg_match_all('/<td[\s\S]*?>(?<content>[\s\S]*?)<\/td>/i', $product, $productContentTd);
$productPageContent['productList'] = [];
if($productContentTd['content']){
$productContent = $productContentTd['content'];
$productContent = array_chunk($productContent, 5);
foreach($productContent as $content){
if(empty($content[0])) continue;
$productPageContent['productList'][] = ['productUnit' => $content[0], 'stock' => $content[1]];
}
}
}
}
}
$this->apiService->saveProduct($productPageContent);
}
dd($productPageContent);
}
}catch(\Throwable $exception){
SimpleLogs::writeLog($exception->getMessage(), 'chemenu error', 'error');
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment