Last active
August 29, 2015 14:05
-
-
Save bcks/2897c8f86d08ab9dcdbc to your computer and use it in GitHub Desktop.
Brooklyn Museum Bot
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
// runs under Node.js | |
var fs = require('fs'), | |
request = require('request'), | |
Twitter = require('node-twitter'); | |
var twitterRestClient = new Twitter.RestClient( | |
'CONSUMER_KEY', | |
'CONSUMER_SECRET', | |
'TOKEN', | |
'TOKEN_SECRET' | |
); | |
function getObject() { | |
var objectId = Math.floor((Math.random() * 5205) + 1); // 5206 is last item as of August 20, 2014 | |
var accessToken = 'BROOKYLN_MUSEUM_API_KEY'; | |
var url = 'http://www.brooklynmuseum.org/opencollection/api/?method=collection.getItem&version=1&api_key='+accessToken+'&item_type=object&item_id='+objectId+'&image_results_limit=1&include_item_fields=true&max_image_size=768&format=json&include_html_style_block=false'; | |
request(url, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var obj = JSON.parse(body); | |
if (obj.length != 0) { | |
if (typeof obj.response.object !== "undefined") { | |
getImage(obj.response.object); | |
} else { | |
console.log('Item not found, trying again.',obj); | |
getObject(); | |
} | |
} else { | |
console.log('empty'); | |
} | |
} | |
}); | |
} | |
function getImage(object) { | |
if (typeof object.images !== "undefined") { | |
object.image = object.images[0].uri; | |
download(object.image, '/tmp/'+ object.id + '.jpg', function(){ | |
//console.log('downloaded'); | |
tweet(object); | |
}); | |
} else { | |
console.log('No image detected. Trying again.',object); | |
getObject(); | |
} | |
} | |
var download = function(uri, filename, callback){ | |
request.head(uri, function(err, res, body){ | |
if (res.headers['content-length'] < 40000) { | |
console.log('Image is too small. Trying again.',uri); | |
getObject(); | |
} else { | |
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback); | |
} | |
}); | |
}; | |
function tweet(object) { | |
console.log(object.title + ', ' + object.object_date); | |
console.log(object.uri); | |
console.log(object.image); | |
twitterRestClient.statusesUpdateWithMedia( | |
{ | |
'status': object.title + ', ' + object.object_date + ' ' + object.uri, | |
'media[]': '/tmp/' + object.id + '.jpg' | |
}, | |
function(error, result) { | |
fs.unlink('/tmp/' + object.id + '.jpg', function (err) { | |
if (err) throw err; | |
//console.log('successfully deleted /tmp/' + object.id + '.jpg'); | |
}); | |
if (error) { | |
console.log('Twitter Error: ' + (error.code ? error.code + ' ' + error.message : error.message)); | |
setTimeout( getObject(), 10000); | |
} | |
//if (result) { console.log("Twitter response:", result); } | |
} | |
); | |
} | |
getObject(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment