Add FetchGoldRates job to retrieve and store gold rates
- Introduced a new job class `FetchGoldRates` to handle the fetching of gold rates from an external source. - Implemented data extraction using DOM parsing to retrieve relevant gold rate information. - Utilized `NumberFormatter` for consistent number formatting of the fetched values. - Stored the processed gold rates in a JSON file for easy access and further processing. - Enhanced the maintainability of the code by organizing the fetching logic and data handling. This commit establishes a foundation for integrating gold rate data into the application.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Helpers\NumberFormatter;
|
||||
|
||||
class FetchGoldRates implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$data = [];
|
||||
|
||||
// Altın kurları
|
||||
$goldResponse = $this->fetchData('https://altin.doviz.com');
|
||||
|
||||
|
||||
$domGold = new \DOMDocument();
|
||||
@$domGold->loadHTML($goldResponse->body());
|
||||
|
||||
$xpathGold = new \DOMXPath($domGold);
|
||||
|
||||
$elementsGold = $xpathGold->query("//*[@data-socket-key]");
|
||||
|
||||
foreach ($elementsGold as $element) {
|
||||
$attrGold = $element->getAttribute('data-socket-key');
|
||||
$typeGold = $element->getAttribute('data-socket-attr');
|
||||
$valueGold = NumberFormatter::commaToDot($element->nodeValue);
|
||||
|
||||
if (trim($attrGold) !== '') {
|
||||
$nameGold = strtoupper(str_replace("-", "", $attrGold));
|
||||
$nameGold = str_replace("14", "OD", $nameGold);
|
||||
$nameGold = str_replace("18", "OS", $nameGold);
|
||||
$nameGold = str_replace("22", "YI", $nameGold);
|
||||
$nameGold = str_replace("gram-altin", "GRA", $nameGold);
|
||||
$nameGold = str_replace("gramaltin", "GRA", $nameGold);
|
||||
$nameGold = str_replace("gramplatin", "GPL", $nameGold);
|
||||
$nameGold = str_replace("gramhasaltin", "HAS", $nameGold);
|
||||
$nameGold = strtoupper(substr($nameGold, 0, 3));
|
||||
|
||||
$except = ['USD', 'EUR', 'GBP', 'XU1', 'BIT'];
|
||||
if (in_array($nameGold, $except)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($typeGold == "bid") $data[$nameGold]['Buying'] = (float)$valueGold;
|
||||
if ($typeGold == "ask") $data[$nameGold]['Selling'] = (float)$valueGold;
|
||||
if ($typeGold == "s") $data[$nameGold]['Selling'] = (float)$valueGold;
|
||||
if ($typeGold == "c") $data[$nameGold]['Change'] = round((float)$valueGold,
|
||||
2);
|
||||
$data[$nameGold]['Type'] = "Gold";
|
||||
}
|
||||
}
|
||||
|
||||
// JSON dosyasını kaydet
|
||||
Storage::put('gold/today.json', json_encode($data, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function fetchData($url)
|
||||
{
|
||||
return Http::withHeaders([
|
||||
'User-Agent' => 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10',
|
||||
'Accept-Language' => 'en'
|
||||
])->get($url);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user