Bug in process Request
This commit is contained in:
@@ -10,8 +10,103 @@ use Illuminate\Http\Client\Pool;
|
|||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
class CountryCompareController extends Controller
|
||||||
|
{
|
||||||
|
private $countries = [];
|
||||||
|
private $cHash = [];
|
||||||
|
private $dom = null;
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$countries = [];
|
||||||
|
$cHash = [];
|
||||||
|
CountryCode::active('Y')->each(function ($country) use (&$countries, &$cHash) {
|
||||||
|
$countries[] = $country;
|
||||||
|
$cHash[$country->country_name] = $country->country_code;
|
||||||
|
});
|
||||||
|
|
||||||
function parseJson($json_raw, $country)
|
$this->countries = collect($countries);
|
||||||
|
$this->cHash = collect($cHash);
|
||||||
|
$this->dom = new DOMDocument();
|
||||||
|
}
|
||||||
|
public function search(Request $request)
|
||||||
|
{
|
||||||
|
$itemCodes = $request->input("item_codes");
|
||||||
|
$ta_codes = explode(' ', str_replace('.', '', trim($itemCodes)));
|
||||||
|
|
||||||
|
$requests = $this->makeRequests($ta_codes[0]);
|
||||||
|
return $this->processResponse($requests,$ta_codes[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function compare($countries, $cHash, $aCodes){
|
||||||
|
$requests = $this->makeRequests($aCodes);
|
||||||
|
return $this->processResponse($requests, $aCodes);
|
||||||
|
}
|
||||||
|
public function makeRequests($ta_code)
|
||||||
|
{
|
||||||
|
$requests = Http::pool(fn (Pool $pool) => [
|
||||||
|
$this->countries->each(function ($country) use ($pool, $ta_code) {
|
||||||
|
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 $requests;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function makeRequest($ta_code, $country)
|
||||||
|
{
|
||||||
|
$country = CountryCode::where('country_name', $country)->first();
|
||||||
|
return Http::pool(fn (Pool $pool) => [
|
||||||
|
$pool->as($country->country_name)->get($country->search_url . $ta_code)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function processResponse($responses, $code)
|
||||||
|
{
|
||||||
|
$code = $code[0];
|
||||||
|
$prices = [];
|
||||||
|
|
||||||
|
|
||||||
|
foreach ($this->countries as $c) {
|
||||||
|
|
||||||
|
$country = $c->country_code;
|
||||||
|
|
||||||
|
if ($responses[$country] instanceof \Illuminate\Http\Client\Response && $responses[$country]->ok()) {
|
||||||
|
|
||||||
|
// dd($responses);
|
||||||
|
$response = (string) $responses[$country]->body();
|
||||||
|
//dd($response)
|
||||||
|
switch ($country) {
|
||||||
|
case "BG":
|
||||||
|
case "CY":
|
||||||
|
case "GR":
|
||||||
|
$prices[$country] = $this->parseJson_CY_GR_BG($code, (string) $response, $country);
|
||||||
|
break;
|
||||||
|
case "IS":
|
||||||
|
$prices[$country] = $this->parseJson_IS($code,(string) $response, $country);
|
||||||
|
break;
|
||||||
|
case "TR":
|
||||||
|
$prices[$country] = $this->parseJson_TR((string) $response, $country);
|
||||||
|
break;
|
||||||
|
case "EE":
|
||||||
|
case "LT":
|
||||||
|
case "LV":
|
||||||
|
$prices[$country] = $this->parseJson_EE_LT_LV($code, (string) $response, $country);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$prices[$country] = $this->parseJson((string) $response, $country);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$products = [];
|
||||||
|
collect($prices)->each(function ($price) use (&$products) {
|
||||||
|
if (count($price))
|
||||||
|
$products[] = $price;
|
||||||
|
});
|
||||||
|
return collect($products);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parseJson($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]);
|
||||||
@@ -40,12 +135,12 @@ function parseJson($json_raw, $country)
|
|||||||
return $json_values;
|
return $json_values;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseJson_TR($body,$country)
|
public function parseJson_TR($body,$country)
|
||||||
{
|
{
|
||||||
//echo "country: " . $country . ", code: " . $code . "<br>";
|
//echo "country: " . $country . ", code: " . $code . "<br>";
|
||||||
|
|
||||||
$dochtml = new DOMDocument("1.0", "UTF-8");
|
$dochtml = new DOMDocument();
|
||||||
@$dochtml->loadHTML( $body);
|
@$dochtml->loadHTML( $body,LIBXML_NOERROR);
|
||||||
|
|
||||||
@$xpath = new DOMXpath($dochtml);
|
@$xpath = new DOMXpath($dochtml);
|
||||||
$price = null;
|
$price = null;
|
||||||
@@ -67,14 +162,14 @@ function parseJson($json_raw, $country)
|
|||||||
return $json_values;
|
return $json_values;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
@$dochtml->loadHTML($body,LIBXML_NOERROR);
|
||||||
|
|
||||||
@$xpath = new DOMXpath($dochtml);
|
@$xpath = new DOMXpath($dochtml);
|
||||||
$price = null;
|
$price = null;
|
||||||
@@ -97,11 +192,11 @@ function parseJson($json_raw, $country)
|
|||||||
return $json_values;
|
return $json_values;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseJson_EE_LT_LV($code, $body, $country)
|
public function parseJson_EE_LT_LV($code, $body, $country)
|
||||||
{
|
{
|
||||||
//echo "country: " . $country . ", code: " . $code . "<br>";
|
//echo "country: " . $country . ", code: " . $code . "<br>";
|
||||||
|
|
||||||
$dochtml = new DOMDocument("1.0", "UTF-8");
|
$dochtml = new DOMDocument();
|
||||||
@$dochtml->loadHTML( $body);
|
@$dochtml->loadHTML( $body);
|
||||||
|
|
||||||
@$xpath = new DOMXpath($dochtml);
|
@$xpath = new DOMXpath($dochtml);
|
||||||
@@ -145,30 +240,27 @@ function parseJson($json_raw, $country)
|
|||||||
return $json_values;
|
return $json_values;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseJson_IS($code, $body, $country)
|
public function parseJson_IS($code, $body, $country)
|
||||||
{
|
{
|
||||||
//echo "country: " . $country . ", code: " . $code . "<br>";
|
//echo "country: " . $country . ", code: " . $code . "<br>";
|
||||||
|
|
||||||
//$body = file_get_contents("https://ikea.is/is/search/?q=80366284");
|
$dochtml = new DOMDocument();
|
||||||
// $client = new \GuzzleHttp\Client();
|
|
||||||
// $res = $client->request('GET', 'https://ikea.is/is/search/?q=80366284');
|
|
||||||
// $body= $res->getBody()->getContents();
|
|
||||||
$dochtml = new DOMDocument("1.0", "UTF-8");
|
|
||||||
@$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();
|
||||||
|
|
||||||
// try {
|
// try {
|
||||||
@$price = $xpath->query('//*/div[(@class="itemPriceBox")]//p[@class="itemNormalPrice revamp_price price"]/span/span')[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)) {
|
||||||
@$price = $xpath->query('//*/div[(@class="itemPriceBox")]//p[@class="itemBTI display-6 revamp_price price"]/span/span')[0]->nodeValue;
|
@$price = $xpath->query('//*/div[(@class="itemPriceBox")]//p[@class="itemBTI display-6 revamp_price price"]/span/span')->item(0)->nodeValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@$url_product = "https://www.ikea.is" . $xpath->query('/html/head/meta[(@property="og:url")]/@content')[0]->nodeValue;
|
$url_product = "https://www.ikea.is" . $xpath->query('/html/head/meta[(@property="og:url")]/@content')[0]->nodeValue;
|
||||||
|
|
||||||
//echo "url_product: " . $url_product . "<br>";
|
//echo "url_product: " . $url_product . "<br>";
|
||||||
|
|
||||||
@@ -192,98 +284,5 @@ function parseJson($json_raw, $country)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class CountryCompareController extends Controller
|
|
||||||
{
|
|
||||||
private $countries = [];
|
|
||||||
private $cHash = [];
|
|
||||||
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$countries = [];
|
|
||||||
$cHash = [];
|
|
||||||
CountryCode::active('Y')->each(function ($country) use (&$countries, &$cHash) {
|
|
||||||
$countries[] = $country;
|
|
||||||
$cHash[$country->country_name] = $country->country_code;
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->countries = collect($countries);
|
|
||||||
$this->cHash = collect($cHash);
|
|
||||||
}
|
|
||||||
public function search(Request $request)
|
|
||||||
{
|
|
||||||
$itemCodes = $request->input("item_codes");
|
|
||||||
$ta_codes = explode(' ', str_replace('.', '', trim($itemCodes)));
|
|
||||||
|
|
||||||
$requests = $this->makeRequests($ta_codes[0]);
|
|
||||||
return $this->processResponse($requests,$ta_codes[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function compare($countries, $cHash, $aCodes){
|
|
||||||
$requests = $this->makeRequests($aCodes);
|
|
||||||
return $this->processResponse($requests, $aCodes);
|
|
||||||
}
|
|
||||||
public function makeRequests($ta_code)
|
|
||||||
{
|
|
||||||
$requests = Http::pool(fn (Pool $pool) => [
|
|
||||||
$this->countries->each(function ($country) use ($pool, $ta_code) {
|
|
||||||
Log::info('Getting page {url}', [ 'url' => $country->search_url . $ta_code[0] ] );
|
|
||||||
return $pool->as($country->country_code)->get($country->search_url . $ta_code[0] );
|
|
||||||
})
|
|
||||||
]);
|
|
||||||
return $requests;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function makeRequest($ta_code, $country)
|
|
||||||
{
|
|
||||||
$country = CountryCode::where('country_name', $country)->first();
|
|
||||||
return Http::pool(fn (Pool $pool) => [
|
|
||||||
$pool->as($country->country_name)->get($country->search_url . $ta_code)
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function processResponse($responses, $code)
|
|
||||||
{
|
|
||||||
$prices = [];
|
|
||||||
|
|
||||||
foreach ($this->countries as $c) {
|
|
||||||
|
|
||||||
$country = $c->country_code;
|
|
||||||
|
|
||||||
if ($responses[$country] instanceof \Illuminate\Http\Client\Response && $responses[$country]->ok()) {
|
|
||||||
|
|
||||||
// dd($responses);
|
|
||||||
$response = (string) $responses[$country]->body();
|
|
||||||
//dd($response)
|
|
||||||
switch ($country) {
|
|
||||||
case "BG":
|
|
||||||
case "CY":
|
|
||||||
case "GR":
|
|
||||||
$prices[$country] = parseJson_CY_GR_BG($code, (string) $response, $country);
|
|
||||||
break;
|
|
||||||
case "IS":
|
|
||||||
$prices[$country] = parseJson_IS($code,(string) $response, $country);
|
|
||||||
break;
|
|
||||||
case "TR":
|
|
||||||
$prices[$country] = parseJson_TR((string) $response, $country);
|
|
||||||
break;
|
|
||||||
case "EE":
|
|
||||||
case "LT":
|
|
||||||
case "LV":
|
|
||||||
$prices[$country] = parseJson_EE_LT_LV($code, (string) $response, $country);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
$prices[$country] = parseJson((string) $response, $country);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$products = [];
|
|
||||||
collect($prices)->each(function ($price) use (&$products) {
|
|
||||||
if (count($price))
|
|
||||||
$products[] = $price;
|
|
||||||
});
|
|
||||||
return collect($products);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// { "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" },
|
||||||
|
|||||||
@@ -125,5 +125,6 @@
|
|||||||
"referenceerror: ccountry is not defined": "ReferenceError: ccountry is not defined",
|
"referenceerror: ccountry is not defined": "ReferenceError: ccountry is not defined",
|
||||||
"typeerror: invalid assignment to const 'response'": "TypeError: invalid assignment to const 'response'",
|
"typeerror: invalid assignment to const 'response'": "TypeError: invalid assignment to const 'response'",
|
||||||
"error: request aborted": "Error: Request aborted",
|
"error: request aborted": "Error: Request aborted",
|
||||||
"typeerror: can't access property \"code\", form.country is null": "TypeError: can't access property \"code\", form.country is null"
|
"typeerror: can't access property \"code\", form.country is null": "TypeError: can't access property \"code\", form.country is null",
|
||||||
|
"error: request failed with status code 504": "Error: Request failed with status code 504"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user