-
Notifications
You must be signed in to change notification settings - Fork 1
/
termux.js
107 lines (95 loc) · 3 KB
/
termux.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
*
* crypto-price-monitor: Termux example
* Continuously monitor cryptocurrency prices
* Written by Ari Saif
* Should be run inside [Termux](https://termux.com/) in Android using GPS
* First install the following apps:
* - https://play.google.com/store/apps/details?id=com.termux
* - https://play.google.com/store/apps/details?id=com.termux.api
*/
const spinners = Object.assign({}, require("./spinners.json"));
var defines = require("../defines");
const mri = require("mri");
const api = require("termux");
var utility_functions = require("../utility");
const moment = require("moment");
var log = defines.log;
if (!api.hasTermux) {
log.info("Termux doesn't exits!");
}
var g_notification_id = 1;
var g_notificationOutput, g_mktCapFormatted;
var g_updateCounter = 0 ;
//-----------------------------------------------------------------------------
/**
* A function that is called in the beginning
* We set g_notificationOutput to show a proper message
* @returns {string}
*/
async function initialCallback() {
g_notificationOutput = `Please wait...`;
g_mktCapFormatted = "Updating crypto prices...";
}
//-----------------------------------------------------------------------------
/**
* Our callback function that is called each time values are updated
* We only update g_notificationOutput to be shown in notification area
*/
async function updateValuesCallback(notificationOutput, mktCapFormatted) {
g_notificationOutput = notificationOutput;
g_mktCapFormatted = mktCapFormatted;
}
//-----------------------------------------------------------------------------
/**
* This function is called in an interval to update the notification message
*/
async function updateNotification() {
g_updateCounter++;
// Set the animation frame
let frames = spinners.moon.frames;
if (
g_notificationOutput &&
g_mktCapFormatted
) {
api
.notification()
.content(g_notificationOutput)
.id(g_notification_id)
.title(
frames[g_updateCounter % frames.length].toString() +
`💰 ` + moment().format("h:mm") + `: ` + g_mktCapFormatted)
// .url('...')
.run();
}
}
//-----------------------------------------------------------------------------
let options = {
updateInterval: 5,
loopForever: true,
updateValuesCallback: updateValuesCallback,
initialCallback: initialCallback
};
//-----------------------------------------------------------------------------
const argv = process.argv.slice(2);
let cliArgs = mri(argv);
let optionKeys = Object.keys(Globals.options);
optionKeys.forEach(e => {
if (cliArgs[e]) {
options[e] = cliArgs[e];
log.info(`${e}: ${options[e]}`);
}
});
if (utility_functions.isString(options.cryptosOfInterest)) {
options.cryptosOfInterest = options.cryptosOfInterest.split(",");
}
g_notification_id = utility_functions.hashCode(
moment()
.valueOf()
.toString()
);
var crypto_price_checker = require("../index.js")(options);
crypto_price_checker.start();
setInterval(() => {
updateNotification();
}, 1 * 1000);