This repository has been archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
libabr.js
192 lines (174 loc) · 5.31 KB
/
libabr.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
// Copyright (c) 2017 Alexandre Storelli
// This file is licensed under the MIT licence.
// See the LICENSE file.
var log = require("./log.js")("abrsdk");
function abrsdk(customLogger) {
log = customLogger || log;
var APIHOSTS_LIST = "https://www.adblockradio.com/api/servers";
var isNode = new Function("try {return this===global;}catch(e){return false;}")(); // detect node or browser env
if (isNode) {
var ioc = require("socket.io-client");
} else {
var ioc = require("./node_modules/socket.io-client/dist/socket.io.js");
}
var sio;
var predictionCallback = null;
var statusList = {
STATUS_AD: 2001,
STATUS_NOTAD: 2002,
STATUS_SPEECH: 2003,
STATUS_MUSIC: 2004,
STATUS_NOT_AVAILABLE: 2005,
STATUS_FREEMIUM_DISABLED: 2006,
STATUS_NOT_AVAILABLE_TEMPORARY: 2007,
STATUS_STREAM_BROKEN: 2008
}
var GAIN_REF = 70;
var load = function(path, callback) {
if (isNode) {
//console.log("load: node not implemented");
//callback(null);
(path.slice(0,5) == "https" ? require("https") : require("http")).get(path, (res) => {
var statusCode = res.statusCode;
var contentType = res.headers['content-type'];
var error;
if (statusCode !== 200) {
log.warn("load: path=" + path + " status=" + statusCode);
return callback(null);
}
res.setEncoding('utf8');
var rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', function() {
callback(rawData);
});
}).on('error', (e) => {
callback("http request error: " + e.message, null);
});
} else {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4 && xhttp.status === 200) {
callback(xhttp.responseText); //, xhttp.getResponseHeader("Content-Type"));
}
};
xhttp.onerror = function (e) {
log.warn("load: request failed: path=" + path + " e=" + e);
};
xhttp.open("GET", path, true);
xhttp.send();
}
}
var getServerList = function(callback) {
var hosts;
load(APIHOSTS_LIST, function(data) {
try {
hosts = JSON.parse(data);
} catch (e) {
return callback("could not get api servers list: " + e, []);
}
hosts = hosts.map(item => "https://status." + item)
//log.debug("hosts: " + JSON.stringify(hosts));
callback(null, hosts);
});
}
var newSocket = function(hosts, ihost, callback) {
if (sio && sio.disconnect) {
log.info("previous socket will be closed");
sio.disconnect();
}
if (isNode) {
sio = ioc.connect(hosts[ihost]);
} else {
sio = ioc(hosts[ihost]);
}
sio.once("reconnect", function() {
log.info("reconnected");
callback(null, true);
});
sio.once("connect", function() {
log.info("connected to API host " + hosts[ihost]);
callback(null, true);
});
sio.once("connect_timeout", function() {
log.warn("connect timeout, will attempt to reconnect.");
newSocket(hosts, (ihost + 1) % hosts.length, callback);
});
sio.on("predictions", function(data) {
//log.debug("predictions: " + JSON.stringify(data));
if (predictionCallback) predictionCallback(data);
});
sio.on("disconnect", function() {
log.warn("disconnected, will try to reconnect");
callback(null, false);
newSocket(hosts, (ihost + 1) % hosts.length, callback);
});
};
var connectServer = function(callback) {
getServerList(function(err, hosts) {
if (err) {
return callback("connectServer: error: " + err);
} else {
let ihost = Math.floor(Math.random()*hosts.length); // Math.random() is always < 1
//log.debug("selected host is " + hosts[ihost]);
newSocket(hosts, ihost, callback);
}
});
};
var getSupportedRadios = function(callback) {
sio.emit("supportedRadios", {}, callback);
};
// - radios is an array of strings of the form COUNTRY_RADIONAME,
// with country and radio names matching entries in www.radio-browser.info
// - the token is a string that can be obtained by registering on
// https://www.adblockradio.com/player
// - callback(errors or null, radios array taken into account):
var sendPlaylist = function(radios, token, callback) {
if (!token) return callback("please provide a token", null);
log.info("send playlist update");
sio.emit("playlist", { names: radios, token: token }, function(msg) {
if (msg.error) {
return callback("playlist update problem: " + msg.error, null);
} else {
return callback(null, msg.playlist);
}
});
}
var sendFeedback = function(isPositive, feedbackText, token) {
if (!token) return false;
log.info("send feedback");
sio.emit("feedback", {
token: token,
feedback: {
small: isPositive ? "up" : "dn",
large: feedbackText
}
});
return true;
};
// - radios is an array of strings of the form COUNTRY_RADIONAME,
// with country and radio names matching entries in www.radio-browser.info
var sendFlag = function(radios, token) {
log.info("send flag");
sio.emit("flag", {
token: token,
playlist: radios,
});
};
var setPredictionCallback = function(func) {
predictionCallback = func;
}
return {
_load: load, // for dev only
_newSocket: newSocket, // for dev only
connectServer: connectServer,
getSupportedRadios: getSupportedRadios,
sendPlaylist: sendPlaylist,
sendFeedback: sendFeedback,
sendFlag: sendFlag,
setPredictionCallback: setPredictionCallback,
statusList: statusList,
GAIN_REF: GAIN_REF
}
}
module.exports = abrsdk;