Skip to content

Instantly share code, notes, and snippets.

@zthomae
Created August 19, 2012 01:25
Show Gist options
  • Select an option

  • Save zthomae/3390783 to your computer and use it in GitHub Desktop.

Select an option

Save zthomae/3390783 to your computer and use it in GitHub Desktop.
Get recent tweets and put them on tumblr blog
require "twitter"
require "tumblr4r"
require "date"
# Construct Twitter instance
t = Twitter.new
# Define screen_name.
screen_name = "zthomae"
# Set initial params.Open tweetfile to find id of last tweet. If none, get last 200.
params = { :include_rts => true, :count => 200 }
if File.exists?("last.txt") && !File.read("last.txt").empty?
tweetfile = open("last.txt", "r")
params[:since_id] = tweetfile.read[0..tweetfile.size-2].to_i
tweetfile.close
else
params[:since_id] = t.user_timeline(screen_name, params).last.id
end
# Make string to hold post now
tweetstring = ""
# Find highest page to get
pagemax = 0
begin
newmax = pagemax + 1
params[:page] = newmax
pagemax = newmax if !t.user_timeline(screen_name, params).empty?
end until t.user_timeline(screen_name, params).empty?
# If pagemax is 0, then there are new tweets. Stop.
exit if pagemax == 0
(0..pagemax-1).each do |page|
params[:page] = page
tweets = t.user_timeline(screen_name, params)
# Add tweets to tweetstring in reverse-chronological order, one per line
(0..tweets.length-1).each { |x| tweetstring += ("<a href=\"http://twitter.com/" + screen_name \
+ "/status/" + tweets[-1-x].id.to_s + "\">" + tweets[-1-x].created_at.to_s[0..-7] + "</a> - " \
+ tweets[-1-x].text + "\n\n") }
end
# Tumble about it: Post tweetstring to tumblr
# The tumblfile is a text file of three lines. The Blog URL is on the first line, the username
# is on the second and the password is on the third.
# TODO: Make this *safe*.
tumblfile = File.open(File.expand_path("~/.tumblr_login"), "r")
blog = Tumblr4r::Site.new(tumblfile.readline.chomp, tumblfile.readline.chomp, tumblfile.readline.chomp)
post = Tumblr4r::Regular.new()
post.regular_title = "Tweets Onslaught: " + Date.today.to_s
post.regular_body = tweetstring
blog.save(post)
# Write last id to tweetfile.
tweetfile = open("last.txt", "w+")
tweetfile.write(tweets.first.id + "\n")
tweetfile.closerequire "twitter"
require "tumblr4r"
require "date"
# Construct Twitter instance
t = Twitter.new
# Define screen_name.
screen_name = "zthomae"
# Set initial params.Open tweetfile to find id of last tweet. If none, get last 200.
params = { :include_rts => true, :count => 200 }
if File.exists?("last.txt") && !File.read("last.txt").empty?
tweetfile = open("last.txt", "r")
params[:since_id] = tweetfile.read[0..tweetfile.size-2].to_i
tweetfile.close
else
params[:since_id] = t.user_timeline(screen_name, params).last.id
end
# Make string to hold post now
tweetstring = ""
# Find highest page to get
pagemax = 0
begin
newmax = pagemax + 1
params[:page] = newmax
pagemax = newmax if !t.user_timeline(screen_name, params).empty?
end until t.user_timeline(screen_name, params).empty?
# If pagemax is 0, then there are new tweets. Stop.
exit if pagemax == 0
(0..pagemax-1).each do |page|
params[:page] = page
tweets = t.user_timeline(screen_name, params)
# Add tweets to tweetstring in reverse-chronological order, one per line
(0..tweets.length-1).each { |x| tweetstring += ("<a href=\"http://twitter.com/" + screen_name \
+ "/status/" + tweets[-1-x].id.to_s + "\">" + tweets[-1-x].created_at.to_s[0..-7] + "</a> - " \
+ tweets[-1-x].text + "\n\n") }
end
# Tumble about it: Post tweetstring to tumblr
# The tumblfile is a text file of three lines. The Blog URL is on the first line, the username
# is on the second and the password is on the third.
# TODO: Make this *safe*.
tumblfile = File.open(File.expand_path("~/.tumblr_login"), "r")
blog = Tumblr4r::Site.new(tumblfile.readline.chomp, tumblfile.readline.chomp, tumblfile.readline.chomp)
post = Tumblr4r::Regular.new()
post.regular_title = "Tweets Onslaught: " + Date.today.to_s
post.regular_body = tweetstring
blog.save(post)
# Write last id to tweetfile.
tweetfile = open("last.txt", "w+")
tweetfile.write(tweets.first.id + "\n")
tweetfile.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment