Skip to content

Commit

Permalink
restoring...
Browse files Browse the repository at this point in the history
  • Loading branch information
roncioso committed Jan 9, 2011
1 parent e3ed509 commit 1cc376d
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 283 deletions.
7 changes: 0 additions & 7 deletions .idea/vcs.xml

This file was deleted.

239 changes: 0 additions & 239 deletions .idea/workspace.xml

This file was deleted.

75 changes: 56 additions & 19 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
var EventEmitter = require('events').EventEmitter,
request = require('request');
var request = require('request');

var detailMap = {
var SpotifyMeta = function(){}

SpotifyMeta.prototype._detailsMap = {
"artist": "album",
"album": "track"
}

var SpotifyMeta = function(){}
};

SpotifyMeta.prototype.lookup = function(id, options, cb){

if(!id){
throw new Error("Nothing to parse...");
}

options = options || {};

var opts = {
id: id,
reqID: "",
type: options.type || "",
detail: "",
detailLevel: options.detail || 0,
callback: cb || function(){},
detailMap: {
"artist": "album",
"album": "track"
}
callback: cb || function(){}
}

//Adjust callback
Expand All @@ -38,36 +35,76 @@ SpotifyMeta.prototype.lookup = function(id, options, cb){
opts.type = parsedUrl["type"];
opts.id = parsedUrl["id"];
}
if(opts.type){
opts.reqID = "spotify:"+opts.type+":"+opts.id;

if(this._isSpotifyInternalUrl(id)){
var splittedUrl = id.split(":");
opts.type = splittedUrl[1];
opts.id = splittedUrl[2];
}

if(!opts.type || !opts.id){
return opts.callback("ERROR! not a valid url", opts.id, opts.type, null);
}

//TODO refactor this
if(opts.detailLevel && opts.detailMap[opts.type]){
opts.reqID = "spotify:"+opts.type+":"+opts.id;

if(opts.detailLevel && this._detailsMap[opts.type]){
opts.detail += "&extras="+opts.detailMap[opts.type];
if(opts.detailLevel > 1) opts.detail += "detail";
opts.reqID += opts.detail;
}

var self = this;
var r = request({
uri: "http://ws.spotify.com/lookup/1/.json?uri="+opts.reqID
}, function(e,r,b){
opts.callback(opts.id,opts.type,b);
}, function(error, r, response){
if(!error){
response = JSON.parse(response);
opts.callback(null, opts.id, opts.type, response);
} else {
return opts.callback("ERROR! something went wrong", opts.id, opts.type, null);
}
})
}

SpotifyMeta.prototype._isOpenSpotifyUrl = function(data){
return (data.match(/http:\/\/open.spotify.com\//gi)) ? true : false;
return (data.match(/open.spotify.com\/(.*)\/(.*)/gi)) ? true : false;
}

SpotifyMeta.prototype._isSpotifyInternalUrl = function(data){
return (data.match(/spotify:(.*):(.*)/gi)) ? true : false;
}

SpotifyMeta.prototype._parseUrl = function(url){
var u = url.replace(/http:\/\/open.spotify.com\//gi, "").split("/");
var u = url.replace(/(http:\/\/)?open.spotify.com\//gi, "").split("/");
return {
type: u[0],
id: u[1]
}
}

SpotifyMeta.prototype.searchArtist = function(q, cb){
this._singleSearch(q, "artist", cb);
}

SpotifyMeta.prototype.searchAlbum = function(q, cb){
this._singleSearch(q, "album", cb);
}

SpotifyMeta.prototype.searchTrack = function(q, cb){
this._singleSearch(q, "track", cb);
}

SpotifyMeta.prototype._singleSearch = function(q, type, cb){
q = encodeURI(q);
request({
uri: "http://ws.spotify.com/search/1/"+type+".json?q="+type+":"+q
}, function(e,r,b){
b = JSON.parse(b);
cb(b[type+"s"][0]);
});
}

SpotifyMeta.prototype.search = function(query){
var results = [],
counter = 0,
Expand Down
Loading

0 comments on commit 1cc376d

Please sign in to comment.