registerPlugin({
name: 'CoverArt',
version: '1.0',
description: 'Automagically downloads album art from theaudiodb.com.',
author: 'Michael Friese <[email protected]>',
vars: {}
}, function(sinusbot, config) {
var invalids = [];
sinusbot.on('track', function(ev) {
if (invalids.indexOf(ev.uuid) >= 0) return;
if (ev.title != '' && ev.artist != '' && !ev.thumbnail && (ev.type === '' || ev.type == 'file')) {
sinusbot.log('Searching for thumbnail for ' + ev.artist + ' - ' + ev.title);
var out = sinusbot.http({ method: 'GET', url: 'http://www.theaudiodb.com/api/v1/json/1/searchtrack.php?s=' + encodeURIComponent(ev.artist) + '&t=' + encodeURIComponent(ev.title) }, function (err, res) {
if(out) {
if (res && res.data) {
var ares = JSON.parse(res.data);
if (ares && ares.track && ares.track.length > 0) {
var track = ares.track[0];
sinusbot.log('AlbumTrackId: ' + track.idAlbum);
var ret = http({ method: 'GET', url: 'http://www.theaudiodb.com/api/v1/json/1/album.php?m=' + track.idAlbum }, function (err, res) {
if(ret) {
if (res && res.data) {
var ares = JSON.parse(res.data);
if (ares && ares.album && ares.album.length > 0) {
var album = ares.album[0];
sinusbot.log('AlbumThumbUrl: ' + album.strAlbumThumb);
if (sinusbot.downloadTrackThumbnail(ev.uuid, album.strAlbumThumb)) {
sinusbot.log('Updated thumbnail!');
} else {
sinusbot.log('Thumbnail failed!');
invalids.push(ev.uuid);
}
}
else {
sinusbot.log('2:(ares && ares.album && ares.album.length > 0)==null');
invalids.push(ev.uuid);
}
}
else {
sinusbot.log('2:(res && res.data)==null');
invalids.push(ev.uuid);
}
}
else {
sinusbot.log('2:(ret)==null');
invalids.push(ev.uuid);
}
});
}
else {
sinusbot.log('1:(ares && ares.track && ares.track.length > 0)==false');
invalids.push(ev.uuid);
}
}
else {
sinusbot.log('1:(res && res.data false)==false');
invalids.push(ev.uuid);
}
}
else {
sinusbot.log('1:(out)== null');
invalids.push(ev.uuid);
}
});
}
});
});