Initial Commit

This commit is contained in:
Jonathan Cremin 2014-11-30 12:21:03 +00:00
commit f8d3ea92b1
12 changed files with 350 additions and 0 deletions

59
lib/googleplaymusic.js Normal file
View file

@ -0,0 +1,59 @@
var parse = require("url").parse;
var PlayMusic = require('playmusic');
var pm = new PlayMusic();
if (!process.env.GOOGLE_EMAIL || !process.env.GOOGLE_PASSWORD) {
throw new Error("You need to set GOOGLE_EMAIL and GOOGLE_PASSWORD environment variables");
}
// 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.lookupId = function(id, type, next) {
pm.getAlbum(id, true, function(album) {
next({
id: album.albumId,
name: album.name,
url: "https://play.google.com/music/listen#/album/" + album.albumId,
artwork: album.albumArtRef.replace("http:", ""),
artist: {
name: album.artist
},
type: "album"
});
});
}
module.exports.search = function(query, type, next) {
pm.search(query, 5, function(data) { // max 5 results
var result = data.entries.filter(function(result) {
return result.album;
}).sort(function(a, b) { // sort by match score
return a.score < b.score;
}).shift();
module.exports.lookupId(result.album.albumId, "album", next);
});
}
module.exports.parseUrl = function(url, next) {
// https://play.google.com/music/listen#/album/B3lxthejqxjxja2bhzchcw5qaci
// https://play.google.com/music/listen#/album//Underworld/Everything%2C+Everything+(Live)
var parsed = parse(url.replace(/\+/g, "%20"));
var hash = parsed.hash;
var matches = hash.match(/\/album[\/]+([^\/]+)\/([^\/]+)/);
if (matches && matches[2]) {
var artist = decodeURIComponent(matches[1]);
var album = decodeURIComponent(matches[2]);
module.exports.search(artist + " " + album, "album", function(googleAlbum) {
next(googleAlbum);
})
} else {
var matches = hash.match(/\/album[\/]+([\w]+)/);
if (matches && matches[1]) {
return next({id:matches[1], type: "album"})
}
}
}

49
lib/spotify.js Normal file
View file

@ -0,0 +1,49 @@
var parse = require('url').parse;
var spotify = require('spotify');
module.exports.lookupId = function(id, type, next) {
spotify.lookup({id: id, type: type}, function(err, data) {
if ( err ) {
console.log('Error occurred: ' + err);
return;
}
var artist = data.artists[0];
next({
id: data.id,
name: data.name,
url: "https://play.spotify.com/album/" + data.id,
artwork: data.images[0].url.replace("http:", ""),
artist: {
name: artist.name
}
})
});
}
module.exports.search = function(query, type, next) {
spotify.search({query: query, type: type}, function(err, data) {
if ( err ) {
console.log('Error occurred: ' + err);
return;
}
album = data.albums.items[0];
module.exports.lookupId(album.id, "album", next);
});
}
module.exports.parseUrl = function(url, next) {
// https://play.spotify.com/album/3W3ENDBQMJ9bD2qmxWI2f0
// https://play.spotify.com/track/3W3ENDBQMJ9bD2qmxWI2f0
// https://open.spotify.com/album/3W3ENDBQMJ9bD2qmxWI2f0
// https://open.spotify.com/track/3W3ENDBQMJ9bD2qmxWI2f0
var matches = parse(url).path.match(/\/album[\/]+([^\/]+)/);
if (matches && matches[1]) {
return next({id:matches[1], type: "album"})
}
}