Skip to content

Instantly share code, notes, and snippets.

@okuryu
Created December 13, 2010 15:44
Show Gist options
  • Save okuryu/739114 to your computer and use it in GitHub Desktop.
Save okuryu/739114 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
if (!process.argv[2] || !process.argv[3]) {
console.log('Usage: ' + process.argv[1] + ' <Flickr API Key> <User NSID>');
process.exit();
};
const API_KEY = process.argv[2];
const USER_ID = process.argv[3];
const FLICKR_DOMAIN = 'api.flickr.com';
var http = require('http'),
querystring = require('querystring'),
xml2js = require('xml2js-expat');
main();
function getFavorites(nsid, minDate, maxDate) {
var parser = new xml2js.Parser();
var client = http.createClient(80, FLICKR_DOMAIN);
var path = '/services/rest/?' + querystring.stringify({
method: 'flickr.favorites.getPublicList',
api_key: API_KEY,
user_id: nsid,
min_fave_date: minDate,
max_fave_date: maxDate
});
var request = client.request('GET', path, {
host: FLICKR_DOMAIN
});
request.end();
request.on('response', function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if (parser.parse(body)) {
var photos = parser.resultObject.photos;
if (photos['@'].total > 1) {
photos.photo.forEach(function(item) {
console.log(item['@'].id);
});
} else if (photos['@'].total === '1') {
console.log(photos.photo['@'].id);
}
}
});
});
}
function main() {
var maxDate = Math.round(new Date().getTime() / 1000);
var minDate = maxDate - 60 * 60 * 24;
var parser = new xml2js.Parser();
var client = http.createClient(80, FLICKR_DOMAIN);
var path = '/services/rest/?' + querystring.stringify({
method: 'flickr.contacts.getPublicList',
api_key: API_KEY,
user_id: USER_ID
});
var request = client.request('GET', path, {
host: FLICKR_DOMAIN
});
request.end();
request.on('response', function(response) {
var body = '';
response.setEncoding('UTF-8');
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if (parser.parse(body)) {
parser.resultObject.contacts.contact.forEach(function(item) {
getFavorites(item['@'].nsid, minDate, maxDate);
});
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment