I wrote a twitter application in Python. Following is the code I used for a module where I find if x is following y. This code can be obviously improved upon. A pythonic way to do that?
import urllib2
import sys
import re
import base64
from urlparse import urlparse
import simplejson
def is_follows(follower, following):
theurl = 'http://twitter.com/friendships/exists.json?user_a='+follower+'&user_b='+following
username = 'uname1'
password = 'pwd1'
handle = urllib2.Request(theurl)
base64string = base64.encodestring('%s:%s' % (username, password))
authheader = "Basic %s" % base64string
handle.add_header("Authorization", authheader)
fol=True
try:
fol = simplejson.load(urllib2.urlopen(handle))
except IOError, e:
# here we shouldn't fail if the username/password is right
print "It looks like the username or password is wrong."
return fol
Update: Indentation fixed.