Skip to content

Instantly share code, notes, and snippets.

@luisparravicini
Created May 6, 2010 11:55
Show Gist options
  • Save luisparravicini/392051 to your computer and use it in GitHub Desktop.
Save luisparravicini/392051 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby1.9.1
#
# Reads the next line from a file and tweets it. It saves the position
# of the last line read so successive executions will use different
# lines of text. Authentication info is read from a file with YAML.
#
# http://ktulu.com.ar/blog/2010/05/07/twiteos-automaticos/
# lparravi@gmail.com
require 'yaml'
# Reads user and password. the yaml file must be a Hash
# with keys :user and :password
# An example file could be something like this:
#---
#:user: username
#:password: the.real.password
#
def read_conf
conf_file = 'auto.tweet.yaml'
raise "conf file not found" unless File.exists?(conf_file)
YAML::load(IO.readlines(conf_file).join)
end
# Returns next line of text from a file or nil if there are no
# more lines to read
def next_status
tweets_fname = 'to.tweet'
offset_fname = "#{tweets_fname}.offset"
last_offset = if File.exists?(offset_fname)
File.open(offset_fname) { |f| Marshal::load(f) }
else
0
end
unless File.exists?(tweets_fname)
raise "needs a file '#{tweets_fname}' with text"
end
msg = nil
File.open(tweets_fname) do |f|
f.seek(last_offset)
msg = f.gets
last_offset = f.tell
end
File.open(offset_fname, 'w') { |f| Marshal::dump(last_offset, f) }
msg.strip unless msg.nil?
end
# Tweets a message using the authentication data in $conf. Needs
# curl to be installed.
def tweet(msg)
raise "no tweet no fun" if msg.nil?
raise "'#{msg}' is too long" if msg.size > 140
`curl -u #{$conf[:user]}:#{$conf[:password]} -d status='#{msg}' http://twitter.com/statuses/update.xml`
end
$conf = read_conf
tweet next_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment