-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
217 lines (201 loc) · 6.46 KB
/
extension.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const St = imports.gi.St;
const Main = imports.ui.main;
const Lang = imports.lang;
const PanelMenu = imports.ui.panelMenu;
const Clutter = imports.gi.Clutter;
const Mainloop = imports.mainloop;
const Soup = imports.gi.Soup;
const Util = imports.misc.util;
const TIMEOUT = 10 //seconds
const BASE_URL_BINANCE = 'https://api.binance.com/api/v1/ticker/24hr';
const BASE_URL_FREIEX = 'https://freiexchange.com/api/public/'
const BASE_TRADEVIEW_URL_FREIEX = 'https://freiexchange.com/market/'
const BASE_TRADEVIEW_URL_BINANCE = 'https://www.binance.com/tradeDetail.html?symbol='
const BASE_URL_BITPANDA = 'https://api.bitpanda.com/v1/ticker'
const BASE_TRADEVIEW_URL_BITPANDA = 'https://cointicker.online/'
const symbols = [
{
symbol: 'AIQ/BTC',
tradeSymbol: 'AIQ',
name: 'AIQ',
decimals: 8,
removeZero:true,
url: BASE_URL_FREIEX,
url_trade: BASE_TRADEVIEW_URL_FREIEX,
},
{
symbol: 'PAN',
tradeSymbol: 'PAN',
name: 'PAN',
decimals: 4,
removeZero: false,
url: BASE_URL_BITPANDA,
url_trade: BASE_TRADEVIEW_URL_BITPANDA,
},
{
symbol: 'TNBBTC',
tradeSymbol: 'TNB_BTC',
name: 'TNB',
decimals: 8,
removeZero: true,
url: BASE_URL_BINANCE,
url_trade: BASE_TRADEVIEW_URL_BINANCE,
},
{
symbol: 'LENDBTC',
tradeSymbol: 'LEND_BTC',
name: 'LEND',
decimals: 8,
removeZero: true,
url: BASE_URL_BINANCE,
url_trade: BASE_TRADEVIEW_URL_BINANCE,
},
{
symbol: 'XLMBTC',
tradeSymbol: 'XLM_BTC',
name: 'XLM',
decimals: 8,
removeZero: true,
url: BASE_URL_BINANCE,
url_trade: BASE_TRADEVIEW_URL_BINANCE,
},
{
symbol: 'ADABTC',
tradeSymbol: 'ADA_BTC',
name: 'ADA',
decimals: 8,
removeZero: true,
url: BASE_URL_BINANCE,
url_trade: BASE_TRADEVIEW_URL_BINANCE,
},
{
symbol: 'SUBBTC',
tradeSymbol: 'SUB_BTC',
name: 'SUB',
decimals: 8,
removeZero: true,
url: BASE_URL_BINANCE,
url_trade: BASE_TRADEVIEW_URL_BINANCE,
},
{
symbol: 'ETHBTC',
tradeSymbol: 'ETH_BTC',
name: 'E',
decimals: 8,
removeZero: true,
url: BASE_URL_BINANCE,
url_trade: BASE_TRADEVIEW_URL_BINANCE,
},
{
symbol: 'BTCUSDT',
tradeSymbol: 'BTC_USDT',
name: 'B',
decimals: 0,
url: BASE_URL_BINANCE,
url_trade: BASE_TRADEVIEW_URL_BINANCE,
}
];
const Ticker = new Lang.Class({
Name: 'Ticker BaseClass',
Extends: PanelMenu.Button,
_init: function(symbol) {
this.parent(0.0, symbol.name + ' ticker', false);
this.label = new St.Label({ text: symbol.name, y_align: Clutter.ActorAlign.CENTER });
this.actor.add_actor(this.label);
this.actor.connect('button-press-event', Lang.bind(this, this._openBrowser))
this._symbol = symbol;
this._refresh();
},
_openBrowser: function() {
if (this._symbol.url == BASE_URL_BINANCE) {
let url = this._symbol.url_trade + this._symbol.tradeSymbol
Util.spawnCommandLine("xdg-open " + url)
}else if (this._symbol.url == BASE_URL_FREIEX) {
let url = this._symbol.url_trade + this._symbol.tradeSymbol + '/BTC'
Util.spawnCommandLine("xdg-open " + url)
}
},
_refresh: function() {
this._loadData(this._refreshUI);
this._removeTimeout();
this._timeout = Mainloop.timeout_add_seconds(TIMEOUT, Lang.bind(this, this._refresh))
return true
},
_loadData: function() {
if (this._symbol.url == BASE_URL_BINANCE) {
let params = { symbol: this._symbol.symbol };
this._httpSession = new Soup.Session();
let _httpSession = this._httpSession
let message = Soup.form_request_new_from_hash('GET', this._symbol.url, params)
this._httpSession.queue_message(message, Lang.bind(this, function(httpSession, message) {
if (message.status_code !== 200) {
global.log(message);
return;
}
let json = JSON.parse(message.response_body.data);
this._refreshUI(json)
}))
}else if (this._symbol.url == BASE_URL_FREIEX) {
this._httpSession = new Soup.Session();
let _httpSession = this._httpSession
let message = Soup.Message.new('GET', this._symbol.url+this._symbol.symbol)
this._httpSession.queue_message(message, Lang.bind(this, function(httpSession, message) {
if (message.status_code !== 200) {
global.log(message);
return;
}
let json = JSON.parse(message.response_body.data)
this._refreshUI(json)
}))
}
},
_refreshUI: function(data) {
let text = this._symbol.name + ': '
let removeZeroRegex = /(?![0\.])(\d*)/g;
let value = 0
//process different ways to retrieve data from exchanges
if (this._symbol.url == BASE_URL_BINANCE) {
value = Number(data.lastPrice).toFixed(this._symbol.decimals);
}else if (this._symbol.url == BASE_URL_FREIEX) {
value = Number(data.public.last).toFixed(this._symbol.decimals);
}
if (this._symbol.removeZero) {
text += removeZeroRegex.exec(value)[0]
} else {
text += value
}
this.label.set_text(text);
},
_removeTimeout: function() {
if (this._timeout) {
Mainloop.source_remove(this._timeout);
this._timeout = null;
}
},
stop: function() {
if (this._httpSession) {
this._httpSession.abort();
}
this._httpSession = undefined;
if(this._timeout) {
Mainloop.source_remove(this._timeout);
}
this._timeout = undefined;
this.menu.removeAll();
}
})
function init() {
}
function enable() {
for (let i = 0; i < symbols.length; i++) {
let ticker = new Ticker(symbols[i])
symbols[i].ticker = ticker
Main.panel.addToStatusArea(symbols[i].name + 'Menu', ticker)
}
}
function disable() {
for (let i = 0; i < symbols.length; i++) {
symbols[i].ticker.stop();
symbols[i].ticker.destroy();
}
}