mirror of
https://github.com/rottenwheel/moner.ooo.git
synced 2024-12-22 08:07:47 +00:00
refactor(coingecko.php): streamline currency data update process
Consolidated currency list initialization and improved data fetching from CoinGecko API. Enhanced error handling for CURL requests. Optimized the logic for updating and saving currency data, ensuring more efficient and readable code. Ensured data write-back occurs only when sufficient time has elapsed since the last update.
This commit is contained in:
parent
d89a2c9053
commit
f475b4ff73
1 changed files with 53 additions and 97 deletions
150
coingecko.php
150
coingecko.php
|
@ -1,110 +1,66 @@
|
||||||
<?php
|
<?php
|
||||||
$new_array = array();
|
|
||||||
$array = array("EUR",
|
|
||||||
"BTC",
|
|
||||||
"USD",
|
|
||||||
"GBP",
|
|
||||||
"CHF",
|
|
||||||
"RUB",
|
|
||||||
"CNY",
|
|
||||||
"JPY",
|
|
||||||
"IDR",
|
|
||||||
"KRW",
|
|
||||||
"TRY",
|
|
||||||
"AUD",
|
|
||||||
"BMD",
|
|
||||||
"CAD",
|
|
||||||
"HKD",
|
|
||||||
"NZD",
|
|
||||||
"SGD",
|
|
||||||
"TWD",
|
|
||||||
"ILS",
|
|
||||||
"PLN",
|
|
||||||
"ZAR",
|
|
||||||
"CZK",
|
|
||||||
"DKK",
|
|
||||||
"NOK",
|
|
||||||
"SEK",
|
|
||||||
"ARS",
|
|
||||||
"CLP",
|
|
||||||
"PHP",
|
|
||||||
"MXN",
|
|
||||||
"BHD",
|
|
||||||
"KWD",
|
|
||||||
"BRL",
|
|
||||||
"MYR",
|
|
||||||
"VEF",
|
|
||||||
"UAH",
|
|
||||||
"VND",
|
|
||||||
"BDT",
|
|
||||||
"HUF",
|
|
||||||
"MMK",
|
|
||||||
"NGN",
|
|
||||||
"THB",
|
|
||||||
"AED",
|
|
||||||
"SAR",
|
|
||||||
"PKR",
|
|
||||||
"LKR",
|
|
||||||
"INR",
|
|
||||||
"BTC",
|
|
||||||
"LTC",
|
|
||||||
"ETH",
|
|
||||||
"XAG",
|
|
||||||
"XAU");
|
|
||||||
|
|
||||||
//echo "<h1>coingecko.com</h1>";
|
// Set default timezone
|
||||||
|
|
||||||
// Die Standard-Zeitzone, die verwendet werden soll, setzen.
|
|
||||||
date_default_timezone_set('Europe/Berlin');
|
date_default_timezone_set('Europe/Berlin');
|
||||||
|
|
||||||
// Holt den letzten Wert für die if Abfrage
|
// Define the array of currencies
|
||||||
$xmrdatas = json_decode(file_get_contents("coingecko.json"), true);
|
$currencies = [
|
||||||
|
"EUR", "BTC", "USD", "GBP", "CHF", "RUB", "CNY", "JPY", "IDR", "KRW", "TRY", "AUD",
|
||||||
|
"BMD", "CAD", "HKD", "NZD", "SGD", "TWD", "ILS", "PLN", "ZAR", "CZK", "DKK", "NOK",
|
||||||
|
"SEK", "ARS", "CLP", "PHP", "MXN", "BHD", "KWD", "BRL", "MYR", "VEF", "UAH", "VND",
|
||||||
|
"BDT", "HUF", "MMK", "NGN", "THB", "AED", "SAR", "PKR", "LKR", "INR", "LTC", "ETH",
|
||||||
|
"XAG", "XAU"
|
||||||
|
];
|
||||||
|
|
||||||
// Liefert den aktuellen Unix-Zeitstempel
|
// Fetch the previously stored data
|
||||||
$zeit = time();
|
$previousData = json_decode(file_get_contents("coingecko.json"), true);
|
||||||
|
$currentTime = time();
|
||||||
|
|
||||||
// Sind ~30 Minuten vergangen?
|
// Check if five seconds have passed since the last update
|
||||||
if(($zeit - $xmrdatas['time']) >= 5){
|
if (($currentTime - $previousData['time']) >= 5) {
|
||||||
//echo "Gespeicherte Zeit: ".$xmrdatas['time']."<br/>Aktuelle Zeit: ".$zeit;
|
// Fetch the latest data from CoinGecko API
|
||||||
|
$apiUrl = 'https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=' . implode('%2C', array_map('strtolower', $currencies)) . '&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true';
|
||||||
|
|
||||||
// Initialize CURL
|
$ch = curl_init($apiUrl);
|
||||||
$ch = curl_init('https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=btc%2Ceth%2Cltc%2Cbch%2Cbnb%2Ceos%2Cxrp%2Cxlm%2Clink%2Cdot%2Cyfi%2Cusd%2Caed%2Cars%2Caud%2Cbdt%2Cbhd%2Cbmd%2Cbrl%2Ccad%2Cchf%2Cclp%2Ccny%2Cczk%2Cdkk%2Ceur%2Cgbp%2Chkd%2Chuf%2Cidr%2Cils%2Cinr%2Cjpy%2Ckrw%2Ckwd%2Clkr%2Cmmk%2Cmxn%2Cmyr%2Cngn%2Cnok%2Cnzd%2Cphp%2Cpkr%2Cpln%2Crub%2Csar%2Csek%2Csgd%2Cthb%2Ctry%2Ctwd%2Cuah%2Cvef%2Cvnd%2Czar%2Cxdr%2Cxag%2Cxau%2Cbits%2Csats&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true');
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
// Store the data:
|
|
||||||
$json = curl_exec($ch);
|
$json = curl_exec($ch);
|
||||||
curl_close($ch);
|
|
||||||
|
|
||||||
// Sortiert/Formatiert die Ausgabe
|
|
||||||
$sort_array = json_decode($json, true);
|
|
||||||
$new_sort_array = $sort_array['monero'];
|
|
||||||
|
|
||||||
// Zeit wird ergänzt
|
if (curl_errno($ch)) {
|
||||||
$new_array['time'] = $zeit;
|
echo 'Curl error: ' . curl_error($ch);
|
||||||
$new_sort_array['time'] = $zeit;
|
curl_close($ch);
|
||||||
|
exit;
|
||||||
foreach ($array as $fiatValue) {
|
|
||||||
$fiatValue = strtolower($fiatValue);
|
|
||||||
if (array_key_exists($fiatValue, $sort_array['monero'])) {
|
|
||||||
$new_array[$fiatValue]['lastValue'] = $sort_array['monero'][$fiatValue];
|
|
||||||
$new_array[$fiatValue]['lastDate'] = $zeit;
|
|
||||||
}else{
|
|
||||||
$new_array[$fiatValue]['lastValue'] = $xmrdatas[$fiatValue]['lastValue'];
|
|
||||||
$new_array[$fiatValue]['lastDate'] = $xmrdatas[$fiatValue]['lastDate'];
|
|
||||||
}
|
|
||||||
//print_r($new_array);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
// Decode the fetched data
|
||||||
// Werte in die .json gespeichert
|
$fetchedData = json_decode($json, true);
|
||||||
file_put_contents("coingecko.json", json_encode($new_array));
|
$moneroData = $fetchedData['monero'];
|
||||||
file_put_contents("coingecko-original.json", json_encode($new_sort_array));
|
|
||||||
|
// Initialize new data array
|
||||||
// echo "<br/>Sync.<hr/>";
|
$newData = ['time' => $currentTime];
|
||||||
|
|
||||||
}//else{
|
// Update the data for each currency
|
||||||
//echo "Keine Daten, noch zu früh!<br/><br/>";
|
foreach ($currencies as $currency) {
|
||||||
//echo "Gespeicherte Zeit: ".$xmrdatas['time']."<br/>Aktuelle Zeit: ".$zeit."<br/><br/>Differenz: ".$zeit - $xmrdatas['time']."<br/><hr/>";
|
$currencyLower = strtolower($currency);
|
||||||
//}
|
if (isset($moneroData[$currencyLower])) {
|
||||||
?>
|
$newData[$currencyLower] = [
|
||||||
|
'lastValue' => $moneroData[$currencyLower],
|
||||||
|
'lastDate' => $currentTime
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
$newData[$currencyLower] = [
|
||||||
|
'lastValue' => $previousData[$currencyLower]['lastValue'] ?? null,
|
||||||
|
'lastDate' => $previousData[$currencyLower]['lastDate'] ?? null
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the new data to JSON files
|
||||||
|
file_put_contents("coingecko.json", json_encode($newData, JSON_PRETTY_PRINT));
|
||||||
|
file_put_contents("coingecko-original.json", json_encode($moneroData, JSON_PRETTY_PRINT));
|
||||||
|
|
||||||
|
echo "Data updated successfully.";
|
||||||
|
} else {
|
||||||
|
echo "No data update needed. Last update was less than five seconds ago.";
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue