forked from gabek/node-internet-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (113 loc) · 3.41 KB
/
index.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
var icecast = require('./lib/icecast.js');
var shoutcast = require('./lib/shoutcast.js');
var icystream = require('./lib/icystream.js');
var radionomy = require('./lib/radionomy');
var StreamSource = {
SHOUTCAST_V1: 'SHOUTCAST_V1',
SHOUTCAST_V2: 'SHOUTCAST_V2',
STREAM: 'STREAM',
ICECAST: 'ICECAST',
RADIONOMY: 'RADIONOMY'
};
function getStationInfo(url, callback, method) {
var methodHandler = undefined;
switch (method) {
case StreamSource.SHOUTCAST_V1:
methodHandler = shoutcast.getShoutcastV1Station;
break;
case StreamSource.SHOUTCAST_V2:
methodHandler = shoutcast.getShoutcastV2Station;
break;
case StreamSource.ICECAST:
methodHandler = icecast.getIcecastStation;
break;
case StreamSource.STREAM:
methodHandler = icystream.getStreamStation;
break;
case StreamSource.RADIONOMY:
methodHandler = radionomy.getRadionomyStation;
default:
}
// If we have a specific method to fetch from then
// attempt only that.
if (methodHandler) {
return methodHandler(url, callback);
}
// Resolve the promise from the async function and return the station with the callback
// We shouldnt mix callbacks and promises but for backwards compatability I am breaking
// the law here......
return findStation(url)
.then(station => {
return callback(null, station);
})
.catch(err => {
return callback(err);
});
/*
@params -> string: url of given stream
@returns -> mixed (object if successful, string if error)
*/
async function findStation(url) {
this.results = await V1(url);
// Find which provider has our station
if (this.results == null || typeof this.results == 'undefined') {
this.results = await V2(url);
}
if (this.results == null || typeof this.results == 'undefined') {
this.results = await Ice(url);
}
if (this.results == null || typeof this.results == 'undefined') {
this.results = await Icy(url);
}
return this.results;
//====================================================================================
//= Promise wrapper functions =
//====================================================================================
function V1(url) {
return new Promise((resolve, reject) => {
try {
shoutcast.getShoutcastV1Station(url, function(error, station) {
resolve(station);
});
} catch (err) {
reject(err);
}
});
}
function V2(url) {
return new Promise((resolve, reject) => {
try {
shoutcast.getShoutcastV2Station(url, function(error, station) {
resolve(station);
});
} catch (err) {
reject(err);
}
});
}
function Icy(url) {
return new Promise((resolve, reject) => {
try {
icystream.getStreamStation(url, function(error, station) {
resolve(station);
});
} catch (err) {
reject(err);
}
});
}
function Ice(url) {
return new Promise((resolve, reject) => {
try {
icecast.getIcecastStation(url, function(error, station) {
resolve(station);
});
} catch (err) {
reject(err);
}
});
}
}
}
module.exports.StreamSource = StreamSource;
module.exports.getStationInfo = getStationInfo;