Files
finance/app/Jobs/FetchGoldRates.php
Ümit Tunç 41eaf7c6df Update gold rate naming convention in FetchGoldRates job
- Changed the abbreviation for "22AYARBILEZIK" from "YIB" to "YIA" to improve clarity and consistency in the naming convention for gold rates.

This change enhances the accuracy of the data representation in the FetchGoldRates job within the Truncgil Finance application.
2025-04-22 10:36:45 +03:00

112 lines
4.6 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;
use Illuminate\Support\Facades\Log;
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);
// Daha spesifik bir sorgu ile sadece ihtiyacımız olan elementleri seçiyoruz
$elementsGold = $xpathGold->query("//*[@data-socket-key]");
// Debug için gelen data-socket-key değerlerini loglamak için
$socketKeys = [];
foreach ($elementsGold as $element) {
$attrGold = $element->getAttribute('data-socket-key');
$typeGold = $element->getAttribute('data-socket-attr');
$valueGold = NumberFormatter::commaToDot($element->nodeValue);
// Debug için data-socket-key değerlerini topluyoruz
if (!in_array($attrGold, $socketKeys)) {
$socketKeys[] = $attrGold;
}
if (trim($attrGold) !== '') {
// Önce orijinal ismi tam olarak koruyalım
$nameGold = strtoupper(str_replace("-", "", $attrGold));
$fullNameGold = $nameGold;
// Veri tipi belirlemeleri
$metalType = "Gold"; // Varsayılan olarak altın
// Özel metaller için tür kontrolü
if (strpos($nameGold, 'GRAMPALADYUM') !== false) {
$metalType = "Palladium";
} elseif (strpos($nameGold, 'GRAMPLATIN') !== false) {
$metalType = "Platinum";
} elseif (strpos($nameGold, 'GRAMGUMUS') !== false) {
$metalType = "Silver";
}
// Kısaltma işlemleri
$nameGold = str_replace("14AYARBILEZIK", "ODB", $nameGold);
$nameGold = str_replace("18AYARBILEZIK", "OSB", $nameGold);
$nameGold = str_replace("22AYARBILEZIK", "YIA", $nameGold);
$nameGold = str_replace("GRAMALTIN", "GRA", $nameGold);
$nameGold = str_replace("GRAMPLATIN", "GPL", $nameGold);
$nameGold = str_replace("GRAMHASALTIN", "HAS", $nameGold);
$nameGold = str_replace("GRAMPALADYUM", "PAL", $nameGold);
$nameGold = str_replace("GRAMGUMUS", "GUM", $nameGold);
// Eğer bir kısaltma yapıldıysa, ilk 3 karakteri al
// Aksi takdirde orijinal değeri kullan
if ($nameGold != $fullNameGold) {
$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'] = $metalType;
$data[$nameGold]['Name'] = $fullNameGold;
}
}
// Debug için socket key'leri loglayalım
Log::info('Available data-socket-keys: ' . implode(', ', $socketKeys));
// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept-Language' => 'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
])->get($url);
}
}