How we built a Twitter Application

By : lakshman

Twitter, just like many other Web2.0 sites, has an excellent API. So much so, writing a twitter app is considered to be "Hello World" of Web2.0 Programming.

I wrote an application recently. It is DM++. By default, this application allows you to receive direct messages from twitter users you have @replied even if you currently don't follow them. You can even configure application to be able to receive direct messages from people you have @replied in only last 2 days, or receive Dm from all other twitter users.

Look how many people are currently complaining Cant DM U as U dont follow me or read our Faq

Now, let me illustrate some steps in building this application.

One of the modules is where I need to find out whether the userx follows usery. This request needs authentication headers added for twitter to respond, else U receive a "Not authorized" response

import urllib2,base64,simplejson

def is_follows(follower, following):

    theurl = 'http://twitter.com/friendships/exists.json?user_a=%s&user_b=%s'%(follower, following)

    username = 'uname'
    password = 'pwd'

    handle = urllib2.Request(theurl)

    authheader =  "Basic %s" % base64.encodestring('%s:%s' % (username, password))

    handle.add_header("Authorization", authheader)

    try:
        return simplejson.load(urllib2.urlopen(handle))
    except IOError, e:
        # This is reached when allocated API requests to IP are completed.
        print "parsing the is_follows json from twitter, failed"
    return False

if __name__=='__main__':
    print is_follows(u'scorpion032',u'scobleizer')

This is an independent module, intended so, as this functionality is best that way, simplest without the bloat of any other functions and it is portable.

There is a very good Python-twitter wrapper by Google, many functions on twitter are now very simple, for anything more than finding out if userx follows usery as above, this can be very useful.

import twitter
api=twitter.Api('uname','pwd')

Thats it and you have an authenticated api object from which U can do nearly all twitter functions. Infact it is working with this wrapper, that tempted me to write this application. There are a host of things you can do with this wrapper including what is shown below, that I have used.

This code retrieves all Direct Messages that are received since it was last retrieved. We store the fetchinfo table pk=1 field with the last date time it was retrieved

try:
    f=FetchInfo.objects.get(pk=1).last_datetime
except:
    f=None   
dm_list=api.GetDirectMessages(since=f)

Following snippet sends the Direct Messages to the users that can receive it, else it is marked filtered. 'canhesend()' is a function where we find verify if the user is able to send direct messages based on his access_level setting

    if canhesend(adm.sent_by.username,user):
            try:
                api.PostDirectMessage(user,"via @"+adm.sent_by.username+": "+adm.text)
                api.PostDirectMessage(adm.sent_by.username,"Your Dm has been sent to @"+user)
                adm.is_sent=True
                adm.save()
            except HTTPError:
                adm.is_filter=True
                adm.save()
        else:
            adm.is_filter=True
            adm.save()

adm is the parameter that loops on direct messages.

Among other important things see how we seperate the usernames and text which are sent to us, there is a very nice input from stackoverflow, which we incorporated.

We have a text of the format '@abc [@def ..] This part is text' which we want seperated to user names and text part. We want retrieved in the following format:

l=['abc','def']
s='This part is text'

This is how we went about doing it:

import re
rx = re.compile("((?:@\w+ +)+)(.*)")
t='@abc   @def  @xyz Hello this part is text and my email is foo@ba.r'
a,s = rx.match(t).groups()
l = re.split('[@ ]+',a)[1:-1]
print l
print s


Related Posts


Can we help you build amazing apps? Contact us today.

Topics : web2.0

Comments

Brian 19th Feb., 2009

Uh, isn't your user's password being sent basically in the clear (base64-encoded, ooh)?

commmenttor
Brent Shepherd

Nice tutorial. It was exactly what I was looking for - a brief intro to using the Twitter API on App Engine. Thanks for sharing. :)

commmenttor
Twitter Search

thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.

commmenttor
nordstrom womens ugg slippers

ugg cozy ii slippers How we built a Twitter Application - Agiliq Blog | Django web app development
nordstrom womens ugg slippers http://www.eireoggreystones.com/kensington/nordstrom-womens-ugg-slippers.html

commmenttor
men ugg boots cheap

ugg shoes online How we built a Twitter Application - Agiliq Blog | Django web app development
men ugg boots cheap http://www.dublinmedievalsociety.com/zealand/men-ugg-boots-cheap.html

commmenttor
ugg slippers womens

ugg boots size 5 How we built a Twitter Application - Agiliq Blog | Django web app development
ugg slippers womens http://www.shannonenergyvalley.com/delivery/ugg-slippers-womens.html

commmenttor
ugg boots for man

[url="http://massimotailor.com/ugg-boots-uk/uggs-mens-boots.html"]uggs mens boots[/url]
ugg boots for man http://massimotailor.com/ugg-boots-uk/ugg-boots-for-man.html

commmenttor
classic ugg boots sale

real uggs australia How we built a Twitter Application - Agiliq Blog | Django web app development
classic ugg boots sale

commmenttor
ugg butte

cost of ugg boots How we built a Twitter Application - Agiliq Blog | Django web app development
ugg butte

commmenttor
cheap uggs from australia

boots ugg sale How we built a Twitter Application - Agiliq Blog | Django web app development
cheap uggs from australia

commmenttor
ugg mens slippers

mens black ugg slippers How we built a Twitter Application - Agiliq Blog | Django web app development
ugg mens slippers

commmenttor
ugg slipper socks

sale kids ugg boots How we built a Twitter Application - Agiliq Blog | Django web app development
ugg slipper socks

commmenttor
ugg coquette clearance

waterproof ugg boots How we built a Twitter Application - Agiliq Blog | Django web app development
ugg coquette clearance

commmenttor
ugg purse

ugg ear muffs How we built a Twitter Application - Agiliq Blog | Django web app development
ugg purse

commmenttor
kids uggs on sale

where to buy ugg boots cheap How we built a Twitter Application - Agiliq Blog | Django web app development
kids uggs on sale

commmenttor
ugg australia australia

ugg boots free shipping How we built a Twitter Application - Agiliq Blog | Django web app development
ugg australia australia

commmenttor
furla elisabeth hobo

How we built a Twitter Application - Agiliq Blog | Django web app development furla bags furla handbags sale
furla elisabeth hobo

commmenttor
macy s ugg boots

ugg boots for children How we built a Twitter Application - Agiliq Blog | Django web app development
macy s ugg boots

commmenttor
© Agiliq, 2009-2012