Last active
December 16, 2016 14:59
-
-
Save toofusan/bbd4d97c3793c3bd977725f4d5badd11 to your computer and use it in GitHub Desktop.
twitter bot with tumblr image http://a4typo.tumblr.com/
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
'use strict'; | |
// Tumblr Authenticate | |
const Tumblr = require('tumblr.js'); | |
const tumblr = Tumblr.createClient({ | |
consumer_key: '...', | |
consumer_secret: '...', | |
token: '...', | |
token_secret: '...' | |
}); | |
// Twitter Authenticate | |
const Twit = require('twit'); | |
const twitter = new Twit({ | |
consumer_key: '...', | |
consumer_secret: '...', | |
access_token: '...', | |
access_token_secret: '...' | |
}) | |
const fs = require('fs'); | |
const request = require('request'); | |
const CronJob = require('cron').CronJob; | |
// ============================= | |
// Tweet with image from tumblr | |
// ============================= | |
let randomPost = function() { | |
tumblr.userInfo((err, data) => { | |
const offset = Math.floor(Math.random() * data.user.blogs[0].total_posts); // number of posts for tumblr | |
tumblr.blogPosts('a4typo.tumblr.com', {limit: 1, offset: offset}, (err, data) => { | |
const imageURL = data.posts[0].photos[0].alt_sizes[0].url; | |
const postText = data.posts[0].summary; | |
const postURL = data.posts[0].short_url; | |
request({method: 'GET', url: imageURL, encoding: null}, (err, res, body) => { | |
if(err) console.log(err); | |
if(!err && res.statusCode === 200) { | |
fs.writeFile('tmp.jpg', body, 'binary', (err) => { | |
const b64content = fs.readFileSync('tmp.jpg', {encoding: 'base64'}); | |
twitter.post('media/upload', {media_data: b64content}, (err, data, response) => { | |
const mediaIdStr = data.media_id_string; | |
let altText = postText; | |
if(altText === '') altText = '大判プリンターを持たない人たちが、それでも大きな文字を印刷したかった結果'; | |
const meta_params = { media_id: mediaIdStr, alt_text: { text: altText } }; | |
twitter.post('media/metadata/create', meta_params, (err, data, response) => { | |
if(err) console.log(err); | |
if(!err) { | |
const tweetText = postText + '\n' + postURL; | |
const params = {status: tweetText, media_ids: [mediaIdStr]}; | |
twitter.post('statuses/update', params, (err, data, response) => { | |
}); | |
} | |
}); | |
}); | |
}); | |
} | |
}); | |
}); | |
}); | |
}; | |
// ============================= | |
// Tweet every 2 hour | |
// ============================= | |
new CronJob('00 */2 * * *', randomPost, null, true, 'Asia/Tokyo'); | |
// ============================= | |
// Retweet when someone tweet with '#A4タイポグラフィ' hashtag | |
// ============================= | |
const stream = twitter.stream('statuses/filter', {track: '#A4タイポグラフィ'}); | |
stream.on('tweet', (tw) => { | |
twitter.post('statuses/retweet/:id', {id: tw.id_str}, (err, data, response) => { | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment