Skip to content

Instantly share code, notes, and snippets.

@pvivera
Created October 19, 2011 20:47
Show Gist options
  • Save pvivera/1299615 to your computer and use it in GitHub Desktop.
Save pvivera/1299615 to your computer and use it in GitHub Desktop.
Get latest timeline and mentions then email them
require 'rubygems'
require 'gmail'
require 'twitter'
USERNAME = {GMAIL_USERNAME}
PASSWORD = {GMAIL_PASSWORD]
TWITTER_USER = {TWITTER_USERNAME}
LAST_TIMELINE_TWEET_FILE = 'last_timeline_tweet_id.txt'
LAST_MENTION_TWEET_FILE = 'last_mention_tweet_id.txt'
#Config from Twitter Dev
Twitter.configure do |config|
config.consumer_key = ''
config.consumer_secret = ''
config.oauth_token = ''
config.oauth_token_secret = ''
end
def send_mail(from, to, subject, message)
Gmail.new(USERNAME, PASSWORD) do |gmail|
gmail.deliver do
from from
to to
subject subject
text_part do
body message
end
end
end
end
def get_timeline
last_id = get_last_timeline_id()
ts = last_id == 0 ? Twitter.user_timeline(TWITTER_USER, {:count => 200}) : Twitter.user_timeline(TWITTER_USER, {:count => 200, :since_id => last_id})
timeline = "Timeline\n\r========\n\r\n\r"
ts.each do |t|
timeline += "#{t[:created_at]}\t#{t[:id]}\twww.twitter.com/#{TWITTER_USER}/status/#{t[:id]}\t#{t[:text]}\n\r"
end
timeline
save_last_timeline_id(ts[0][:id]) unless ts.length == 0
end
def get_mentions
last_id = get_last_mention_id()
ts = last_id == 0 ? Twitter.mentions({:count => 200}) : Twitter.mentions({:count => 200, :since_id => last_id})
mentions = "Mentions\n\r========\n\r\n\r"
ts.each do |t|
mentions += "#{t[:created_at]}\t#{t[:id]}\t#{t[:user][:screen_name]}\twww.twitter.com/#{t[:user][:screen_name]}/status/#{t[:id]}\t#{t[:text]}\n\r"
end
mentions
save_last_mention_id(ts[0][:id]) unless ts.length == 0
end
def get_last_timeline_id
get_last_id(LAST_TIMELINE_TWEET_FILE)
end
def get_last_mention_id
get_last_id(LAST_TIMELINE_TWEET_FILE)
end
def save_last_timeline_id(last_id)
set_last_id(LAST_TIMELINE_TWEET_FILE, last_id)
end
def save_last_mention_id(last_id)
set_last_id(LAST_TIMELINE_TWEET_FILE, last_id)
end
def get_last_id(filename)
if File.exists?(filename)
file = File.open('last_tweet_id.txt', 'r')
last_id = file.readline
file.close
return last_id
else
return 0
end
end
def save_last_id(filename, last_id)
File.delete filename
file = File.open(filename, 'w')
file.pust last_id
file.close
end
send_mail({FROM_MAIL}, {TO_MAIL}, 'Tweets', get_timeline + get_mentions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment