Friday, January 29, 2016

Cron: Tweet a Line Every 10-19 Minutes

Cron - Tweet a line every 30-40 mins:

*/10 * * * * perl -le 'sleep rand 540' && head -$[${RANDOM} \% `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/lines.rb

lines.rb - using standard input:

require 'json'
require 'oauth'

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)

# STATUS
status = STDIN.gets

# POST
response = endpoint.post('https://api.twitter.com/1.1/statuses/update.json', status: status )
result = JSON.parse(response.body)