I am making a Twitter bot that get an image from URL and post to Twitter.
I have set the interval for getPhoto()
to run every 1 minute.
After the image is saved, the bot needs to check if the image is updated before it uploads to Twitter.
Below is my current code.
const Twit = require('twit')
const request = require('request')
const fs = require('fs')
const config = require('./config')
const moment = require('moment');
const os = require('os')
const tmpDir = os.tmpdir()
const bot = new Twit(config)
function getPhoto(fileName) {
const url = 'http://blabla.com/pic.jpg'
const descriptionText = 'Latest Image Now'
const filePath = tmpDir + "/" + fileName
const file = fs.createWriteStream(filePath);
request(url).pipe(file).on('close', err => {
if (err) {
console.log(err)
} else {
uploadMedia(descriptionText, filePath)
}
})
}
function uploadMedia(descriptionText, filePath) {
bot.postMediaChunked({ file_path: filePath }, (err, data, response) => {
if (err) {
console.log(err)
} else {
var params = { status: descriptionText, media_ids: [data.media_id_string] }
bot.post('statuses/update', params, (err, data, respone) => {
if (err) {
console.log(err)
})
}
})
}
getPhoto('pic.jpg');
setInterval(getPhoto, 1000*60, 'pic.jpg')
via PolarBear
No comments:
Post a Comment