combine.fm/lib/services/googleplaymusic.js

123 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-12-02 11:46:39 +00:00
"use strict";
2014-11-30 12:21:03 +00:00
var parse = require("url").parse;
var PlayMusic = require('playmusic');
var pm = new PlayMusic();
var Q = require('q');
module.exports.id = "google";
2014-11-30 12:21:03 +00:00
if (!process.env.GOOGLE_EMAIL || !process.env.GOOGLE_PASSWORD) {
console.warn("GOOGLE_EMAIL or GOOGLE_PASSWORD environment variables not found, deactivating Rdio.");
return;
2014-11-30 12:21:03 +00:00
}
// It's probably ok to not wait for this to finish
pm.init({email: process.env.GOOGLE_EMAIL, password: process.env.GOOGLE_PASSWORD}, function() {});
module.exports.match = function(url, type) {
var parsed = parse(url);
return parsed.host.match(/play\.google\.com$/);
};
2014-11-30 12:21:03 +00:00
module.exports.lookupId = function(id, type, next) {
var deferred = Q.defer();
2014-12-02 01:35:10 +00:00
if (type == "album") {
pm.getAlbum(id, true, function(album) {
deferred.resolve({
service: "googleplaymusic",
type: "album",
2014-12-02 01:35:10 +00:00
id: album.albumId,
name: album.name,
2014-12-04 21:11:55 +00:00
streamUrl: "https://play.google.com/music/listen#/album/" + album.albumId,
purchaseUrl: "https://play.google.com/store/music/album?id=" + album.albumId,
2014-12-02 01:35:10 +00:00
artwork: album.albumArtRef.replace("http:", ""),
artist: {
name: album.artist
}
2014-12-02 01:35:10 +00:00
});
2014-11-30 12:21:03 +00:00
});
2014-12-02 01:35:10 +00:00
} else if (type == "track") {
pm.getAllAccessTrack(id, function(track) {
deferred.resolve({
service: "googleplaymusic",
type: "track",
2014-12-02 01:35:10 +00:00
id: track.nid,
name: track.title,
2014-12-04 21:11:55 +00:00
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-02 01:35:10 +00:00
artwork: track.albumArtRef[0].url.replace("http:", ""),
album: {
name: track.album
},
2014-12-02 01:35:10 +00:00
artist: {
name: track.artist
}
2014-12-02 01:35:10 +00:00
});
});
}
return deferred.promise;
2014-11-30 12:21:03 +00:00
}
module.exports.search = function(data) {
var deferred = Q.defer();
var query = "";
var type = data.type;
if (type == "album") {
query = data.artist.name + " " + data.name;
} else if (type == "track") {
query = data.artist.name + " " + data.album.name + " " + data.name;
}
2014-12-04 21:53:50 +00:00
pm.search(query, 5, function(data) {
if (!data.entries) {
deferred.resolve({service: "googleplaymusic"});
return;
}
2014-11-30 12:21:03 +00:00
var result = data.entries.filter(function(result) {
2014-12-02 01:35:10 +00:00
return result[type];
2014-11-30 12:21:03 +00:00
}).sort(function(a, b) { // sort by match score
return a.score < b.score;
}).shift();
2014-12-04 16:29:39 +00:00
if (!result.album && !result.track) {
deferred.resolve({service:"googleplaymusic"});
2014-12-04 16:29:39 +00:00
}
2014-12-02 01:35:10 +00:00
var id;
if (type == "album") {
2014-12-02 01:35:10 +00:00
id = result.album.albumId;
} else if (type == "track") {
2014-12-02 01:35:10 +00:00
id = result.track.nid;
}
module.exports.lookupId(id, type).then(deferred.resolve);
2014-11-30 12:21:03 +00:00
});
return deferred.promise;
2014-11-30 12:21:03 +00:00
}
module.exports.parseUrl = function(url, next) {
var deferred = Q.defer();
2014-11-30 12:21:03 +00:00
var parsed = parse(url.replace(/\+/g, "%20"));
var path = parsed.path;
2014-11-30 12:21:03 +00:00
var hash = parsed.hash;
if (hash) {
2014-12-01 11:53:25 +00:00
var parts = hash.split("/");
var type = parts[1];
var id = parts[2];
var artist = decodeURIComponent(parts[3]);
var album = decodeURIComponent(parts[4]);
2014-11-30 12:21:03 +00:00
2014-12-01 11:53:25 +00:00
if (id.length > 0) {
deferred.resolve({id: id, type: type});
2014-12-01 11:53:25 +00:00
} else {
module.exports.search({type: type, name:album, artist: {name: artist}}).then(deferred.resolve);
2014-11-30 12:21:03 +00:00
}
} else if(path) {
var matches = path.match(/\/music\/m\/([\w]+)/);
2014-12-02 01:35:10 +00:00
var type = matches[1][0] == "T" ? "track" : "album";
module.exports.lookupId(matches[1], type).then(deferred.resolve);
2014-11-30 12:21:03 +00:00
}
return deferred.promise;
2014-11-30 12:21:03 +00:00
}