09e981f94b
- 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.
82 lines
3.0 KiB
PHP
82 lines
3.0 KiB
PHP
<?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 FetchCryptoCurrencyRates implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
public function handle()
|
||
{
|
||
$data = [];
|
||
$data['Update_Date'] = now()->format('Y-m-d H:i:s');
|
||
|
||
// Kripto para kurları
|
||
$response = $this->fetchData('https://www.doviz.com/kripto-paralar');
|
||
// DOM işlemleri için veri çekme
|
||
$dom = new \DOMDocument();
|
||
@$dom->loadHTML($response->body());
|
||
$xpath = new \DOMXPath($dom);
|
||
|
||
// Kripto para verilerini içeren tabloyu ID'ye göre bul
|
||
$rows = $xpath->query("//table[@id='coins']/tbody/tr");
|
||
foreach ($rows as $row) {
|
||
$symbol = trim($xpath->evaluate("string(.//td[1]//text()[last()])", $row));
|
||
// Semboldeki boşlukları temizle ve sadece kripto para sembolünü al
|
||
$symbol = preg_replace('/^.*\s(\w+)\s*$/', '$1', $symbol);
|
||
|
||
// Kripto para ismini al
|
||
$name = $xpath->evaluate("string(.//td[1]//div[@class='cname'])", $row);
|
||
|
||
if (strlen($symbol) > 0) {
|
||
$priceUSD = $xpath->evaluate("string(.//td[2])", $row);
|
||
$priceTRY = $xpath->evaluate("string(.//td[3])", $row);
|
||
$change = $xpath->evaluate("string(.//td[6])", $row);
|
||
|
||
// Fiyat verilerini temizle (örn: $3.310,57 -> 3310.57)
|
||
$priceUSD = str_replace('$', '', $priceUSD);
|
||
$priceUSD = str_replace('.', '', $priceUSD);
|
||
$priceUSD = str_replace(',', '.', $priceUSD);
|
||
|
||
$priceTRY = str_replace('₺', '', $priceTRY);
|
||
$priceTRY = str_replace('.', '', $priceTRY);
|
||
$priceTRY = str_replace(',', '.', $priceTRY);
|
||
|
||
$change = str_replace('%', '', $change);
|
||
$change = str_replace('.', '', $change);
|
||
$change = str_replace(',', '.', $change);
|
||
|
||
$data[$symbol] = [
|
||
'Name' => trim($name),
|
||
'USD_Price' => (float)$priceUSD,
|
||
'TRY_Price' => (float)$priceTRY,
|
||
'Selling' => (float)$priceTRY,
|
||
'Change' => (float)$change,
|
||
'Type' => 'CryptoCurrency'
|
||
];
|
||
}
|
||
}
|
||
|
||
// JSON dosyasını kaydet
|
||
Storage::put('crypto/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);
|
||
}
|
||
}
|