Files
finance/app/Jobs/FetchCurrencyRates.php
T
Ümit Tunç 09e981f94b Implement crypto currency rates feature and enhance API responses
- Added a new job to fetch crypto currency rates from an external source and store them in a JSON file.
- Updated the CurrencyController to include methods for retrieving current crypto currency rates and rates by name.
- Enhanced the API to return detailed metadata for crypto currency rates, including timestamps and update information.
- Updated the API routes to support new endpoints for crypto currency rates.
- Modified the merged rates JSON to include crypto currency data alongside existing currency and gold rates.
- Improved the welcome view to display additional crypto currency information, enhancing user experience.

These changes collectively expand the functionality of the Truncgil Finance application, providing users with comprehensive access to crypto currency data.
2025-01-21 19:27:03 +03:00

82 lines
2.8 KiB
PHP
Raw 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 FetchCurrencyRates implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
$data = [];
$data['Update_Date'] = now()->format('Y-m-d H:i:s');
// Döviz kurları
$response = $this->fetchData('https://kur.doviz.com');
// DOM işlemleri için veri çekme
$dom = new \DOMDocument();
@$dom->loadHTML($response->body());
$xpath = new \DOMXPath($dom);
// Döviz kurlarını çekme
$elements = $xpath->query("//*[@data-socket-key]");
foreach ($elements as $element) {
$name = $element->getAttribute('data-socket-key');
$type = $element->getAttribute('data-socket-attr');
$value = NumberFormatter::commaToDot($element->nodeValue);
if (trim($name) !== '') {
if ($name == "JPY") $value = $value / 100;
if (strlen($name) === 3) {
// Para birimi ismini bul
$nameElement = $xpath->query(".//div[@class='cname']", $element->parentNode);
if ($nameElement->length > 0) {
$data[$name]['Name'] = trim($nameElement->item(0)->nodeValue);
}
if ($type == "bid") $data[$name]['Buying'] = (float)$value;
if ($type == "ask") $data[$name]['Selling'] = (float)$value;
if ($type == "c") $data[$name]['Change'] = round((float)$value, 2);
$data[$name]['Type'] = "Currency";
}
}
}
// AZN kuru için
$aznResponse = $this->fetchData('https://wise.com/tr/currency-converter/azn-to-try-rate?amount=1');
preg_match('/(\d+\.\d+)\s+TRY/', $aznResponse->body(), $matches);
if (isset($matches[1])) {
$data['AZN'] = [
'Buying' => NumberFormatter::commaToDot($matches[1]),
'Selling' => NumberFormatter::commaToDot($matches[1]),
'Change' => "0.00",
'Type' => "Currency"
];
}
// JSON dosyasını kaydet
Storage::put('currency/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);
}
}