327 lines
10 KiB
PHP
327 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use GuzzleHttp\Client as GuzzleClient;
|
|
use GuzzleHttp\Promise as GuzzlePromise;
|
|
|
|
use App\Models\CountryCode;
|
|
|
|
function parseJson($json_raw)
|
|
{
|
|
$json_values = array();
|
|
|
|
$json_decoded = json_decode($json_raw, true);
|
|
|
|
$searchResultPage = $json_decoded["searchResultPage"];
|
|
$products = $searchResultPage["products"];
|
|
$main = $products["main"];
|
|
$items = $main["items"];
|
|
|
|
foreach ($items as $item) {
|
|
$product = $item["product"];
|
|
|
|
$salesPrice = $product["salesPrice"];
|
|
$numeral = $salesPrice["numeral"];
|
|
|
|
$json_values = array(
|
|
'url_product' => $product['pipUrl'],
|
|
'tag' => $product['tag'],
|
|
'price' => $numeral,
|
|
'price_eur' => 0
|
|
);
|
|
|
|
break;
|
|
}
|
|
|
|
return $json_values;
|
|
}
|
|
|
|
function parseJson_TR($body)
|
|
{
|
|
//echo "country: " . $country . ", code: " . $code . "<br>";
|
|
|
|
$dochtml = new \DOMDocument();
|
|
$dochtml->loadHTML($body);
|
|
|
|
$xpath = new \DOMXpath($dochtml);
|
|
$price = null;
|
|
//$c = ltrim($code, "0");
|
|
$json_values = array();
|
|
|
|
$url_product = $xpath->query('/html/head/meta[(@property="og:url")]/@content')[0]->nodeValue;
|
|
$price = $xpath->query('/html/head/meta[(@property="og:price:amount")]/@content')[0]->nodeValue;
|
|
|
|
$price = floatval(trim(str_replace('.', '', $price)));
|
|
|
|
$json_values = array(
|
|
'url_product' => $url_product,
|
|
'tag' => null,
|
|
'price' => floatval($price),
|
|
'price_eur' => 0
|
|
);
|
|
|
|
return $json_values;
|
|
}
|
|
|
|
function parseJson_CY_GR_BG($code, $body)
|
|
{
|
|
//echo "country: " . $country . ", code: " . $code . "<br>";
|
|
|
|
$dochtml = new \DOMDocument();
|
|
$dochtml->loadHTML($body);
|
|
|
|
$xpath = new \DOMXpath($dochtml);
|
|
$price = null;
|
|
$c = ltrim($code, "0");
|
|
$json_values = array();
|
|
|
|
$price = $xpath->query('//*/div[(@class="yotpo yotpo-main-widget" and @data-product-id="' . $code . '")]/@data-price')[0]->nodeValue;
|
|
$url_product = $xpath->query('//*/div[(@class="yotpo yotpo-main-widget" and @data-product-id="' . $code . '")]/@data-url')[0]->nodeValue;
|
|
|
|
$price = floatval($price);
|
|
|
|
|
|
$json_values = array(
|
|
'url_product' => $url_product,
|
|
'tag' => null,
|
|
'price' => floatval($price),
|
|
'price_eur' => 0
|
|
);
|
|
|
|
return $json_values;
|
|
}
|
|
|
|
function parseJson_EE_LT_LV($country, $code, $body)
|
|
{
|
|
//echo "country: " . $country . ", code: " . $code . "<br>";
|
|
|
|
$dochtml = new \DOMDocument();
|
|
$dochtml->loadHTML($body);
|
|
|
|
$xpath = new \DOMXpath($dochtml);
|
|
$price = null;
|
|
$c = ltrim($code, "0");
|
|
$json_values = array();
|
|
|
|
$price = $xpath->query('//*/div[(@class="itemPriceBox" and @data-item="' . $c . '")]//p[@class="itemNormalPrice display-6"]/span')[0]->nodeValue;
|
|
|
|
if (is_null($price) || empty($price)) {
|
|
$price = $xpath->query('//*/div[(@class="itemPriceBox" and @data-item="' . $c . '")]//div[@class="itemBTI display-6"]/span')[0]->nodeValue;
|
|
}
|
|
|
|
switch ($country) {
|
|
case "Estonia":
|
|
$url_product = "https://www.ikea.ee" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
|
|
break;
|
|
case "Lithuania":
|
|
$url_product = "https://www.ikea.lt" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
|
|
break;
|
|
case "Latvia":
|
|
$url_product = "https://www.ikea.lv" . $xpath->query('//*/div[(@class="card-header")]/a/@href')[0]->nodeValue;
|
|
break;
|
|
default:
|
|
$url_product = null;
|
|
}
|
|
|
|
if (is_null($price) || empty($price)) {
|
|
$url_product = null;
|
|
}
|
|
|
|
$price = floatval(trim(str_replace(' ', '', str_replace(',', '.', str_replace(array('€'), '', $price)))));
|
|
|
|
$json_values = array(
|
|
'url_product' => $url_product,
|
|
//$url_product,
|
|
'tag' => null,
|
|
'price' => $price,
|
|
'price_eur' => 0
|
|
);
|
|
|
|
return $json_values;
|
|
}
|
|
|
|
function parseJson_IS($country, $code, $body)
|
|
{
|
|
//echo "country: " . $country . ", code: " . $code . "<br>";
|
|
|
|
$dochtml = new \DOMDocument();
|
|
$dochtml->loadHTML($body);
|
|
|
|
$xpath = new \DOMXpath($dochtml);
|
|
$price = null;
|
|
$c = ltrim($code, "0");
|
|
$json_values = array();
|
|
|
|
$price = $xpath->query('//*/div[(@class="itemPriceBox")]//p[@class="itemNormalPrice revamp_price price"]/span/span')[0]->nodeValue;
|
|
|
|
if (is_null($price) || empty($price)) {
|
|
$price = $xpath->query('//*/div[(@class="itemPriceBox")]//p[@class="itemBTI display-6 revamp_price price"]/span/span')[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>";
|
|
|
|
if (is_null($price) || empty($price)) {
|
|
$url_product = null;
|
|
}
|
|
|
|
$price = floatval(trim(str_replace(' ', '', str_replace(',', '.', str_replace('.', '', str_replace(array('€'), '', $price))))));
|
|
|
|
$json_values = array(
|
|
'url_product' => $url_product,
|
|
//$url_product,
|
|
'tag' => null,
|
|
'price' => $price,
|
|
'price_eur' => 0
|
|
);
|
|
|
|
return $json_values;
|
|
}
|
|
|
|
class OnlineCompareController extends Controller
|
|
{
|
|
private $countries = [];
|
|
public function __construct()
|
|
{
|
|
$countries = [];
|
|
CountryCode::all()->each(function ($country) use (&$countries) {
|
|
$countries[] = $country->country_name;
|
|
});
|
|
|
|
$this->countries = collect($countries);
|
|
}
|
|
public function compare()
|
|
{
|
|
// $codes = $request->input("codes");
|
|
// $countries = $request->input("countries");
|
|
// $currency = $request->input("currency");
|
|
$codes = ['00102452',];
|
|
$countries = ['Austria', 'Belgium'];
|
|
$ta_codes = $codes;
|
|
$exch_rates = "EUR";
|
|
|
|
$item = array();
|
|
$codes = array();
|
|
|
|
// krajiny ktore som pridal do zoznamu z DB
|
|
$added_countries[] = null;
|
|
|
|
// loop cez jednotlive code z textarea a pridanie url
|
|
/* ******************************************************************** */
|
|
foreach ($ta_codes as $code) {
|
|
unset($item);
|
|
|
|
if (!is_numeric($code) || strlen($code) < 6) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($countries as $country) {
|
|
|
|
if (!in_array($country['country_name'], $added_countries)) {
|
|
|
|
$item[] = array(
|
|
'country_name' => $country['country_name'],
|
|
'url_call' => $country['search_url'] . $code,
|
|
'price' => 0,
|
|
'url_product' => null,
|
|
'tag' => null
|
|
);
|
|
}
|
|
}
|
|
|
|
$codes[] = array(
|
|
'code' => $code,
|
|
'item' => $item
|
|
);
|
|
}
|
|
//echo var_dump($codes);
|
|
|
|
/* ******************************************************************** */
|
|
|
|
|
|
|
|
// paralelene prevolanie URL
|
|
/* ******************************************************************** */
|
|
$client = new GuzzleClient(['timeout' => 12.0]);
|
|
|
|
$promises[] = null;
|
|
$prices[] = null;
|
|
|
|
foreach ($codes as $x => $code) {
|
|
//echo "CODE: " . $code_value['code'] . "<BR>";
|
|
|
|
unset($promises);
|
|
unset($responses);
|
|
unset($prices);
|
|
|
|
// naplnenie pola promises URL
|
|
foreach ($code['item'] as $i => $i_value) {
|
|
if (!is_null($i_value['url_call'])) {
|
|
$promises[$i_value['country_name']] = $client->getAsync($i_value['url_call']);
|
|
}
|
|
}
|
|
|
|
$responses = GuzzlePromise\settle($promises)->wait();
|
|
|
|
foreach ($responses as $country => $response) {
|
|
|
|
if ($response['state'] === 'fulfilled') {
|
|
$response_value = $response['value'];
|
|
|
|
if ($response_value->getStatusCode() == 200) {
|
|
|
|
switch ($country) {
|
|
case "Bulgaria":
|
|
case "Cyprus":
|
|
case "Greece":
|
|
$prices[$country] = parseJson_CY_GR_BG($code['code'], (string) $response_value->body());
|
|
break;
|
|
case "Iceland":
|
|
$prices[$country] = parseJson_IS($country, $code['code'], (string) $response_value->body());
|
|
break;
|
|
case "Turkey":
|
|
$prices[$country] = parseJson_TR((string) $response_value->body());
|
|
break;
|
|
case "Lithuania":
|
|
case "Estonia":
|
|
case "Latvia":
|
|
$prices[$country] = parseJson_EE_LT_LV($country, $code['code'], (string) $response_value->body());
|
|
break;
|
|
default:
|
|
$prices[$country] = parseJson((string) $response_value->body());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/* {"products":{"Illuminate\\Database\\Eloquent\\Collection":[{"country":"AT","code":"00222671","url":"https://www.ikea.com/at
|
|
/de/p/vardoe-bettkasten-weiss-00222671/","name":"VARDÖ","typeName":"Bettkasten","mainImageUrl":"https://www.ikea.com/at/de/images/products/vardoe-bettkasten-weiss__0636
|
|
223_pe697736_s5.jpg","itemNoGlobal":"00222671","salesPrice":"34.99","tag":"NONE","last_mod":"2024-01-04 15:41:48","mainImageAlt":"VARDÖ Bettkasten, weiß, 65x70 cm","pri
|
|
ceUnit":null,"countryName":"Austria"} */
|
|
|
|
// zistenu cenu doplnim do kolekcie
|
|
foreach ($code['item'] as $i => $i_value) {
|
|
asort($codes[$x]['item']); // zotriedenie
|
|
$codes[$x]['item'][$i]['salesPrice'] = $prices[$i_value['country_name']]['price'] ?? 0;
|
|
|
|
|
|
if ($prices[$i_value['country_name']]['tag'] == 'NONE') {
|
|
$codes[$x]['item'][$i]['tag'] = "";
|
|
} else {
|
|
$codes[$x]['item'][$i]['tag'] = $prices[$i_value['country_name']]['tag'];
|
|
}
|
|
|
|
$codes[$x]['item'][$i]['url'] = $prices[$i_value['country_name']]['url_product'];
|
|
}
|
|
}
|
|
/* ******************************************************************** */
|
|
|
|
|
|
// vypis do tabulky
|
|
/* ******************************************************************** */
|
|
return $codes;
|
|
}
|
|
}
|