Friday 18 September 2015

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:

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

No comments:

Post a Comment