Better retrieval mechanism; also caching for promise

master
Martin Zwigl 2025-03-08 01:34:29 +01:00
parent 0cfd65b919
commit 3d6c78afac
3 changed files with 69 additions and 43 deletions

View File

@ -7,4 +7,4 @@ Crypto-Coins Infos with the help of the Coinmarketcap API
Martin Zwigl
## Reminder for last working
- ef6c04b1cca90125d3e41381be4a70231c076412
- 0cfd65b919157356d46ccbbeb2416d794312e13a

View File

@ -1,67 +1,93 @@
(function() {
const COIN_ICON = atob("MDCBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/AAAAAA//8AAAAB///AAAAB8A/gAAAAgAH4AAAwAAB8AAB4AAA+AADwA4APAADgA4APAAHgA4AHgADAf/ADwAAAf/gBwAAAf/wB4AAAcB4A4AAAcA4A4AAAcA4A4AYAcA4AcA8AcB4AcB+Af/wDdj/Af/4D/n/Af/8D/n/AcAcB/A4AcAOA+A4AcAOAcAcAcAOAAAcAcAAAAAcAcAAAAAeAf/AAAAOAf/AAAAPAf/ADAAHgA4AHgADwA4AHAADwA4APAAB8AAA+AAA+AAB8AAAfgAH4AAAH8A/gAAAD///AAAAA//8AAAAAD/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="); // (full base64 string here)
const settings = require("Storage").readJSON("coin_info.settings.json",1)||{};
const db = require("Storage").readJSON("coin_info.cmc_key.json",1)||{};
const settings = require("Storage").readJSON("coin_info.settings.json", 1) || {};
const db = require("Storage").readJSON("coin_info.cmc_key.json", 1) || {};
const logFile = require("Storage").open("log.txt", "a");
if (!(settings.tokenSelected instanceof Array))
settings.tokenSelected = [];
// Ensure tokenSelected is an array
if (!(settings.tokenSelected instanceof Array)) settings.tokenSelected = [];
// Cache for storing results
let cache = {};
return {
name: "CoinInfo",
items: settings.tokenSelected.map(token => {
return { name : token,
get : function()
{
// const url = `https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?slug=${token}`;
// const url = `https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?slug=bitcoin`;
const url = `https://api.binance.com/api/v3/ticker/24hr?symbol=BTCUSDT`;
return Bangle
.http(url, {
method: 'GET'
// ,headers: {
// 'CMC_PRO_API_KEY': db.apikey
// }
})
return {
name: token,
get: function() {
// Return cached data if available
if (cache[token]) {
return cache[token];
}
// Return placeholder while fetching data
return {
text: "Load...",
img: COIN_ICON
};
},
show: function() {
var self = this;
// Fetch data when shown
const url = `https://api.binance.com/api/v3/ticker/24hr?symbol=${token.toUpperCase()}`;
Bangle.http(url, { method: 'GET' })
.then(cmcResult => {
// text: JSON.parse(cmcResult.resp).data["1"].symbol, // Fixed data path
logFile.write("HTTP resp:" + JSON.stringify(cmcResult));
const apiData = JSON.parse(cmcResult.resp); // Correctly declare variable
const apiData = JSON.parse(cmcResult.resp);
logFile.write("data:" + JSON.stringify(apiData));
return {
text: apiData.symbol,
// Update cache with fetched data
cache[token] = {
// text: `${apiData.symbol}\n${apiData.lastPrice} USD`,
text: `${apiData.symbol}`,
img: COIN_ICON
};
// Trigger a redraw of this item
self.emit("redraw");
})
.catch(err => {
logFile.write("API Error: " + JSON.stringify(err));
return {
text: err.toString(),
cache[token] = {
text: "Error",
img: COIN_ICON
}
});
},
show : function() {
var self = this;
// Set timeout to align to the next minute
self.interval = setTimeout(function timerTimeout() {
self.emit("redraw");
// Continue updating every hour
self.interval = setInterval(function intervalCallback() {
};
self.emit("redraw");
}, 3600000);
}, 3600000 - (Date.now() % 3600000));
});
// Set interval to refresh data every hour
self.interval = setInterval(() => {
Bangle.http(url, { method: 'GET' })
.then(cmcResult => {
const apiData = JSON.parse(cmcResult.resp);
cache[token] = {
// text: `${apiData.symbol}\n${apiData.lastPrice} USD`,
text: `${apiData.symbol}`,
img: COIN_ICON
};
self.emit("redraw");
})
.catch(err => {
logFile.write("API Error: " + JSON.stringify(err));
cache[token] = {
text: "Error",
img: COIN_ICON
};
self.emit("redraw");
});
}, 3600000); // Refresh every hour
},
hide : function() {
hide: function() {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}
// ,run: function () {
// this.emit("redraw");
// }
}
};
})
};
})
})();

View File

@ -1,7 +1,7 @@
{ "id": "coin_info",
"name": "Crypto-Coins Info",
"shortName":"Coins Info",
"version": "0.04.24",
"version": "0.04.25",
"description": "Crypto-Coins Infos with the help of the Coinmarketcap API",
"icon": "app.png",
"tags": "clkinfo",