Python + Facebook = PyFacebook on iPhone 5

One more down from the long TODO list – created a CLI to update the status on facebook. And the application is named iPhone 5 on FB :P I find it so hilarious that people will wonder when the actual iPhone 5 will be released :P So here is how it goes:

Facebook provides a nice and dandy interface (infrastructure rather) to create your own application. All of us know that FarmVille and some other silly applications on FB are earning in billions. Thanks to FB. So yea, this interface can be accessed via http://www.facebook.com/developers. You need to log in to this page and just create your application placeholder. This way you will get a registered FB App name, app_id, app_key and secret_key. Once you have all these things, you can write your own application using PHP, Python, C#, Java and hell lot of other languages. If you are good at web development, you can write a web application which can be integrated with FB. These application can access a user’s information (i.e. profile pic, status msgs, videos, photos etc etc) which is shared on facebook. If you have notices, whenever you start using a new game/app on FB, the app will ask the user confirmation about giving the access to user info. Once user approves, the app can update the status msg on behalf of the user.

My intention was the same, to update the status msg on behalf of a user. Cool. So I created a facebook app called “iPhone 5”, and got the required details (app_key, secret_key mainly). Then it was just a cup of cake. People have implemented a facebook interface over various languages including Python. One such library is called PyFacebook. It is hosted over code.google.com and it’s very easy to use. Just install this library (steps to install are there in the readme.txt file) and then you will be able to do “import facebook” in your python code. Apart from this page, you may need couple of other packages which are internally used by PyFacebook, like setuptools, django etc. Just do apt-cache search for these packages and you will get the list of available options. Then I got the simple program to update the status on fb from stackoverflow. I did little modification to make it work according to my need. What I found while doing R&D with the code that, the code actually creates a session with facebook, logs in, ask the user to confirm the application making use of user info and then updates the status using that session. Now each session has its own unique token. Once your logged in, you can use the same token to update the status msgs. But if you are already logged in, then you will get a stale token and that will not allow you to update any of the status messages.

So here is what I did: I made two applications. 1) FB daemon 2) FB status update util. The FB daemon will start as soon as the system starts and open the browser and will make user login. Once login is successful, it will get the session authorization token. After that, the daemon will keep on waiting on a local socket (127.0.0.1:1212) to get the data. The data will be sent via FB util (2nd app). Here is the code to do that:

FB Daemon:

#!/usr/bin/python
import facebook, socket
# Replace these with your app's credentials
api_key = 'ac4ae09275c9312e440348f923399ed1'
secret_key = 'fb60f25cb5f0ab2200f4a1bab916cc89'
client = facebook.Facebook(api_key, secret_key)
client.auth.createToken()
client.login()

fb_port=2606
fb_host="127.0.0.1"

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((fb_host, fb_port))


raw_input()
try:
	client.auth.getSession()
#	client.request_extended_permission("publish_stream", popup=False)
except facebook.FacebookError, e:
	print "ERROR", e


try:
	while True:
		msg, addr = s.recvfrom(420)
		print "Setting status to \"",msg,"\""				
		cek = client.status.set(msg)
except facebook.FacebookError, e:
	print "ERROR", e
	print "Igoring exception.."
	pass

FB Utility to post the message:

#!/usr/bin/python

import socket,sys

fb_host = "127.0.0.1"
fb_port = 2606

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(sys.argv[1], (fb_host, fb_port))
print "Your status is updated with the msg \"", sys.argv[1]," \""
s.close()

Have fun with iPhone 5 :P

Advertisements

2 thoughts on “Python + Facebook = PyFacebook on iPhone 5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s