Last active
September 8, 2016 19:23
-
-
Save travischoma/9279105 to your computer and use it in GitHub Desktop.
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
// adapted from https://gist.github.com/adaline/7363853 which read the image from a file | |
// modified to support posting to twitter update_with_media - with image from s3 rather than a | |
// local file. | |
(function() { | |
var fs, path, request, twitter_update_with_media; | |
fs = require('fs'); | |
path = require('path'); | |
request = require('request'); | |
twitter_update_with_media = (function() { | |
function twitter_update_with_media(auth_settings) { | |
this.auth_settings = auth_settings; | |
this.api_url = 'https://api.twitter.com/1.1/statuses/update_with_media.json'; | |
} | |
twitter_update_with_media.prototype.post = function(status, imageUrl, callback) { | |
var form, r; | |
r = request.post(this.api_url, { | |
oauth: this.auth_settings | |
}, callback); | |
form = r.form(); | |
form.append('status', status); | |
return form.append('media[]', request(imageUrl)); | |
}; | |
return twitter_update_with_media; | |
})(); | |
module.exports = twitter_update_with_media; | |
}).call(this); |
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
var twitter_update_with_media = require('twitter_update_with_media'); | |
var tuwm = new twitter_update_with_media({ | |
consumer_key: '...', | |
consumer_secret: '...', | |
token: '...', | |
token_secret: '...' | |
}); | |
tuwm.post('This is a test', 'http://imageurl.com/image.png', function(err, response) { | |
if (err) { | |
console.log(err); | |
} | |
console.log(response); | |
}); |
@neeravmakwana
change var twitter_update_with_media = require('twitter_update_with_media');
to var twitter_update_with_media = require('./twitter_update_with_media');
btw, thanks Travis!
This is simply awesome, Travis. Works like charms. Among the deprecated nodejs libraries and useless tweet libraries, this piece of code made all the breakthrough for image tweet sharing.
Found issues when uploading some image urls
- Error : "500 Connection refused"
image url : http://motherboard-images.vice.com/content-images/article/14404/140775769503065.jpg?crop=0.6642984014209592xw:1xh;*,*&resize=500:*&output-format=jpeg&output-quality=90 - Error: "189 Error creating status."
image url : http://www.inc.com/uploaded_files/image/970x450/getty_492792477_50539.jpg
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I installed request module as suggested by you. Then I created the following 2 .js files in the folder C:\node_modules\request:
I updated the consumer key, secret etc. in usage.js and then ran usage.js from command prompt. I am getting the following error:
C:>node node_modules\request\usage.js
module.js:340
throw err;
^
Error: Cannot find module 'twitter_update_with_media'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (C:\node_modules\request\usage.js:1:95)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
Please note that I have created some basic bots which search for tweets and then retweet/reply and have these working satisfactorily (using twit module). However, I want to post media (use update with media) and couldn't figure out how to do it. So I installed request module, but can't seem to figure out how it works too.
Help will be appreciated.
Thanks.