Better retrieval mechanism; also caching for promise
parent
3d6c78afac
commit
b2e6077e8b
|
|
@ -1,11 +1,10 @@
|
|||
(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 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==");
|
||||
|
||||
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");
|
||||
|
||||
// Ensure tokenSelected is an array
|
||||
if (!(settings.tokenSelected instanceof Array)) settings.tokenSelected = [];
|
||||
|
||||
// Cache for storing results
|
||||
|
|
@ -17,12 +16,10 @@
|
|||
return {
|
||||
name: token,
|
||||
get: function() {
|
||||
// Return cached data if available
|
||||
// Immediate return from cache or placeholder
|
||||
if (cache[token]) {
|
||||
return cache[token];
|
||||
}
|
||||
|
||||
// Return placeholder while fetching data
|
||||
return {
|
||||
text: "Load...",
|
||||
img: COIN_ICON
|
||||
|
|
@ -32,42 +29,23 @@
|
|||
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 => {
|
||||
logFile.write("HTTP resp:" + JSON.stringify(cmcResult));
|
||||
const apiData = JSON.parse(cmcResult.resp);
|
||||
logFile.write("data:" + JSON.stringify(apiData));
|
||||
const url = `https://api.binance.com/api/v3/ticker/24hr?symbol=${token.toUpperCase()}USDT`;
|
||||
|
||||
// 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));
|
||||
cache[token] = {
|
||||
text: "Error",
|
||||
img: COIN_ICON
|
||||
};
|
||||
self.emit("redraw");
|
||||
});
|
||||
|
||||
// Set interval to refresh data every hour
|
||||
self.interval = setInterval(() => {
|
||||
(function fetchData() {
|
||||
Bangle.http(url, { method: 'GET' })
|
||||
.then(cmcResult => {
|
||||
logFile.write("HTTP resp:" + JSON.stringify(cmcResult));
|
||||
const apiData = JSON.parse(cmcResult.resp);
|
||||
logFile.write("data:" + JSON.stringify(apiData));
|
||||
|
||||
// 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 => {
|
||||
|
|
@ -78,6 +56,11 @@
|
|||
};
|
||||
self.emit("redraw");
|
||||
});
|
||||
})();
|
||||
|
||||
// Set interval to refresh data every hour
|
||||
self.interval = setInterval(() => {
|
||||
fetchData(); // Call the fetch function again
|
||||
}, 3600000); // Refresh every hour
|
||||
},
|
||||
hide: function() {
|
||||
|
|
@ -90,4 +73,3 @@
|
|||
})
|
||||
};
|
||||
})();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{ "id": "coin_info",
|
||||
"name": "Crypto-Coins Info",
|
||||
"shortName":"Coins Info",
|
||||
"version": "0.04.25",
|
||||
"version": "0.04.26",
|
||||
"description": "Crypto-Coins Infos with the help of the Coinmarketcap API",
|
||||
"icon": "app.png",
|
||||
"tags": "clkinfo",
|
||||
|
|
|
|||
Loading…
Reference in New Issue