Upgrade cookie-based streamuris to m3u playlist. (Fixes #146).

This commit is contained in:
Nick Steel 2016-02-01 00:12:26 +00:00
parent ee928f5841
commit 45a9c4522a
3 changed files with 52 additions and 31 deletions

View File

@ -480,7 +480,6 @@
<script src="js/library.js"></script>
<script src="js/functionsvars.js"></script>
<script src="js/process_ws.js"></script>
<script src="js/streamuris.js"></script>
<!-- use lastfm -->
<script type="text/javascript" src="js/lastfm.api.md5.js"></script>

View File

@ -538,6 +538,21 @@ function getFavourites() {
});
}
function addToFavourites(newTracks) {
getFavourites().then(function(favourites) {
if (favourites) {
if (favourites.tracks) {
Array.prototype.push.apply(favourites.tracks, newTracks)
} else {
favourites.tracks = [newTracks];
}
mopidy.playlists.save({'playlist': favourites}).then(function(s) {
showFavourites();
});
}
});
}
function addFavourite(uri, name) {
var uri = uri || $('#streamuriinput').val().trim();
var name = name || $('#streamnameinput').val().trim();
@ -548,19 +563,7 @@ function addFavourite(uri, name) {
if (name) {
newTracks[0].name = name; // User overrides name.
}
getFavourites().then(function(favourites) {
if (favourites) {
if (favourites.tracks) {
//Array.prototype.push.apply(favourites.tracks, newTracks)
favourites.tracks.push(newTracks[0]);
} else {
favourites.tracks = [newTracks[0]];
}
mopidy.playlists.save({'playlist': favourites}).then(function(s) {
showFavourites();
});
}
});
addToFavourites(newTracks);
} else {
if (newTracks.length == 0) {
console.log('No tracks to add');
@ -587,9 +590,15 @@ function deleteFavourite(index) {
function showFavourites() {
$('#streamuristable').empty();
$.cookie.json = true;
if ($.cookie('streamUris')) {
toast('Converting streamUris...');
upgradeStreamUrisToFavourites();
}
getFavourites().then(function(favourites) {
if (favourites && favourites.tracks) {
tracks = favourites.tracks;
var tmp = '';
var child = '';
for (var i = 0; i < tracks.length; i++) {
@ -604,6 +613,36 @@ function showFavourites() {
});
}
function upgradeStreamUrisToFavourites() {
$.cookie.json = true;
var streamUris = $.cookie('streamUris'); // Read the cookie.
if (streamUris) {
var uris = []; // Prepare a list of uris to lookup.
for (var key in streamUris) {
var rs = streamUris[key];
if (rs) {
uris.push(rs[1]);
}
}
mopidy.library.lookup({'uris': uris}).then(function(results) {
var tracks = []; // Prepare a list of tracks to add.
for (var key in streamUris) {
var rs = streamUris[key];
if (rs) {
var track = results[rs[1]][0];
track.name = rs[0] || track.name; // Use custom name if provided.
tracks.push(track);
}
}
addToFavourites(tracks);
$.cookie('streamUris', null); // Delete the cookie now we're done.
console.log(tracks.length + " streamUris added to favourites");
});
} else {
console.log("No streamUris cookie found");
}
}
function haltSystem() {
$.post("/settings/shutdown");
toast('Stopping system...', 10000);

View File

@ -1,17 +0,0 @@
/**
* Default streamuris which appear in the webinterface. Edit if you like.
* Take care when editting. Only edit the stuff between ''
* And don't use the default Windows Notepad for this (use Notepad++ on Windows)
*/
var streamUris = [];
//fill with defaults
streamUris.push(['NPR 24', 'http://nprdmp.ic.llnwd.net/stream/nprdmp_live01_mp3']);
streamUris.push(['3FM Dutch', 'http://icecast.omroep.nl/3fm-bb-mp3']);
streamUris.push(['BBC WorldService', 'http://vprbbc.streamguys.net:8000/vprbbc24.mp3']);
streamUris.push(['Arrow Jazz', 'http://81.173.3.132:8082']);
streamUris.push(['Queen Live Aid at Wembley (YouTube)', 'yt:http://www.youtube.com/watch?v=eQsM6u0a038']);
streamUris.push(['Groove Salad - Soma FM', 'http://somafm.com/groovesalad.pls']);
streamUris.push(['Kiss FM Berlin', 'http://stream.kissfm.de/kissfm/mp3-128/internetradio/']);
streamUris.push(['Muse - Newborn (SoundCloud)', 'sc:https://soundcloud.com/muse/01-new-born']);
streamUris.push(['Pinguin Radio', 'http://pr320.pinguinradio.com/listen.pls']);