combine.fm/lib/services/google/index.js

149 lines
4.4 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 Promise = require('bluebird');
var PlayMusic = require('../../playmusic');
var pm = Promise.promisifyAll(new PlayMusic());
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 Google Play Music.");
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
module.exports.match = require('./url').match;
module.exports.parseUrl = function(url) {
return ready.then(function() {
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]);
if (type != "album" && type != "track") {
return false;
}
if (id.length > 0) {
return {id: id, type: type};
} else {
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";
return module.exports.lookupId(matches[1], type);
}
return false;
})
}
module.exports.lookupId = function(id, type) {
return ready.then(function() {
2014-12-05 14:32:03 +00:00
if (type == "album") {
return pm.getAlbumAsync(id, false).then(function(album) {
return {
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-05 17:40:35 +00:00
}, function(error) {
throw error;
2014-12-02 01:35:10 +00:00
});
2014-12-05 14:32:03 +00:00
} else if (type == "track") {
return pm.getTrackAsync(id).then(function(track) {
return {
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-05 17:40:35 +00:00
}, function(error) {
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
}
module.exports.search = function(data) {
return ready.then(function() {
2014-12-05 14:32:03 +00:00
var query, album;
var type = data.type;
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
return pm.searchAsync(query, 5).then(function(result) {
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
}
return module.exports.search(cleanedData);
2014-12-05 14:32:03 +00:00
} else {
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) {
return {service: "google"};
2014-12-05 14:32:03 +00:00
} else {
var id;
if (type == "album") {
2014-12-05 14:32:03 +00:00
id = result.album.albumId;
} else if (type == "track") {
2014-12-05 14:32:03 +00:00
id = result.track.nid;
}
2014-12-04 16:29:39 +00:00
return module.exports.lookupId(id, type);
2014-12-05 14:32:03 +00:00
}
});
2014-11-30 12:21:03 +00:00
});
}