Update the rest of the libraries for bluebird
This commit is contained in:
parent
90c1385fb3
commit
17de5e9b92
8 changed files with 591 additions and 144 deletions
|
@ -1,27 +1,22 @@
|
|||
"use strict";
|
||||
var parse = require("url").parse;
|
||||
var PlayMusic = require('playmusic');
|
||||
var pm = new PlayMusic();
|
||||
var Q = require('q');
|
||||
var Promise = require('bluebird');
|
||||
var PlayMusic = require('../../playmusic');
|
||||
var pm = Promise.promisifyAll(new PlayMusic());
|
||||
|
||||
module.exports.id = "google";
|
||||
|
||||
if (!process.env.GOOGLE_EMAIL || !process.env.GOOGLE_PASSWORD) {
|
||||
console.warn("GOOGLE_EMAIL or GOOGLE_PASSWORD environment variables not found, deactivating Rdio.");
|
||||
console.warn("GOOGLE_EMAIL or GOOGLE_PASSWORD environment variables not found, deactivating Google Play Music.");
|
||||
return;
|
||||
}
|
||||
|
||||
var ready = Q.defer();
|
||||
|
||||
pm.init({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}, function() {
|
||||
ready.resolve();
|
||||
});
|
||||
var ready = pm.initAsync({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD});
|
||||
|
||||
module.exports.match = require('./url').match;
|
||||
|
||||
module.exports.parseUrl = function(url) {
|
||||
var deferred = Q.defer();
|
||||
ready.promise.then(function() {
|
||||
return ready.then(function() {
|
||||
var parsed = parse(url.replace(/\+/g, "%20"));
|
||||
var path = parsed.path;
|
||||
var hash = parsed.hash;
|
||||
|
@ -33,25 +28,23 @@ module.exports.parseUrl = function(url) {
|
|||
var album = decodeURIComponent(parts[4]);
|
||||
|
||||
if (id.length > 0) {
|
||||
deferred.resolve({id: id, type: type});
|
||||
return {id: id, type: type};
|
||||
} else {
|
||||
module.exports.search({type: type, name:album, artist: {name: artist}}).then(deferred.resolve);
|
||||
return module.exports.search({type: type, name:album, artist: {name: artist}});
|
||||
}
|
||||
} else if(path) {
|
||||
var matches = path.match(/\/music\/m\/([\w]+)/);
|
||||
var type = matches[1][0] == "T" ? "track" : "album";
|
||||
module.exports.lookupId(matches[1], type).then(deferred.resolve);
|
||||
return module.exports.lookupId(matches[1], type);
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.lookupId = function(id, type, next) {
|
||||
var deferred = Q.defer();
|
||||
ready.promise.then(function() {
|
||||
module.exports.lookupId = function(id, type) {
|
||||
return ready.then(function() {
|
||||
if (type == "album") {
|
||||
pm.getAlbum(id, false, function(album) {
|
||||
deferred.resolve({
|
||||
return pm.getAlbumAsync(id, false).then(function(album) {
|
||||
return {
|
||||
service: "google",
|
||||
type: "album",
|
||||
id: album.albumId,
|
||||
|
@ -62,13 +55,13 @@ module.exports.lookupId = function(id, type, next) {
|
|||
artist: {
|
||||
name: album.artist
|
||||
}
|
||||
});
|
||||
};
|
||||
}, function(error) {
|
||||
deferred.reject(error);
|
||||
throw error;
|
||||
});
|
||||
} else if (type == "track") {
|
||||
pm.getAllAccessTrack(id, function(track) {
|
||||
deferred.resolve({
|
||||
return pm.getTrackAsync(id).then(function(track) {
|
||||
return {
|
||||
service: "google",
|
||||
type: "track",
|
||||
id: track.nid,
|
||||
|
@ -82,18 +75,16 @@ module.exports.lookupId = function(id, type, next) {
|
|||
artist: {
|
||||
name: track.artist
|
||||
}
|
||||
});
|
||||
};
|
||||
}, function(error) {
|
||||
deferred.reject(error);
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
module.exports.search = function(data) {
|
||||
var deferred = Q.defer();
|
||||
ready.promise.then(function() {
|
||||
return ready.then(function() {
|
||||
var query, album;
|
||||
var type = data.type;
|
||||
|
||||
|
@ -105,7 +96,7 @@ module.exports.search = function(data) {
|
|||
album = data.album.name;
|
||||
}
|
||||
|
||||
pm.search(query, 5, function(result) {
|
||||
return pm.searchAsync(query, 5).then(function(result) {
|
||||
if (!result.entries) {
|
||||
var matches = album.match(/^[^\(\[]+/);
|
||||
if (matches[0] && matches[0] != album) {
|
||||
|
@ -115,11 +106,10 @@ module.exports.search = function(data) {
|
|||
} else if (type == "track") {
|
||||
cleanedData.album.name = matches[0].trim();
|
||||
}
|
||||
module.exports.search(cleanedData).then(deferred.resolve);
|
||||
return module.exports.search(cleanedData);
|
||||
} else {
|
||||
deferred.resolve({service: "googleplaymusic"});
|
||||
return {service: "googleplaymusic"};
|
||||
}
|
||||
return;
|
||||
}
|
||||
var result = result.entries.filter(function(result) {
|
||||
return result[type];
|
||||
|
@ -128,7 +118,7 @@ module.exports.search = function(data) {
|
|||
}).shift();
|
||||
|
||||
if (!result) {
|
||||
deferred.resolve({service: "google"});
|
||||
return {service: "google"};
|
||||
} else {
|
||||
var id;
|
||||
if (type == "album") {
|
||||
|
@ -137,9 +127,8 @@ module.exports.search = function(data) {
|
|||
id = result.track.nid;
|
||||
}
|
||||
|
||||
module.exports.lookupId(id, type).then(deferred.resolve);
|
||||
return module.exports.lookupId(id, type);
|
||||
}
|
||||
});
|
||||
});
|
||||
return deferred.promise;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue