Enhance FetchGoldRates job with improved data handling and logging
- Added logging for data-socket-key values to assist in debugging and monitoring. - Refined the extraction of gold rates to include specific metal types (Gold, Palladium, Platinum, Silver) based on the data attributes. - Updated the user agent and accept language headers for improved compatibility with the data source. - Enhanced the naming convention for gold rates to ensure clarity and consistency in the data structure. These changes collectively improve the functionality and maintainability of the FetchGoldRates job in the Truncgil Finance application.
This commit is contained in:
+43
-10
@@ -10,6 +10,7 @@ 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
|
||||
{
|
||||
@@ -23,30 +24,59 @@ class FetchGoldRates implements ShouldQueue
|
||||
// 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);
|
||||
|
||||
if (trim($attrGold) !== '') {
|
||||
// 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;
|
||||
$nameGold = str_replace("14", "OD", $nameGold);
|
||||
$nameGold = str_replace("18", "OS", $nameGold);
|
||||
$nameGold = str_replace("22", "YI", $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", "YIB", $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)) {
|
||||
@@ -56,13 +86,15 @@ class FetchGoldRates implements ShouldQueue
|
||||
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";
|
||||
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));
|
||||
|
||||
@@ -72,8 +104,9 @@ class FetchGoldRates implements ShouldQueue
|
||||
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'
|
||||
'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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user