October 18, 2010

Synchronizing django with twitter using django-syncr

Ok so you have your first django powered application running (let me guess - CMS ? Blog ? :) ) and now you'd like to make it more "web 2.0"-ish by adding your twitter feeds. Althought I personally think this app is useless and dumb people who pay me think differently. So let's start. First you'll need some python's dependencies.

python-oauth2
python-twitter and stay alert, cause you need the latest build (so don't use easy_install as you'll get the deprecated 0.6 version)
django-syncr

Then go to dev.twitter and (if you already haven't) register a new application. This will generate you "consumer_key" and "consumer_secret". Write them down. Now let's get back to our application.
Iinstall both python dependencies but django-syncr add as an application in settings.INSTALLED_APPS. The reason for this is that you need to edit a bit of code, since currently available syncr does not support OAuth recently introduced on Twitter. Next either find "get_access_token.py" in python-twitter package, or download this function from sources and send somewhere within the scope of python. Before sending it though, open and edit :

AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'
SIGNIN_URL        = 'https://api.twitter.com/oauth/authenticate'

consumer_key    = None
consumer_secret = None

if consumer_key is None or consumer_secret is None:

In place of consumer_key and consumer_secret insert proper (previously saved) values as strings. Save and close and execute it with :

"python get_access_token.py run"

You will get a unique link printed in the console, that you need to visit. Visit it and press "Enter". Now you will get two more values to store somewhere : "access_token_key" and "access_token_secret".

Okay, now open syncr.app.tweet and change `__init__` function to this :

def __init__(self, username, key, secret, token_key, token_secret):

        self.username = username
        self.api = twitter.Api(
                            consumer_key=key, 
                            consumer_secret=secret, 
                            access_token_key=token_key,
                            access_token_secret=token_secret)
        self.user_cache = dict()

Here you can have different approach of course. You can hardcode your key values in it, add them to settings or send them each time when synchronizing with Twitter. Decision is up to you. Okay, last improvements to django-syncr. Open syncr.twitter folder, and edit models.py file. Here change Tweet.tweeter_id field from PositiveIntegerField to either IntegerField(if you're running < 1.2) or BigIntegerField. We're finished with edits, so sync your database and add this code to the view/context_processor that will serve your tweets :

#1. create TwitterSyncr object
t = TwitterSyncr('twitter_username',
'your_consumer_key',
'your_consumer_secret',
'your_access_token_key',
'your_access_token_secret',)
#2. synchronize your TwitterSyncr object with existing Twitter user
t.syncUser('twitter_username')
#3. get tweets for the Twitter user (so it will import tweets from Twitter to your DB)
t.syncTwitterUserTweets('twitter_username')

#4. grab user from database and it's tweets
user = TwitterUser.objects.get(screen_name="twitter_username")
tweets = Tweet.objects.filter(user=user)

And voila ! You have a list of your most recent tweets available. Of course you can limit the amount of parsed tweets with [:number].

2 comments:

  1. where do we should put this limitation [:number]?

    ReplyDelete
  2. tweets = Tweet.objects.filter(user=user)[:limitation]

    ReplyDelete