Skip to content

Instantly share code, notes, and snippets.

@billsaysthis
Created September 28, 2012 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billsaysthis/3802239 to your computer and use it in GitHub Desktop.
Save billsaysthis/3802239 to your computer and use it in GitHub Desktop.
OSX terminal script to post tweets to a WordPress blog
#!/usr/bin/ruby
require 'rubygems'
# https://github.com/nu7hatch/gmail
require 'gmail'
# https://github.com/sferik/twitter
require 'twitter'
def mail_tweet(gmail, tid, subj)
if !gmail.logged_in?
p "Not logged in"
return
end
msg = gmail.compose do
to "Your Secret WordPress email address"
subject subj # some subject is necessary, otherwise WP makes the post title a timestamp
# body content assumes you use a category named Tweets for these
# also, publicize is off since this might create loop of post -> tweet -> post -> tweet ad infinitum
body "[tweet " + tid.to_s + "][category Tweets][comments off][publicize off]"
end
msg.deliver!
puts "Delivered mail for tweet " + tid.to_s
end
gmail = Gmail.connect('Your Gmail address', 'Your Gmail password')
Twitter.configure do |config|
config.consumer_key = 'Your Twitter consumer keu'
config.consumer_secret = 'Your Twitter config secret'
config.oauth_token = 'Your Twitter oauth token'
config.oauth_token_secret = 'Your Twitter auth token secret'
end
tweets = Twitter.user_timeline("Your Twitter username")
faves = Twitter.favorites("billsaysthis")
now = Time.new
start = Time.mktime(now.year, now.month, now.day, 0, 0, 0) # today midnight
# use reverse_each since Twitter returns in reverse chronological order
# And we want to post oldest first
tweets.reverse_each do | tw |
if tw.created_at > start
mail_tweet(gmail, tw.id, "A Tweet")
end
end
faves.reverse_each do | tw |
if tw.created_at > start
mail_tweet(gmail, tw.id, "I Favorited a Tweet")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment