From 8730465a429fde103bb5155f924a1e00070b4f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Fri, 17 Jan 2025 22:58:44 +0300 Subject: [PATCH] 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. --- app/Jobs/FetchGoldRates.php | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 app/Jobs/FetchGoldRates.php diff --git a/app/Jobs/FetchGoldRates.php b/app/Jobs/FetchGoldRates.php new file mode 100644 index 0000000..636f28d --- /dev/null +++ b/app/Jobs/FetchGoldRates.php @@ -0,0 +1,76 @@ +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); + } +} \ No newline at end of file