Skip to content

Commit

Permalink
Merge pull request volumio#260 from ChrisPanda/feature/kbs-fix
Browse files Browse the repository at this point in the history
change KBS radio station server address and minor fix
  • Loading branch information
Ghembs authored Nov 20, 2018
2 parents 1733e51 + cf632cb commit b3411e8
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 175 deletions.
216 changes: 98 additions & 118 deletions plugins/music_service/personal_radio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var crypto = require('crypto');
module.exports = ControllerPersonalRadio;

function ControllerPersonalRadio(context) {
var self = this;
var self = this;

self.context = context;
self.commandRouter = this.context.coreCommand;
Expand Down Expand Up @@ -173,7 +173,7 @@ ControllerPersonalRadio.prototype.handleBrowseUri = function (curUri) {
response = self.getRadioContent('kbs');
}
else if (curUri === 'kradio/sbs') {
response = self.getRadioContent('sbs');
response = self.getRadioContent('sbs');
}
else if (curUri === 'kradio/mbc') {
response = self.getRadioContent('mbc');
Expand All @@ -187,10 +187,10 @@ ControllerPersonalRadio.prototype.handleBrowseUri = function (curUri) {
}

return response
.fail(function (e) {
self.logger.info('[' + Date.now() + '] ' + 'ControllerPersonalRadio::handleBrowseUri failed');
libQ.reject(new Error());
});
.fail(function (e) {
self.logger.info('[' + Date.now() + '] ' + 'ControllerPersonalRadio::handleBrowseUri failed');
libQ.reject(new Error());
});
};

ControllerPersonalRadio.prototype.getRootContent = function() {
Expand All @@ -201,14 +201,14 @@ ControllerPersonalRadio.prototype.getRootContent = function() {
response = self.rootNavigation;
response.navigation.lists[0].items = [];
for (var key in self.rootStations) {
var radio = {
service: self.serviceName,
type: 'folder',
title: self.rootStations[key].title,
icon: 'fa fa-folder-open-o',
uri: self.rootStations[key].uri
};
response.navigation.lists[0].items.push(radio);
var radio = {
service: self.serviceName,
type: 'folder',
title: self.rootStations[key].title,
icon: 'fa fa-folder-open-o',
uri: self.rootStations[key].uri
};
response.navigation.lists[0].items.push(radio);
}
defer.resolve(response);
return defer.promise;
Expand Down Expand Up @@ -258,35 +258,35 @@ ControllerPersonalRadio.prototype.clearAddPlayTrack = function(track) {
var defer = libQ.defer();

return self.mpdPlugin.sendMpdCommand('stop', [])
.then(function() {
return self.mpdPlugin.sendMpdCommand('clear', []);
})
.then(function() {
return self.mpdPlugin.sendMpdCommand('add "'+track.uri+'"',[]);
})
.then(function () {
self.commandRouter.pushToastMessage('info',
.then(function() {
return self.mpdPlugin.sendMpdCommand('clear', []);
})
.then(function() {
return self.mpdPlugin.sendMpdCommand('add "'+track.uri+'"',[]);
})
.then(function () {
self.commandRouter.pushToastMessage('info',
self.getRadioI18nString('PLUGIN_NAME'),
self.getRadioI18nString('WAIT_FOR_RADIO_CHANNEL'));

return self.mpdPlugin.sendMpdCommand('play', []).then(function () {
switch (track.radioType) {
case 'kbs':
case 'sbs':
case 'mbc':
return self.mpdPlugin.getState().then(function (state) {
return self.commandRouter.stateMachine.syncState(state, self.serviceName);
});
break;
default:
self.commandRouter.stateMachine.setConsumeUpdateService('mpd');
return libQ.resolve();
}
return self.mpdPlugin.sendMpdCommand('play', []).then(function () {
switch (track.radioType) {
case 'kbs':
case 'sbs':
case 'mbc':
return self.mpdPlugin.getState().then(function (state) {
return self.commandRouter.stateMachine.syncState(state, self.serviceName);
});
break;
default:
self.commandRouter.stateMachine.setConsumeUpdateService('mpd');
return libQ.resolve();
}
})
})
})
.fail(function (e) {
return defer.reject(new Error());
});
.fail(function (e) {
return defer.reject(new Error());
});
};

ControllerPersonalRadio.prototype.seek = function (position) {
Expand All @@ -296,17 +296,17 @@ ControllerPersonalRadio.prototype.seek = function (position) {
};

ControllerPersonalRadio.prototype.stop = function() {
var self = this;
var self = this;

self.commandRouter.pushToastMessage(
'info',
self.getRadioI18nString('PLUGIN_NAME'),
self.getRadioI18nString('STOP_RADIO_CHANNEL')
);
return self.mpdPlugin.stop().then(function () {
return self.mpdPlugin.getState().then(function (state) {
return self.commandRouter.stateMachine.syncState(state, self.serviceName);
});
return self.mpdPlugin.getState().then(function (state) {
return self.commandRouter.stateMachine.syncState(state, self.serviceName);
});
});
};

Expand All @@ -315,7 +315,7 @@ ControllerPersonalRadio.prototype.pause = function() {

return self.mpdPlugin.pause().then(function () {
return self.mpdPlugin.getState().then(function (state) {
return self.commandRouter.stateMachine.syncState(state, self.serviceName);
return self.commandRouter.stateMachine.syncState(state, self.serviceName);
});
});
};
Expand All @@ -325,7 +325,7 @@ ControllerPersonalRadio.prototype.resume = function() {

return self.mpdPlugin.resume().then(function () {
return self.mpdPlugin.getState().then(function (state) {
return self.commandRouter.stateMachine.syncState(state, self.serviceName);
return self.commandRouter.stateMachine.syncState(state, self.serviceName);
});
});
};
Expand All @@ -341,40 +341,25 @@ ControllerPersonalRadio.prototype.explodeUri = function (uri) {

station = uris[0].substring(3);
response = {
service: self.serviceName,
type: 'track',
trackType: self.getRadioI18nString('PLUGIN_NAME'),
radioType: station,
albumart: '/albumart?sourceicon=music_service/personal_radio/'+station+'.svg'
service: self.serviceName,
type: 'track',
trackType: self.getRadioI18nString('PLUGIN_NAME'),
radioType: station,
albumart: '/albumart?sourceicon=music_service/personal_radio/'+station+'.svg'
};

switch (uris[0]) {
case 'webkbs':
var userId = Math.random().toString(36).substring(2, 6) +
Math.random().toString(36).substring(2, 6);
query = {
id: userId,
channel: channel+1
};
self.getStreamUrl(station, self.baseKbsStreamUrl, query)
.then(function (responseUrl) {
if (responseUrl !== null) {
var result = responseUrl.split("\n");
var retCode = parseInt(result[0]);
var streamUrl;
if (retCode === 0)
streamUrl = result[1];
else {
streamUrl = null;
self.errorToast(station, 'INCORRECT_RESPONSE');
var radioChannel = self.radioStations.kbs[channel].channel;
self.getStreamUrl(station, self.baseKbsStreamUrl + radioChannel, "")
.then(function (responseUrl) {
if (responseUrl !== null) {
response["uri"] = responseUrl.channel_item[0].service_url;
response["name"] = self.radioStations.kbs[channel].title;
response["title"] = self.radioStations.kbs[channel].title;
}

response["uri"] = streamUrl;
response["name"] = self.radioStations.kbs[channel].title;
response["title"] = self.radioStations.kbs[channel].title;
}
defer.resolve(response);
});
defer.resolve(response);
});
break;

case 'websbs':
Expand All @@ -386,18 +371,18 @@ ControllerPersonalRadio.prototype.explodeUri = function (uri) {

var baseSbsStreamUrl = self.baseSbsStreamUrl + self.radioStations.sbs[channel].channel;
self.getStreamUrl(station, baseSbsStreamUrl, {device: device})
.then(function (responseUrl) {
if (responseUrl !== null) {
var decipher = crypto.createDecipheriv(self.sbsAlgorithm, self.sbsKey, "");
var streamUrl = decipher.update(responseUrl, 'base64', 'utf8');
streamUrl += decipher.final('utf8');

response["uri"] = streamUrl;
response["name"] = self.radioStations.sbs[channel].title;
response["title"] = self.radioStations.sbs[channel].title;
}
defer.resolve(response);
});
.then(function (responseUrl) {
if (responseUrl !== null) {
var decipher = crypto.createDecipheriv(self.sbsAlgorithm, self.sbsKey, "");
var streamUrl = decipher.update(responseUrl, 'base64', 'utf8');
streamUrl += decipher.final('utf8');

response["uri"] = streamUrl;
response["name"] = self.radioStations.sbs[channel].title;
response["title"] = self.radioStations.sbs[channel].title;
}
defer.resolve(response);
});
break;

case 'webmbc':
Expand All @@ -417,21 +402,14 @@ ControllerPersonalRadio.prototype.explodeUri = function (uri) {
protocol: protocol
};
self.getStreamUrl(station, self.baseMbcStreamUrl, query)
.then(function (responseUrl) {
if (responseUrl !== null) {
var result = JSON.parse(responseUrl.replace(/\(|\)|\;/g, ''));
var streamUrl = result.AACLiveURL;
if (streamUrl === undefined) {
streamUrl = null;
self.errorToast(station, 'INCORRECT_RESPONSE');
.then(function (responseUrl) {
if (responseUrl !== null) {
response["uri"] = responseUrl;
response["name"] = self.radioStations.mbc[channel].title;
response["title"] = self.radioStations.mbc[channel].title;
}

response["uri"] = streamUrl;
response["name"] = self.radioStations.mbc[channel].title;
response["title"] = self.radioStations.mbc[channel].title;
}
defer.resolve(response);
});
defer.resolve(response);
});
break;

case 'weblinn':
Expand Down Expand Up @@ -484,15 +462,15 @@ ControllerPersonalRadio.prototype.getStreamUrl = function (station, url, query)

var Request = unirest.get(url);
Request
.query(query)
.end(function (response) {
if (response.status === 200)
defer.resolve(response.body);
else {
defer.resolve(null);
self.errorToast(station, 'ERROR_STREAM_SERVER');
}
});
.query(query)
.end(function (response) {
if (response.status === 200)
defer.resolve(response.body);
else {
defer.resolve(null);
self.errorToast(station, 'ERROR_STREAM_SERVER');
}
});

return defer.promise;
};
Expand All @@ -517,13 +495,13 @@ ControllerPersonalRadio.prototype.addRadioResource = function() {
self.radioStations.kbs[2].title = self.getRadioI18nString('KBS1_RADIO');
self.radioStations.kbs[3].title = self.getRadioI18nString('KBS2_RADIO');
self.radioStations.kbs[4].title = self.getRadioI18nString('KBS3_RADIO');
self.radioStations.kbs[6].title = self.getRadioI18nString('KBS_UNION');
self.radioStations.kbs[7].title = self.getRadioI18nString('KBS_WORLD');
self.radioStations.kbs[5].title = self.getRadioI18nString('KBS_UNION');
self.radioStations.kbs[6].title = self.getRadioI18nString('KBS_WORLD');
self.radioStations.mbc[0].title = self.getRadioI18nString('MBC_STANDARD');
self.radioStations.mbc[1].title = self.getRadioI18nString('MBC_FM4U');
self.radioStations.mbc[2].title = self.getRadioI18nString('MBC_CHANNEL_M');
self.radioStations.sbs[0].title = self.getRadioI18nString('SBS_POWER_FM');
self.radioStations.sbs[1].title = self.getRadioI18nString('SBS_LOVE_FM');
self.radioStations.sbs[0].title = self.getRadioI18nString('SBS_LOVE_FM');
self.radioStations.sbs[1].title = self.getRadioI18nString('SBS_POWER_FM');
self.radioStations.sbs[2].title = self.getRadioI18nString('SBS_INTERNET_RADIO');

// Korean radio streaming server preparing
Expand Down Expand Up @@ -564,16 +542,18 @@ ControllerPersonalRadio.prototype.getRadioI18nString = function (key) {
ControllerPersonalRadio.prototype.decodeStreamUrl =
function (algorithm, secretKey, encodedUri) {

var decipherObj = crypto.createDecipher(algorithm, secretKey);
var streamUrl = decipherObj.update(encodedUri, 'hex', 'utf8');
streamUrl += decipherObj.final('utf8');
var decipherObj = crypto.createDecipher(algorithm, secretKey);
var streamUrl = decipherObj.update(encodedUri, 'hex', 'utf8');
streamUrl += decipherObj.final('utf8');

return streamUrl;
};
return streamUrl;
};

ControllerPersonalRadio.prototype.errorToast = function (station, msg) {
var errorMessage = self.getRadioI18nString(msg);
errorMessage.replace('{0}', station.toUpperCase());
self.commandRouter.pushToastMessage('error',
self.getRadioI18nString('PLUGIN_NAME'), errorMessage);
};


Loading

0 comments on commit b3411e8

Please sign in to comment.