Created
March 10, 2019 00:05
-
-
Save dremixam/db7ebd9e039a6f7d229b71050bacea8a 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
var ImagesClient = require('google-images'); | |
var request = require('request').defaults({ encoding: null }); | |
var Twitter = require('twitter'); | |
var client = new Twitter({ | |
consumer_key: '', | |
consumer_secret: '', | |
access_token_key: '', | |
access_token_secret: '' | |
}); | |
var gimg = new ImagesClient('', ''); | |
function tweet() { | |
gimg.search('potato chips', { | |
page: Math.floor(Math.random() * 15) | |
}) | |
.then(function (images) { | |
var id = Math.floor(Math.random()*images.length); | |
var item = images[id]; | |
console.log(id + '/'+images.length+' : ' + item.url); | |
request.get(item.url, function (err, res, buffer) { | |
var mediaType = item.type; | |
var mediaData = buffer; | |
var mediaSize = item.size; | |
initUpload() // Declare that you wish to upload some media | |
.then(appendUpload) // Send the data for the media | |
.then(finalizeUpload) // Declare that you are done uploading chunks | |
.then(mediaId => { | |
var status = { | |
status: '', | |
media_ids: mediaId // Pass the media id string | |
} | |
client.post('statuses/update', status, function(error, tweet, response) { | |
if (!error) { | |
// console.log(tweet); | |
} | |
}); | |
}); | |
/** * Step 1 of 3: Initialize a media upload * @return Promise resolving to String mediaId */ | |
function initUpload () { | |
return makePost('media/upload', { | |
command : 'INIT', | |
total_bytes: mediaSize, | |
media_type : mediaType, | |
}).then(data => data.media_id_string); | |
} | |
/** * Step 2 of 3: Append file chunk * @param String mediaId Reference to media object being uploaded * @return Promise resolving to String mediaId (for chaining) */ | |
function appendUpload (mediaId) { | |
return makePost('media/upload', { | |
command : 'APPEND', | |
media_id : mediaId, | |
media : mediaData, | |
segment_index: 0 | |
}).then(data => mediaId); | |
} | |
/** * Step 3 of 3: Finalize upload * @param String mediaId Reference to media * @return Promise resolving to mediaId (for chaining) */ | |
function finalizeUpload (mediaId) { | |
return makePost('media/upload', { | |
command : 'FINALIZE', | |
media_id: mediaId | |
}).then(data => mediaId); | |
} | |
/** * (Utility function) Send a POST request to the Twitter API * @param String endpoint e.g. 'statuses/upload' * @param Object params Params object to send * @return Promise Rejects if response is error */ | |
function makePost (endpoint, params) { | |
return new Promise((resolve, reject) => { | |
client.post(endpoint, params, (error, data, response) => { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(data); | |
} | |
}); | |
}); | |
} | |
}); | |
}); | |
} | |
tweet(); | |
setInterval(tweet,3600000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment