Files
finance/app/Jobs/FetchGoldRates.php
Ümit Tunç 7f09c56967 Enhance FetchGoldRates job to include update date and improve gold name formatting
- Added an 'Update_Date' field to store the current timestamp when fetching gold rates.
- Refactored gold name formatting to ensure consistency by converting names to uppercase and replacing specific substrings with standardized abbreviations.
- Improved maintainability by using a full name variable for gold types before abbreviation.

These changes enhance the data structure and clarity of the gold rates fetched by the application.
2025-01-17 23:28:17 +03:00

79 lines
2.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 = [];
$data['Update_Date'] = now()->format('Y-m-d H:i:s');
// 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));
$fullNameGold = $nameGold;
$nameGold = str_replace("14", "OD", $nameGold);
$nameGold = str_replace("18", "OS", $nameGold);
$nameGold = str_replace("22", "YI", $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";
$data[$nameGold]['Name'] = $fullNameGold;
}
}
// 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);
}
}