Skip to content

Instantly share code, notes, and snippets.

@mh-github
Last active September 18, 2015 01:01
Show Gist options
  • Select an option

  • Save mh-github/cd9491da938f5174b3c8 to your computer and use it in GitHub Desktop.

Select an option

Save mh-github/cd9491da938f5174b3c8 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require "rubygems"
require "twitter"
require "koala"
client = Twitter::REST::Client.new do |config|
config.consumer_key = "my_consumer_key"
config.consumer_secret = "my_consumer_secret"
config.access_token = "my_access_token"
config.access_token_secret = "my_access_token_secret"
end
start_date = Date.parse("2015-01-01")
end_date = Date.parse("2015-06-30")
abort if start_date > end_date
local_offset = Rational(5.5, 24)
options = {:count => 200, :include_rts => true}
tweets = []
twitter_output = []
while (tweets = client.user_timeline("mahboob_h", options)) do
abort if tweets.length == 0
first_tweet_created_date = Date.parse(tweets.first.created_at.to_s[0..9])
last_tweet_created_date = Date.parse(tweets.last.created_at.to_s[0..9])
if first_tweet_created_date < start_date
break
elsif last_tweet_created_date > end_date
# do nothing, decrement id and fetch next 200 tweets
else
tweets.each do |tweet|
tweet_created_date = DateTime.parse(tweet.created_at.to_s[0..23])
tweet_created_date += local_offset
break if tweet_created_date < start_date
if (tweet_created_date >= start_date and
tweet_created_date <= end_date)
twitter_output << "#{tweet_created_date.to_s[0..9]}" + " " + "#{tweet.text}\n"
end
end
end
options[:max_id] = tweets.last.id-1
end
abort if twitter_output.length == 0
fb_update = "Hello Folks! This is the Ruby program written by Mahboob Hussain.\nMy creator, Mr. Hussain gives me a start date and end date. I go and fetch from his Twitter timeline all tweets that he tweeted between those dates and post them to his Facebook wall.\nI am happy to save Mr. Mahboob a small amount of time that he has to spend to copy and paste from Twitter web page to his Facebook wall.\nHere are the tweets he tweeted in the first six months of this year. Read and enjoy. Thanks :)\n"
fb_update << "----------------------------------------------\n"
twitter_output.reverse!
twitter_output.each do |tweet_with_date|
fb_update << tweet_with_date
end
@graph = Koala::Facebook::API.new("my_user_token")
@graph.put_wall_post(fb_update)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment