2023-05-15 09:04:02 +02:00
|
|
|
<?php
|
2024-05-24 11:14:53 +02:00
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
// Set default timezone
|
2023-05-15 09:04:02 +02:00
|
|
|
date_default_timezone_set('Europe/Berlin');
|
|
|
|
|
2024-08-08 09:09:06 +02:00
|
|
|
// Define currencies that should *not* be included in the list
|
|
|
|
$excludedCurrencies = ['bits', 'sats'];
|
2023-05-15 09:04:02 +02:00
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
// Fetch the previously stored data
|
|
|
|
$previousData = json_decode(file_get_contents("coingecko.json"), true);
|
2024-08-08 09:16:25 +02:00
|
|
|
$output = $previousData;
|
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
$currentTime = time();
|
2023-05-15 09:04:02 +02:00
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
// Check if five seconds have passed since the last update
|
|
|
|
if (($currentTime - $previousData['time']) >= 5) {
|
2024-08-08 09:09:06 +02:00
|
|
|
// Fetch the available currencies from CoinGecko API
|
|
|
|
$availableCurrencies = json_decode(file_get_contents("https://api.coingecko.com/api/v3/simple/supported_vs_currencies"), true);
|
|
|
|
|
|
|
|
// Remove excluded currencies
|
|
|
|
$availableCurrencies = array_diff($availableCurrencies, $excludedCurrencies);
|
|
|
|
|
|
|
|
$currencies = array_map('strtoupper', $availableCurrencies);
|
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
// 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';
|
2023-05-15 09:04:02 +02:00
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
$ch = curl_init($apiUrl);
|
2023-05-15 09:04:02 +02:00
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$json = curl_exec($ch);
|
2024-08-08 08:58:55 +02:00
|
|
|
|
|
|
|
if (curl_errno($ch)) {
|
|
|
|
echo 'Curl error: ' . curl_error($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2023-05-15 09:04:02 +02:00
|
|
|
curl_close($ch);
|
2024-05-24 11:14:53 +02:00
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
// Decode the fetched data
|
|
|
|
$fetchedData = json_decode($json, true);
|
|
|
|
$moneroData = $fetchedData['monero'];
|
|
|
|
|
|
|
|
// Initialize new data array
|
|
|
|
$newData = ['time' => $currentTime];
|
2024-05-24 11:14:53 +02:00
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
// Update the data for each currency
|
|
|
|
foreach ($currencies as $currency) {
|
|
|
|
$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
|
|
|
|
];
|
2024-05-24 11:14:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 08:58:55 +02:00
|
|
|
// 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));
|
2024-05-24 11:14:53 +02:00
|
|
|
|
2024-08-08 09:16:25 +02:00
|
|
|
$output = $newData;
|
2024-08-08 08:58:55 +02:00
|
|
|
}
|
2024-08-08 09:16:25 +02:00
|
|
|
|
|
|
|
// Output the data
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode($newData, JSON_PRETTY_PRINT);
|