Basic online search with bugs...

This commit is contained in:
2024-02-04 19:35:22 +01:00
parent d1f5810635
commit 354ac8a242
11 changed files with 549 additions and 96 deletions

View File

@@ -12,15 +12,19 @@ use PHPHtmlParser\Dom;
class CountryCompareController extends Controller
{
private $countries = [];
private $cHash = [];
public function __construct()
{
$countries = [];
CountryCode::all()->each(function ($country) use (&$countries) {
$countries[] = $country->country_name;
$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)
{
@@ -31,13 +35,16 @@ class CountryCompareController extends Controller
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) => [
CountryCode::all()->each(function ($countryCode) use ($pool, $ta_code) {
$this->countries[] = $countryCode->country_name;
Log::info('Getting page {url}', [ 'url' => $countryCode->search_url . $ta_code ] );
return $pool->as($countryCode->country_name)->get($countryCode->search_url . $ta_code);
$this->countries->each(function ($country) use ($pool, $ta_code) {
Log::info('Getting page {url}', [ 'url' => $country->search_url . $ta_code ] );
return $pool->as($country->country_code)->get($country->search_url . $ta_code);
})
]);
return $requests;
@@ -45,9 +52,9 @@ class CountryCompareController extends Controller
public function makeRequest($ta_code, $country)
{
$countryCode = CountryCode::where('country_name', $country)->first();
$country = CountryCode::where('country_name', $country)->first();
return Http::pool(fn (Pool $pool) => [
$pool->as($countryCode->country_name)->get($countryCode->search_url . $ta_code)
$pool->as($country->country_name)->get($country->search_url . $ta_code)
]);
}
@@ -55,38 +62,49 @@ class CountryCompareController extends Controller
{
$prices = [];
foreach ($this->countries as $country) {
foreach ($this->countries as $c) {
if (isset($responses[$country]) && $responses[$country]->ok()) {
$country = $c->country_code;
if ($responses[$country] instanceof \Illuminate\Http\Client\Response && $responses[$country]->ok()) {
$response = $responses[$country]->body();
switch ($country) {
case "Bulgaria":
case "Cyprus":
case "Greece":
$prices[$country] = $this->parseJson_CY_GR_BG($code, (string) $response);
case "BG":
case "BY":
case "GR":
$prices[$country] = $this->parseJson_CY_GR_BG($code, (string) $response, $country);
break;
case "Iceland":
$prices[$country] = $this->parseJson_IS($country, $code, (string) $response);
case "IS":
$prices[$country] = $this->parseJson_IS($code, (string) $response, $country);
break;
case "Türkiye":
$prices[$country] = $this->parseJson_TR((string) $response);
case "TR":
$prices[$country] = $this->parseJson_TR((string) $response, $country);
break;
case "Lithuania":
case "Estonia":
case "Latvia":
$prices[$country] = $this->parseJson_EE_LT_LV($country, $code, (string) $response);
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);
$prices[$country] = $this->parseJson((string) $response, $country);
}
}
}
return $prices;
$products = [];
collect($prices)->each(function ($price) use (&$products) {
if (count($price))
$products[] = $price;
});
return collect($products);
}
public function parseJson($json_raw)
// { "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" },
public function parseJson($json_raw, $country)
{
try {
$json_values = array();
$json_decoded = json_decode($json_raw, true);
@@ -103,18 +121,21 @@ class CountryCompareController extends Controller
$numeral = $salesPrice["numeral"];
$json_values = array(
'url_product' => $product['pipUrl'],
'url' => $product['pipUrl'],
'tag' => $product['tag'],
'price' => $numeral,
'price_eur' => 0
'country' => $country,
'salesPrice' => $numeral,
);
break;
}
} catch (\Exception $e) {
return [];
}
return $json_values;
}
public function parseJson_TR($body)
public function parseJson_TR($body,$country)
{
//echo "country: " . $country . ", code: " . $code . "<br>";
@@ -132,16 +153,16 @@ class CountryCompareController extends Controller
$price = floatval(trim(str_replace('.', '', $price)));
$json_values = array(
'url_product' => $url_product,
'url' => $url_product,
'tag' => null,
'price' => floatval($price),
'price_eur' => 0
'country' => $country,
);
return $json_values;
}
public function parseJson_CY_GR_BG($code, $body)
public function parseJson_CY_GR_BG($code, $body , $country)
{
//echo "country: " . $country . ", code: " . $code . "<br>";
@@ -163,16 +184,16 @@ class CountryCompareController extends Controller
$json_values = array(
'url_product' => $url_product,
'url' => $url_product,
'tag' => null,
'price' => floatval($price),
'price_eur' => 0
'country' => $country,
);
return $json_values;
}
public function parseJson_EE_LT_LV($country, $code, $body)
public function parseJson_EE_LT_LV($code, $body, $country)
{
//echo "country: " . $country . ", code: " . $code . "<br>";
@@ -192,13 +213,13 @@ class CountryCompareController extends Controller
}
switch ($country) {
case "Estonia":
case "EE":
$url_product = "https://www.ikea.ee" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
break;
case "Lithuania":
case "LT":
$url_product = "https://www.ikea.lt" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
break;
case "Latvia":
case "LV":
$url_product = "https://www.ikea.lv" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
break;
default:
@@ -215,17 +236,16 @@ class CountryCompareController extends Controller
$price = floatval(trim(str_replace(' ', '', str_replace(',', '.', str_replace(array('€'), '', $price)))));
$json_values = array(
'url_product' => $url_product,
//$url_product,
'url' => $url_product,
'tag' => null,
'price' => $price,
'price_eur' => 0
'country' => $country,
);
return $json_values;
}
public function parseJson_IS($country, $code, $body)
public function parseJson_IS($code, $body, $country)
{
//echo "country: " . $country . ", code: " . $code . "<br>";
@@ -258,11 +278,10 @@ class CountryCompareController extends Controller
$price = floatval(trim(str_replace(' ', '', str_replace(',', '.', str_replace('.', '', str_replace(array('€'), '', $price))))));
$json_values = array(
'url_product' => $url_product,
//$url_product,
'url' => $url_product,
'tag' => null,
'price' => $price,
'price_eur' => 0
'country' => $country,
);
return $json_values;