Small Fixes

This commit is contained in:
2024-02-13 19:12:52 +01:00
parent 4a51269712
commit c36aef9819

View File

@@ -34,10 +34,11 @@ class CountryCompareController extends Controller
$ta_codes = explode(' ', str_replace('.', '', trim($itemCodes))); $ta_codes = explode(' ', str_replace('.', '', trim($itemCodes)));
$requests = $this->makeRequests($ta_codes[0]); $requests = $this->makeRequests($ta_codes[0]);
return $this->processResponse($requests,$ta_codes[0]); return $this->processResponse($requests, $ta_codes[0]);
} }
public function compare($countries, $cHash, $aCodes){ public function compare($countries, $cHash, $aCodes)
{
$requests = $this->makeRequests($aCodes); $requests = $this->makeRequests($aCodes);
return $this->processResponse($requests, $aCodes); return $this->processResponse($requests, $aCodes);
} }
@@ -45,8 +46,8 @@ class CountryCompareController extends Controller
{ {
$requests = Http::pool(fn (Pool $pool) => [ $requests = Http::pool(fn (Pool $pool) => [
$this->countries->each(function ($country) use ($pool, $ta_code) { $this->countries->each(function ($country) use ($pool, $ta_code) {
Log::info('Getting page {url}', [ 'url' => $country->search_url . $ta_code[0] ] ); Log::info('Getting page {url}', ['url' => $country->search_url . $ta_code[0]]);
return $pool->as($country->country_code)->withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36')->get($country->search_url . $ta_code[0] ); return $pool->as($country->country_code)->withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36')->get($country->search_url . $ta_code[0]);
}) })
]); ]);
return $requests; return $requests;
@@ -68,7 +69,7 @@ class CountryCompareController extends Controller
foreach ($this->countries as $c) { foreach ($this->countries as $c) {
$country = $c->country_code; $country = $c->country_code;
if ($responses[$country] instanceof \Illuminate\Http\Client\Response && $responses[$country]->ok()) { if ($responses[$country] instanceof \Illuminate\Http\Client\Response && $responses[$country]->ok()) {
@@ -82,10 +83,10 @@ class CountryCompareController extends Controller
$prices[$country] = $this->parseJson_CY_GR_BG($code, (string) $response, $country); $prices[$country] = $this->parseJson_CY_GR_BG($code, (string) $response, $country);
break; break;
case "IS": case "IS":
$prices[$country] = $this->parseJson_IS($code,(string) $response, $country); $prices[$country] = $this->parseJson_IS($code, (string) $response, $country);
break; break;
case "TR": case "TR":
$prices[$country] = $this->parseJson_TR((string) $response, $country); $prices[$country] = $this->parseJson_TR($code, (string) $response, $country);
break; break;
case "EE": case "EE":
case "LT": case "LT":
@@ -93,20 +94,20 @@ class CountryCompareController extends Controller
$prices[$country] = $this->parseJson_EE_LT_LV($code, (string) $response, $country); $prices[$country] = $this->parseJson_EE_LT_LV($code, (string) $response, $country);
break; break;
default: default:
$prices[$country] = $this->parseJson((string) $response, $country); $prices[$country] = $this->parseJson($code, (string) $response, $country);
} }
} }
} }
$products = []; $products = [];
collect($prices)->each(function ($price) use (&$products) { collect($prices)->each(function ($price) use (&$products) {
if (count($price)) if (count($price))
$products[] = $price; $products[] = $price;
}); });
return collect($products); return collect($products);
} }
public function parseJson($json_raw, $country) public function parseJson($code, $json_raw, $country)
{ {
$json_values = array(); $json_values = array();
Log::info('{json_raw} {country}', [$json_raw, $country]); Log::info('{json_raw} {country}', [$json_raw, $country]);
@@ -124,6 +125,7 @@ class CountryCompareController extends Controller
$numeral = $salesPrice["numeral"]; $numeral = $salesPrice["numeral"];
$json_values = array( $json_values = array(
'code' => $code,
'url' => $product['pipUrl'], 'url' => $product['pipUrl'],
'tag' => $product['tag'], 'tag' => $product['tag'],
'country' => $country, 'country' => $country,
@@ -135,12 +137,12 @@ class CountryCompareController extends Controller
return $json_values; return $json_values;
} }
public function parseJson_TR($body,$country) public function parseJson_TR($code, $body, $country)
{ {
//echo "country: " . $country . ", code: " . $code . "<br>"; //echo "country: " . $country . ", code: " . $code . "<br>";
$dochtml = new DOMDocument(); $dochtml = new DOMDocument();
@$dochtml->loadHTML( $body,LIBXML_NOERROR); @$dochtml->loadHTML($body, LIBXML_NOERROR);
@$xpath = new DOMXpath($dochtml); @$xpath = new DOMXpath($dochtml);
$price = null; $price = null;
@@ -153,6 +155,7 @@ class CountryCompareController extends Controller
$price = floatval(trim(str_replace('.', '', $price))); $price = floatval(trim(str_replace('.', '', $price)));
$json_values = array( $json_values = array(
'code' => $code,
'url' => $url_product, 'url' => $url_product,
'tag' => null, 'tag' => null,
'salesPrice' => floatval($price), 'salesPrice' => floatval($price),
@@ -162,14 +165,14 @@ class CountryCompareController extends Controller
return $json_values; return $json_values;
} }
public function parseJson_CY_GR_BG($code, $body , $country) public function parseJson_CY_GR_BG($code, $body, $country)
{ {
//echo "country: " . $country . ", code: " . $code . "<br>"; //echo "country: " . $country . ", code: " . $code . "<br>";
//Log::info('{country},{body},{code}',[$country, $body, $code]); //Log::info('{country},{body},{code}',[$country, $body, $code]);
//$body = file_get_contents("https://www.ikea.bg/search-results/?query=80366284"); //$body = file_get_contents("https://www.ikea.bg/search-results/?query=80366284");
libxml_use_internal_errors(true); libxml_use_internal_errors(true);
$dochtml = new DOMDocument(); $dochtml = new DOMDocument();
@$dochtml->loadHTML($body,LIBXML_NOERROR); @$dochtml->loadHTML($body, LIBXML_NOERROR);
@$xpath = new DOMXpath($dochtml); @$xpath = new DOMXpath($dochtml);
$price = null; $price = null;
@@ -183,10 +186,11 @@ class CountryCompareController extends Controller
$json_values = array( $json_values = array(
'url_product' => $url_product, 'code' => $code,
'tag' => null, 'url' => $url_product,
'salesPrice' => floatval($price), 'tag' => null,
'country' => $country, 'salesPrice' => floatval($price),
'country' => $country,
); );
libxml_clear_errors(); libxml_clear_errors();
return $json_values; return $json_values;
@@ -197,32 +201,32 @@ class CountryCompareController extends Controller
//echo "country: " . $country . ", code: " . $code . "<br>"; //echo "country: " . $country . ", code: " . $code . "<br>";
$dochtml = new DOMDocument(); $dochtml = new DOMDocument();
@$dochtml->loadHTML( $body); @$dochtml->loadHTML($body);
@$xpath = new DOMXpath($dochtml); @$xpath = new DOMXpath($dochtml);
$price = null; $price = null;
$c = ltrim($code, "0"); $c = ltrim($code, "0");
$json_values = array(); $json_values = array();
@$price = $xpath->query('//*/div[(@class="itemPriceBox" and @data-item="' . $c . '")]//p[@class="itemNormalPrice display-6"]/span')[0]->nodeValue; @$price = $xpath->query('//*/div[(@class="itemPriceBox" and @data-item="' . $c . '")]//p[@class="itemNormalPrice display-6"]/span')[0]->nodeValue;
if (is_null($price) || empty($price)) { if (is_null($price) || empty($price)) {
@$price = $xpath->query('//*/div[(@class="itemPriceBox" and @data-item="' . $c . '")]//div[@class="itemBTI display-6"]/span')[0]->nodeValue; @$price = $xpath->query('//*/div[(@class="itemPriceBox" and @data-item="' . $c . '")]//div[@class="itemBTI display-6"]/span')[0]->nodeValue;
} }
switch ($country) { switch ($country) {
case "EE": case "EE":
$url_product = "https://www.ikea.ee" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue; $url_product = "https://www.ikea.ee" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
break; break;
case "LT": case "LT":
$url_product = "https://www.ikea.lt" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue; $url_product = "https://www.ikea.lt" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
break; break;
case "LV": case "LV":
$url_product = "https://www.ikea.lv" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue; $url_product = "https://www.ikea.lv" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
break; break;
default: default:
$url_product = null; $url_product = null;
} }
if (is_null($price) || empty($price)) { if (is_null($price) || empty($price)) {
$url_product = null; $url_product = null;
@@ -231,6 +235,7 @@ class CountryCompareController extends Controller
$price = floatval(trim(str_replace(' ', '', str_replace(',', '.', str_replace(array('€'), '', $price))))); $price = floatval(trim(str_replace(' ', '', str_replace(',', '.', str_replace(array('€'), '', $price)))));
$json_values = array( $json_values = array(
'code' => $code,
'url' => $url_product, 'url' => $url_product,
'tag' => null, 'tag' => null,
'salesPrice' => $price, 'salesPrice' => $price,
@@ -245,7 +250,7 @@ class CountryCompareController extends Controller
//echo "country: " . $country . ", code: " . $code . "<br>"; //echo "country: " . $country . ", code: " . $code . "<br>";
$dochtml = new DOMDocument(); $dochtml = new DOMDocument();
@$dochtml->loadHTML( $body); @$dochtml->loadHTML($body);
@$xpath = new DOMXpath($dochtml); @$xpath = new DOMXpath($dochtml);
@@ -253,7 +258,6 @@ class CountryCompareController extends Controller
$c = ltrim($code, "0"); $c = ltrim($code, "0");
$json_values = array(); $json_values = array();
// try {
@$price = $xpath->query('//*/div[(@class="itemPriceBox")]//p[@class="itemNormalPrice revamp_price price"]/span/span')->item(0)->nodeValue; @$price = $xpath->query('//*/div[(@class="itemPriceBox")]//p[@class="itemNormalPrice revamp_price price"]/span/span')->item(0)->nodeValue;
if (is_null($price) || empty($price)) { if (is_null($price) || empty($price)) {
@@ -268,12 +272,10 @@ class CountryCompareController extends Controller
$url_product = null; $url_product = null;
} }
//} catch (\Exception $e) {
// return [];
//}
$price = floatval(trim(str_replace(' ', '', str_replace(',', '.', str_replace('.', '', str_replace(array('€'), '', $price)))))); $price = floatval(trim(str_replace(' ', '', str_replace(',', '.', str_replace('.', '', str_replace(array('€'), '', $price))))));
$json_values = array( $json_values = array(
'code' => $code,
'url' => $url_product, 'url' => $url_product,
'tag' => null, 'tag' => null,
'salesPrice' => $price, 'salesPrice' => $price,
@@ -282,7 +284,5 @@ class CountryCompareController extends Controller
return $json_values; return $json_values;
} }
} }
// { "country": "AT", "code": "50161321", "url": "https://www.ikea.com/at/de/p/hol-aufbewahrungstisch-akazie-50161321/", "name": "HOL", "typeName": "Aufbewahrungstisch", "mainImageUrl": "https://www.ikea.com/at/de/images/products/hol-aufbewahrungstisch-akazie__0104310_pe251255_s5.jpg", "itemNoGlobal": "50161321", "salesPrice": "80.99", "tag": "FAMILY_PRICE", "last_mod": "2023-12-03 16:44:24" }, // { "country": "AT", "code": "50161321", "url": "https://www.ikea.com/at/de/p/hol-aufbewahrungstisch-akazie-50161321/", "name": "HOL", "typeName": "Aufbewahrungstisch", "mainImageUrl": "https://www.ikea.com/at/de/images/products/hol-aufbewahrungstisch-akazie__0104310_pe251255_s5.jpg", "itemNoGlobal": "50161321", "salesPrice": "80.99", "tag": "FAMILY_PRICE", "last_mod": "2023-12-03 16:44:24" },