2014-12-02 11:46:39 +00:00
|
|
|
"use strict";
|
2014-11-30 12:21:03 +00:00
|
|
|
var parse = require("url").parse;
|
2014-12-13 00:00:49 +00:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
var PlayMusic = require('../../playmusic');
|
|
|
|
var pm = Promise.promisifyAll(new PlayMusic());
|
2014-12-04 19:17:41 +00:00
|
|
|
|
|
|
|
module.exports.id = "google";
|
2014-11-30 12:21:03 +00:00
|
|
|
|
|
|
|
if (!process.env.GOOGLE_EMAIL || !process.env.GOOGLE_PASSWORD) {
|
2014-12-13 00:00:49 +00:00
|
|
|
console.warn("GOOGLE_EMAIL or GOOGLE_PASSWORD environment variables not found, deactivating Google Play Music.");
|
2014-12-04 19:17:41 +00:00
|
|
|
return;
|
2014-11-30 12:21:03 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 00:13:46 +00:00
|
|
|
var ready = pm.initAsync({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}).catch(function(err) {
|
|
|
|
console.log(err)
|
|
|
|
});
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2014-12-11 19:53:12 +00:00
|
|
|
module.exports.match = require('./url').match;
|
|
|
|
|
|
|
|
module.exports.parseUrl = function(url) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return ready.then(function() {
|
2014-12-11 19:53:12 +00:00
|
|
|
var parsed = parse(url.replace(/\+/g, "%20"));
|
|
|
|
var path = parsed.path;
|
|
|
|
var hash = parsed.hash;
|
|
|
|
if (hash) {
|
|
|
|
var parts = hash.split("/");
|
|
|
|
var type = parts[1];
|
|
|
|
var id = parts[2];
|
|
|
|
var artist = decodeURIComponent(parts[3]);
|
|
|
|
var album = decodeURIComponent(parts[4]);
|
2014-12-28 23:19:18 +00:00
|
|
|
|
|
|
|
if (type != "album" && type != "track") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-11 19:53:12 +00:00
|
|
|
if (id.length > 0) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {id: id, type: type};
|
2014-12-11 19:53:12 +00:00
|
|
|
} else {
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.search({type: type, name:album, artist: {name: artist}});
|
2014-12-11 19:53:12 +00:00
|
|
|
}
|
|
|
|
} else if(path) {
|
|
|
|
var matches = path.match(/\/music\/m\/([\w]+)/);
|
|
|
|
var type = matches[1][0] == "T" ? "track" : "album";
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.lookupId(matches[1], type);
|
2014-12-11 19:53:12 +00:00
|
|
|
}
|
2014-12-28 23:19:18 +00:00
|
|
|
return false;
|
2014-12-13 00:00:49 +00:00
|
|
|
})
|
2014-12-11 19:53:12 +00:00
|
|
|
}
|
2014-12-04 19:17:41 +00:00
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
module.exports.lookupId = function(id, type) {
|
|
|
|
return ready.then(function() {
|
2014-12-05 14:32:03 +00:00
|
|
|
if (type == "album") {
|
2014-12-13 00:00:49 +00:00
|
|
|
return pm.getAlbumAsync(id, false).then(function(album) {
|
|
|
|
return {
|
2014-12-11 19:53:12 +00:00
|
|
|
service: "google",
|
2014-12-05 14:32:03 +00:00
|
|
|
type: "album",
|
|
|
|
id: album.albumId,
|
|
|
|
name: album.name,
|
|
|
|
streamUrl: "https://play.google.com/music/listen#/album/" + album.albumId,
|
|
|
|
purchaseUrl: "https://play.google.com/store/music/album?id=" + album.albumId,
|
2014-12-13 12:05:47 +00:00
|
|
|
artwork: {
|
|
|
|
small: album.albumArtRef.replace("http:", "https:"),
|
|
|
|
large: album.albumArtRef.replace("http:", "https:")
|
|
|
|
},
|
2014-12-05 14:32:03 +00:00
|
|
|
artist: {
|
|
|
|
name: album.artist
|
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
};
|
2014-12-05 17:40:35 +00:00
|
|
|
}, function(error) {
|
2014-12-13 00:00:49 +00:00
|
|
|
throw error;
|
2014-12-02 01:35:10 +00:00
|
|
|
});
|
2014-12-05 14:32:03 +00:00
|
|
|
} else if (type == "track") {
|
2014-12-13 00:00:49 +00:00
|
|
|
return pm.getTrackAsync(id).then(function(track) {
|
|
|
|
return {
|
2014-12-11 19:53:12 +00:00
|
|
|
service: "google",
|
2014-12-05 14:32:03 +00:00
|
|
|
type: "track",
|
|
|
|
id: track.nid,
|
|
|
|
name: track.title,
|
|
|
|
streamUrl: "https://play.google.com/music/listen#/track/" + track.nid + "/" + track.albumId,
|
|
|
|
purchaseUrl: "https://play.google.com/store/music/album?id=" + track.albumId,
|
2014-12-13 12:05:47 +00:00
|
|
|
artwork: {
|
|
|
|
small: track.albumArtRef[0].url.replace("http:", "https:"),
|
|
|
|
large: track.albumArtRef[0].url.replace("http:", "https:")
|
|
|
|
},
|
2014-12-05 14:32:03 +00:00
|
|
|
album: {
|
|
|
|
name: track.album
|
|
|
|
},
|
|
|
|
artist: {
|
|
|
|
name: track.artist
|
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
};
|
2014-12-05 17:40:35 +00:00
|
|
|
}, function(error) {
|
2014-12-13 00:00:49 +00:00
|
|
|
throw error;
|
2014-12-02 01:35:10 +00:00
|
|
|
});
|
2014-12-05 14:32:03 +00:00
|
|
|
}
|
|
|
|
});
|
2014-11-30 12:21:03 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 19:17:41 +00:00
|
|
|
module.exports.search = function(data) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return ready.then(function() {
|
2014-12-05 14:32:03 +00:00
|
|
|
var query, album;
|
|
|
|
var type = data.type;
|
2014-12-03 23:03:34 +00:00
|
|
|
|
2014-12-05 14:32:03 +00:00
|
|
|
if (type == "album") {
|
|
|
|
query = data.artist.name + " " + data.name;
|
|
|
|
album = data.name;
|
|
|
|
} else if (type == "track") {
|
|
|
|
query = data.artist.name + " " + data.album.name + " " + data.name;
|
|
|
|
album = data.album.name;
|
2014-12-04 21:53:50 +00:00
|
|
|
}
|
2014-11-30 12:21:03 +00:00
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
return pm.searchAsync(query, 5).then(function(result) {
|
2014-12-28 23:19:18 +00:00
|
|
|
|
2014-12-05 17:40:35 +00:00
|
|
|
if (!result.entries) {
|
2014-12-05 14:32:03 +00:00
|
|
|
var matches = album.match(/^[^\(\[]+/);
|
2014-12-05 22:20:17 +00:00
|
|
|
if (matches[0] && matches[0] != album) {
|
2014-12-05 14:32:03 +00:00
|
|
|
var cleanedData = JSON.parse(JSON.stringify(data));
|
|
|
|
if (type == "album") {
|
|
|
|
cleanedData.name = matches[0].trim();
|
|
|
|
} else if (type == "track") {
|
2014-12-05 22:26:12 +00:00
|
|
|
cleanedData.album.name = matches[0].trim();
|
2014-12-05 14:32:03 +00:00
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.search(cleanedData);
|
2014-12-05 14:32:03 +00:00
|
|
|
} else {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {service: "googleplaymusic"};
|
2014-12-05 14:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-05 17:40:35 +00:00
|
|
|
var result = result.entries.filter(function(result) {
|
2014-12-05 14:32:03 +00:00
|
|
|
return result[type];
|
|
|
|
}).sort(function(a, b) { // sort by match score
|
|
|
|
return a.score < b.score;
|
|
|
|
}).shift();
|
|
|
|
|
|
|
|
if (!result) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {service: "google"};
|
2014-12-05 14:32:03 +00:00
|
|
|
} else {
|
|
|
|
var id;
|
2014-12-05 14:13:47 +00:00
|
|
|
if (type == "album") {
|
2014-12-05 14:32:03 +00:00
|
|
|
id = result.album.albumId;
|
2014-12-05 14:13:47 +00:00
|
|
|
} else if (type == "track") {
|
2014-12-05 14:32:03 +00:00
|
|
|
id = result.track.nid;
|
2014-12-05 14:13:47 +00:00
|
|
|
}
|
2014-12-04 16:29:39 +00:00
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
return module.exports.lookupId(id, type);
|
2014-12-05 14:32:03 +00:00
|
|
|
}
|
|
|
|
});
|
2014-11-30 12:21:03 +00:00
|
|
|
});
|
|
|
|
}
|