Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor pair lib, export only a single method
- Loading branch information
1 parent
63295ee
commit 7f432db
Showing
2 changed files
with
54 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,75 @@ | ||
var querystring = require('querystring'); | ||
var request = require('request'); | ||
|
||
function format(string, object) { | ||
return string.replace(/\$\{(.+?)\}/g, function(match, key) { | ||
return object[key]; | ||
}); | ||
} | ||
|
||
exports.get = function(tag, callback) { | ||
var replies = 0; | ||
var content = {}; | ||
function done() { | ||
replies += 1; | ||
if (replies === 2) { | ||
callback(content); | ||
} | ||
} | ||
// see http://www.flickr.com/services/api/flickr.photos.search.html | ||
request.get('http://api.flickr.com/services/rest/?' + querystring.stringify({ | ||
// see http://www.flickr.com/services/api/flickr.photos.search.html | ||
function flickrUrl(tag) { | ||
return 'http://api.flickr.com/services/rest/?' + querystring.stringify({ | ||
nojsoncallback: 1, | ||
format: 'json', | ||
per_page: 1, | ||
api_key: 'd95cee71d49de386fba7a2290de116eb', | ||
method: 'flickr.photos.search', | ||
safe_search: 1, | ||
tags: tag | ||
}), function(error, response, body) { | ||
var parsed = JSON.parse(body); | ||
var photos = parsed.photos.photo.map(function(photo) { | ||
return { | ||
title: photo.title, | ||
src: format('http://farm${farm}.static.flickr.com/${server}/${id}_${secret}_b.jpg', photo), | ||
href: format('http://www.flickr.com/photos/${owner}/${id}/', photo) | ||
}; | ||
}); | ||
content.flickr = photos[0]; | ||
done(); | ||
}); | ||
} | ||
function format(string, object) { | ||
return string.replace(/\$\{(.+?)\}/g, function(match, key) { | ||
return object[key]; | ||
}); | ||
} | ||
function parseFlickr(data) { | ||
var parsed = JSON.parse(data); | ||
var photos = parsed.photos.photo.map(function(photo) { | ||
return { | ||
title: photo.title, | ||
src: format('http://farm${farm}.static.flickr.com/${server}/${id}_${secret}_b.jpg', photo), | ||
href: format('http://www.flickr.com/photos/${owner}/${id}/', photo) | ||
}; | ||
}); | ||
return photos[0]; | ||
} | ||
|
||
// see http://developers.soundcloud.com/docs/api/tracks#filtering | ||
var sc_client_id = '515a1fcab907701f29fe6f1f7dabc556'; | ||
request.get('http://api.soundcloud.com/tracks.json?' + querystring.stringify({ | ||
// see http://developers.soundcloud.com/docs/api/tracks#filtering | ||
var sc_client_id = '515a1fcab907701f29fe6f1f7dabc556'; | ||
function soundcloudUrl(tag) { | ||
return 'http://api.soundcloud.com/tracks.json?' + querystring.stringify({ | ||
// TODO try to limit to one result | ||
tags: tag, | ||
client_id: sc_client_id | ||
}), function(error, response, body) { | ||
var result = JSON.parse(body)[0]; | ||
if (result) { | ||
result.stream_url += '?client_id=' + sc_client_id; | ||
content.soundcloud = result; | ||
} else { | ||
content.soundcloud = { | ||
title: "Nothing found" | ||
}; | ||
}); | ||
} | ||
function parseSoundcloud(data) { | ||
var result = JSON.parse(data)[0]; | ||
if (!result) { | ||
return { | ||
title: "Nothing found" | ||
}; | ||
} | ||
result.stream_url += '?client_id=' + sc_client_id; | ||
return result; | ||
} | ||
|
||
module.exports = function(tag, callback) { | ||
var replies = 0; | ||
var content = {}; | ||
function done(type, result) { | ||
content[type] = result; | ||
replies += 1; | ||
if (replies === 2) { | ||
callback(content); | ||
} | ||
done(); | ||
} | ||
request.get(flickrUrl(tag), function(error, response, body) { | ||
done('flickr', parseFlickr(body)); | ||
}); | ||
request.get(soundcloudUrl(tag), function(error, response, body) { | ||
done('soundcloud', parseSoundcloud(body)); | ||
}); | ||
}; | ||
/* | ||
exports.get("alligator", function(response) { | ||
module.exports("alligator", function(response) { | ||
console.log("results", JSON.stringify(response)); | ||
}); | ||
//*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters