MultiSocial : Posting Tweets To Facebook

The three social networks -- Twitter, Facebook and LinkedIn -- are basically communication forums each to a different set of people.

On Twitter you are putting out your information and ideas in a very succinct manner and normally people don't hide their timelines. On Facebook you can choose to communicate with all people with internet access or to your friends or to a subset of friends, as you can control the privacy settings on each individual update. LinkedIn is the means to communicate with all your professional contacts.

Since each medium has its target audience, it kind of determines your vocabulary and activity. In fact, my profile picture on each of them is also different reflecting the purpose and audience. I have a thoughtful and controlled pose on Twitter, a frolicking image on Facebook and a leadership picture on LinkedIn.

Though the purpose and audience varies between each of the three networks, I feel the need to cross post. From time to time I take a set of tweets from Twitter and post in Facebook. I go to my Twitter timeline, dutifully copy tweets in a certain time period one at time, paste to notepad and then finally copy all those tweets and paste on my Facebook wall.

But I had not done that recently. When I checked my Facebook wall, I realized that I had not posted tweets the whole of this year. What the heck, I thought and decided to post the tweets I tweeted during the first six months. I started doing it and then again I thought what the heck, this is tedious, let me write a program to do it.


So I wrote a Ruby program. Whenever I want to have some fun with code, my first choice is always Ruby. The program had to take a start date and end date, fetch tweets between those dates, and write to my Facebook wall.

To access tweets programmatically, I used a rubygem called twitter. You can access the Twitter REST API, with a client object. The method user_timeline fetches a set of 200 tweets maximum with each call. Each tweet has attributes text which is basically your tweet, created_at which is the timestamp when you tweeted, and an id. Whenever you call user_timeline, you can pass in the max_id and it will fetch tweets older than the tweet of this id.

First you register an app with Twitter. The steps are explained in the article, "How to Register a Twitter App in 8 Easy Steps"[1]. As shown in the last step, get your consumer_key, consumer_secret, access_token and access_token_secret. These are required to connect to Twitter REST services.

Please note a couple of tweet attributes: we get tweets in the reverse chronological order like a stack, that is the the first tweet is the most recent one. The tweets come with their created date stored in GMT. So, if you tweeted at 2 a.m. IST, the created date would have the time of 8:30 pm of the previous date, so date conversion is required.

The programming logic I used is: Fetch 200 tweets at a time. If the first tweet’s created date is greater than the start date, we are done, so break out of the fetching loop. Otherwise if the last tweet’s created date is greater than end date, the bunch of tweets is not in our date range, so fetch the next 200 by continuing the fetching loop. Otherwise, check if each tweet is within the required date range, and store it in an array.

Decrement the max_id before the loop continues. Once the loop is completed, reverse the array.

Next connect to Facebook. For this connection also, you need to have an application registered with Facebook. The steps are well explained in the article, “How to get a Facebook Access Token”.[2] Follow the steps except the last one, how to get the user token. This changes dynamically, and the latest one is available at the developer's access_token URL[3].

The rubygem needed to access Facebook API is koala. You just have to instantiate a new koala API object and call the put_object method with your text. It posts the text on your Facebook wall.

Here is the code:
#!/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)
view raw multi-social.rb hosted with ❤ by GitHub

Reference
1. http://iag.me/socialmedia/how-to-create-a-twitter-app-in-8-easy-steps/
2. https://smashballoon.com/custom-facebook-feed/access-token/
3. https://developers.facebook.com/tools/access_token

Comments

Popular posts from this blog

Mentoring Trainees In Java

27th

Fetching Blogger Posts With Python