Compare contoller, commands for search articles from command line
This commit is contained in:
40
app/Console/Commands/IkeaPrice.php
Normal file
40
app/Console/Commands/IkeaPrice.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Http\Controllers\CountryCompareController;
|
||||
|
||||
class IkeaPrice extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'ikea:price {country} {article}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Get Ikea Prices';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(CountryCompareController $countryCompareController)
|
||||
{
|
||||
$article = $this->argument('article');
|
||||
$country = $this->argument('country');
|
||||
|
||||
$responses = $countryCompareController->makeRequest($article,$country);
|
||||
$prices = $countryCompareController->processResponse($responses,$article);
|
||||
//dd($prices);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
38
app/Console/Commands/IkeaPrices.php
Normal file
38
app/Console/Commands/IkeaPrices.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Http\Controllers\CountryCompareController;
|
||||
|
||||
class IkeaPrices extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'ikea:prices {article}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Get Ikea Prices';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle(CountryCompareController $countryCompareController)
|
||||
{
|
||||
$article = $this->argument('article');
|
||||
$responses = $countryCompareController->makeRequests($article);
|
||||
$prices = $countryCompareController->processResponse($responses,$article);
|
||||
//dd($prices);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
app/Http/Controllers/.DS_Store
vendored
Normal file
BIN
app/Http/Controllers/.DS_Store
vendored
Normal file
Binary file not shown.
269
app/Http/Controllers/CountryCompareController.php
Normal file
269
app/Http/Controllers/CountryCompareController.php
Normal file
@@ -0,0 +1,269 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\CountryCode;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Client\Pool;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use PHPHtmlParser\Dom;
|
||||
|
||||
class CountryCompareController 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 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 makeRequests($ta_code)
|
||||
{
|
||||
$requests = Http::pool(fn (Pool $pool) => [
|
||||
CountryCode::all()->each(function ($countryCode) use ($pool, $ta_code) {
|
||||
$this->countries[] = $countryCode->country_name;
|
||||
return $pool->as($countryCode->country_name)->get($countryCode->search_url . $ta_code);
|
||||
})
|
||||
]);
|
||||
return $requests;
|
||||
}
|
||||
|
||||
public function makeRequest($ta_code, $country)
|
||||
{
|
||||
$countryCode = CountryCode::where('country_name', $country)->first();
|
||||
return Http::pool(fn (Pool $pool) => [
|
||||
$pool->as($countryCode->country_name)->get($countryCode->search_url . $ta_code)
|
||||
]);
|
||||
}
|
||||
|
||||
public function processResponse($responses, $code)
|
||||
{
|
||||
$prices = [];
|
||||
|
||||
foreach ($this->countries as $country) {
|
||||
|
||||
if (isset($responses[$country]) && $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);
|
||||
break;
|
||||
case "Iceland":
|
||||
$prices[$country] = $this->parseJson_IS($country, $code, (string) $response);
|
||||
break;
|
||||
case "Türkiye":
|
||||
$prices[$country] = $this->parseJson_TR((string) $response);
|
||||
break;
|
||||
case "Lithuania":
|
||||
case "Estonia":
|
||||
case "Latvia":
|
||||
$prices[$country] = $this->parseJson_EE_LT_LV($country, $code, (string) $response);
|
||||
break;
|
||||
default:
|
||||
$prices[$country] = $this->parseJson((string) $response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $prices;
|
||||
}
|
||||
public function parseJson($json_raw)
|
||||
{
|
||||
$json_values = array();
|
||||
|
||||
$json_decoded = json_decode($json_raw, true);
|
||||
//dd($json_decoded);
|
||||
$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;
|
||||
}
|
||||
|
||||
public function parseJson_TR($body)
|
||||
{
|
||||
//echo "country: " . $country . ", code: " . $code . "<br>";
|
||||
|
||||
$dochtml = new \DOMDocument();
|
||||
@$dochtml->loadHTML('<?xml encoding="UTF-8">' . $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;
|
||||
}
|
||||
|
||||
public function parseJson_CY_GR_BG($code, $body)
|
||||
{
|
||||
//echo "country: " . $country . ", code: " . $code . "<br>";
|
||||
|
||||
$dochtml = new \DOMDocument();
|
||||
@$dochtml->loadHTML('<?xml encoding="UTF-8">' . $body);
|
||||
|
||||
$xpath = new \DOMXpath($dochtml);
|
||||
$price = null;
|
||||
$c = ltrim($code, "0");
|
||||
$json_values = array();
|
||||
|
||||
try {
|
||||
$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;
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
$price = floatval($price);
|
||||
|
||||
|
||||
$json_values = array(
|
||||
'url_product' => $url_product,
|
||||
'tag' => null,
|
||||
'price' => floatval($price),
|
||||
'price_eur' => 0
|
||||
);
|
||||
|
||||
return $json_values;
|
||||
}
|
||||
|
||||
public function parseJson_EE_LT_LV($country, $code, $body)
|
||||
{
|
||||
//echo "country: " . $country . ", code: " . $code . "<br>";
|
||||
|
||||
$dochtml = new \DOMDocument();
|
||||
@$dochtml->loadHTML('<?xml encoding="UTF-8">' . $body);
|
||||
|
||||
$xpath = new \DOMXpath($dochtml);
|
||||
$price = null;
|
||||
$c = ltrim($code, "0");
|
||||
$json_values = array();
|
||||
|
||||
try {
|
||||
$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;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function parseJson_IS($country, $code, $body)
|
||||
{
|
||||
//echo "country: " . $country . ", code: " . $code . "<br>";
|
||||
|
||||
$dochtml = new \DOMDocument();
|
||||
@$dochtml->loadHTML('<?xml encoding="UTF-8">' . $body);
|
||||
|
||||
$xpath = new \DOMXpath($dochtml);
|
||||
$price = null;
|
||||
$c = ltrim($code, "0");
|
||||
$json_values = array();
|
||||
|
||||
try {
|
||||
$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;
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user