Skip to content

Commit

Permalink
Fixes issue #806 and issue #814
Browse files Browse the repository at this point in the history
  • Loading branch information
fanciulli committed Nov 5, 2016
1 parent 12e6d63 commit 814b972
Showing 1 changed file with 144 additions and 110 deletions.
254 changes: 144 additions & 110 deletions app/plugins/music_service/mpd/index.js
Expand Up @@ -1765,55 +1765,13 @@ ControllerMpd.prototype.explodeUri = function(uri) {
var cmd = libMpd.cmd;

self.clientMpd.sendCommand(cmd("find artist \""+artist+"\"", []), function (err, msg) {
var list = [];
var albums=[],albumarts=[];
if (msg) {
var path;
var name;
var lines = msg.split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.indexOf('file:') === 0) {
var path = line.slice(6);
var name = path.split('/').pop();

var artist = self.searchFor(lines, i + 1, 'Artist:');
var album = self.searchFor(lines, i + 1, 'Album:');
var title = self.searchFor(lines, i + 1, 'Title:');
var albumart=self.getAlbumArt({artist: artist, album: album}, self.getParentFolder('/mnt/'+path));
var time = parseInt(self.searchFor(lines, i + 1, 'Time:'));

if (title) {
title = title;
} else {
title = name;
}
list.push({
uri: 'music-library/'+path,
service: 'mpd',
name: title,
artist: artist,
album: album,
type: 'track',
tracknumber: 0,
albumart: albumart,
duration: time,
trackType: path.split('.').pop()
});
}

}


defer.resolve(list);


}
else
if(msg=='')
{
self.logger.info(err);
defer.reject(new Error());
self.clientMpd.sendCommand(cmd("find albumartist \""+artist+"\"", []), function (err, msg) {
self.exploderArtist(err,msg,defer);
});
}
else self.exploderArtist(err,msg,defer);
});

}
Expand Down Expand Up @@ -1963,6 +1921,61 @@ ControllerMpd.prototype.explodeUri = function(uri) {
return defer.promise;
};

ControllerMpd.prototype.exploderArtist=function(err,msg,defer)
{
var self=this;

var list = [];
var albums=[],albumarts=[];
if (msg) {
var path;
var name;
var lines = msg.split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.indexOf('file:') === 0) {
var path = line.slice(6);
var name = path.split('/').pop();

var artist = self.searchFor(lines, i + 1, 'Artist:');
var album = self.searchFor(lines, i + 1, 'Album:');
var title = self.searchFor(lines, i + 1, 'Title:');
var albumart=self.getAlbumArt({artist: artist, album: album}, self.getParentFolder('/mnt/'+path));
var time = parseInt(self.searchFor(lines, i + 1, 'Time:'));

if (title) {
title = title;
} else {
title = name;
}
list.push({
uri: 'music-library/'+path,
service: 'mpd',
name: title,
artist: artist,
album: album,
type: 'track',
tracknumber: 0,
albumart: albumart,
duration: time,
trackType: path.split('.').pop()
});
}

}


defer.resolve(list);


}
else
{
self.logger.info(err);
defer.reject(new Error());
}
}

ControllerMpd.prototype.fromUriToPath = function (uri) {
var sections = uri.split('/');
var prev = '';
Expand Down Expand Up @@ -2546,7 +2559,7 @@ ControllerMpd.prototype.listArtists = function () {
};

var cmd = libMpd.cmd;
self.clientMpd.sendCommand(cmd("list", ["artist"]), function (err, msg) {
self.clientMpd.sendCommand(cmd("list", ["albumartist"]), function (err, msg) {
if(err)
defer.reject(new Error('Cannot list artist'));
else
Expand All @@ -2555,9 +2568,9 @@ ControllerMpd.prototype.listArtists = function () {

for(var i in splitted)
{
if(splitted[i].startsWith('Artist:'))
if(splitted[i].startsWith('AlbumArtist:'))
{
var artist=splitted[i].substring(8);
var artist=splitted[i].substring(13);

if(artist!=='')
{
Expand Down Expand Up @@ -2632,77 +2645,98 @@ ControllerMpd.prototype.listArtist = function (curUri,index,previous,uriBegin) {
var cmd = libMpd.cmd;

self.clientMpd.sendCommand(cmd("find artist \""+artist+"\"", []), function (err, msg) {
var list = [];
var albums=[],albumarts=[];
if (msg) {
var path;
var name;
var lines = msg.split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.indexOf('file:') === 0) {
var path = line.slice(6);
var name = path.split('/').pop();

var artist = self.searchFor(lines, i + 1, 'Artist:');
var album = self.searchFor(lines, i + 1, 'Album:');
var title = self.searchFor(lines, i + 1, 'Title:');
var albumart=self.getAlbumArt({artist: artist, album: album}, self.getParentFolder(path),'fa-dot-circle-o');
console.log("ERR: "+err);
console.log("MSG: "+msg+ " "+(msg=='')+ " "+(msg==' '));

if (title) {
title = title;
} else {
title = name;
}
response.navigation.lists[1].items.push({
service: 'mpd',
type: 'song',
title: title,
artist: artist,
album: album,
albumart: albumart,
uri: path
});
if(msg=='')
{
self.clientMpd.sendCommand(cmd("find albumartist \""+artist+"\"", []), function (err, msg) {
self.parseListAlbum(err,msg,defer,response,uriBegin);
});
}
else self.parseListAlbum(err,msg,defer,response,uriBegin);

if(albums.indexOf(album)===-1)
{
albums.push(album);
albumarts.push();

var uri;

if(uriBegin===undefined)
uri='artists://' + nodetools.urlEncode(artist) +'/'+nodetools.urlEncode(album);
else uri=uriBegin+'/'+nodetools.urlEncode(album);

response.navigation.lists[0].items.push(
{
service:'mpd',
type: 'folder',
title: album,
albumart: self.getAlbumArt({artist: artist, album: album}, self.getParentFolder(path),'fa-dot-circle-o'),
uri: uri
});
}
}
});
});

}
return defer.promise;

defer.resolve(response);
};


ControllerMpd.prototype.parseListAlbum= function(err,msg,defer,response,uriBegin)
{
var self=this;
var list = [];
var albums=[],albumarts=[];
if (msg) {


var path;
var name;
var lines = msg.split('\n');

console.log("LINES: "+lines.length);
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.indexOf('file:') === 0) {
var path = line.slice(6);
var name = path.split('/').pop();

var artist = self.searchFor(lines, i + 1, 'Artist:');
var album = self.searchFor(lines, i + 1, 'Album:');
var title = self.searchFor(lines, i + 1, 'Title:');
var albumart=self.getAlbumArt({artist: artist, album: album}, self.getParentFolder(path),'fa-dot-circle-o');

if (title) {
title = title;
} else {
title = name;
}
else
response.navigation.lists[1].items.push({
service: 'mpd',
type: 'song',
title: title,
artist: artist,
album: album,
albumart: albumart,
uri: path
});

if(albums.indexOf(album)===-1)
{
self.logger.info(err);
defer.reject(new Error());
albums.push(album);
albumarts.push();

var uri;

if(uriBegin===undefined)
uri='artists://' + nodetools.urlEncode(artist) +'/'+nodetools.urlEncode(album);
else uri=uriBegin+'/'+nodetools.urlEncode(album);

response.navigation.lists[0].items.push(
{
service:'mpd',
type: 'folder',
title: album,
albumart: self.getAlbumArt({artist: artist, album: album}, self.getParentFolder(path),'fa-dot-circle-o'),
uri: uri
});
}
});
});
}

return defer.promise;
}

};
defer.resolve(response);


}
else
{
self.logger.info(err);
defer.reject(new Error());
}
}


/**
Expand Down

1 comment on commit 814b972

@rezeptpflichtig
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, thx for the fix, however the album list is not yet grouped by albumartist, i think following should do the trick (changing artist to albumartist and setting the split from 7 to 12)

/**
 *
 * list album
 */
ControllerMpd.prototype.listAlbums = function () {
    var self = this;


    var defer = libQ.defer();

    var response = {
        "navigation": {
            "lists": [
                {
                    "availableListViews": [
                        "list",
                        "grid"
                    ],
                    "items": [

                    ]
                }
            ]
        }
    };

    var cmd = libMpd.cmd;
    self.clientMpd.sendCommand(cmd("list", ["album","group","albumartist"]), function (err, msg) {
        if(err)
            defer.reject(new Error('Cannot list albums'));
        else
        {
            var lines=msg.split('\n');

            for (var i = 0; i < lines.length; i++)
            {
                var line=lines[i];
                if (line.indexOf('Album:') === 0) {
                    var albumName = line.slice(6).trim();

                    if(albumName!==undefined && albumName!=='')
                    {
                        var artistName=lines[i+1].slice(12).trim();

                        var album = {service:'mpd',type: 'folder', title: albumName,  artist:artistName,albumart: self.getAlbumArt({artist:artistName,album:albumName},undefined,'fa-dot-circle-o'), uri: 'albums://' + albumName};

                        response.navigation.lists[0].items.push(album);
                    }
                }
            }
            defer.resolve(response);
        }
    });
    return defer.promise;

};

Please sign in to comment.