Add support for sharing tracks

This commit is contained in:
Jonathan Cremin 2014-12-02 01:35:10 +00:00
parent 39a22f65d9
commit 7e6527855b
5 changed files with 72 additions and 37 deletions

View file

@ -10,29 +10,51 @@ if (!process.env.GOOGLE_EMAIL || !process.env.GOOGLE_PASSWORD) {
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"
if (type == "album") {
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: type
});
});
});
} else if (type == "track") {
pm.getAllAccessTrack(id, function(track) {
next({
id: track.nid,
name: track.title,
url: "https://play.google.com/music/listen#/track/" + track.nid + "/" + track.albumId,
artwork: track.albumArtRef[0].url.replace("http:", ""),
artist: {
name: track.artist
},
type: type
});
});
}
}
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;
return result[type];
}).sort(function(a, b) { // sort by match score
return a.score < b.score;
}).shift();
module.exports.lookupId(result.album.albumId, "album", next);
var id;
if (result.album) {
id = result.album.albumId;
} else if (result.track) {
id = result.track.nid;
}
module.exports.lookupId(id, type, next);
});
}
@ -56,6 +78,7 @@ module.exports.parseUrl = function(url, next) {
}
} else if(path) {
var matches = path.match(/\/music\/m\/([\w]+)/);
return next({id:matches[1], type: "album"});
var type = matches[1][0] == "T" ? "track" : "album";
module.exports.lookupId(matches[1], type, next);
}
}

View file

@ -18,6 +18,7 @@ module.exports.lookupId = function(id, next) {
var result = JSON.parse(results).result;
var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", "");
var type = result.album ? "track" : "album";
next({
id: id,
name: result.name,
@ -26,7 +27,7 @@ module.exports.lookupId = function(id, next) {
artist: {
name: result.artist
},
type: "album"
type: type
});
});
};
@ -54,6 +55,7 @@ module.exports.lookupUrl = function(url, next) {
var result = JSON.parse(results).result;
var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", "");
var type = result.album ? "track" : "album";
next({
id: id,
name: result.name,
@ -62,7 +64,7 @@ module.exports.lookupUrl = function(url, next) {
artist: {
name: result.artist
},
type: "album"
type: type
});
});
};
@ -74,6 +76,9 @@ module.exports.search = function(query, type, next) {
types: type,
}, function(err, results) {
var result = JSON.parse(results).result.results[0];
if (!result) {
return next({});
}
var parsed = parse(result.shortUrl)
var id = parsed.path.replace("/x/", "").replace("/", "");
next({
@ -84,7 +89,7 @@ module.exports.search = function(query, type, next) {
artist: {
name: result.artist
},
type: "album"
type: type
});
});
};

View file

@ -13,8 +13,8 @@ module.exports.lookupId = function(id, type, next) {
next({
id: data.id,
name: data.name,
url: "https://play.spotify.com/album/" + data.id,
artwork: data.images[0].url.replace("http:", ""),
url: "https://play.spotify.com/" + type + "/" + data.id,
artwork: data.images ? data.images[0].url.replace("http:", "") : data.album.images[0].url.replace("http:", ""),
artist: {
name: artist.name
}
@ -29,16 +29,16 @@ module.exports.search = function(query, type, next) {
return;
}
album = data.albums.items[0];
var item = data[type + "s"].items[0];
module.exports.lookupId(album.id, "album", next);
module.exports.lookupId(item.id, type, next);
});
}
module.exports.parseUrl = function(url, next) {
var matches = parse(url).path.match(/\/album[\/]+([^\/]+)/);
var matches = parse(url).path.match(/\/(album|track)[\/]+([^\/]+)/);
if (matches && matches[1]) {
next({id:matches[1], type: "album"})
if (matches && matches[2]) {
next({id:matches[2], type: matches[1]})
}
}