Wednesday 18 May 2011

Android tweeting with python tweepy and sl4a

my sweetheart bought me some cute android toys, thanks dear(my sweetheart gave me this cute android toy )

Jeff Miller's blog has an easy how-to tweet using Joshua Roesslein's tweepy module for python. You can run this on your computer (for ease of editing) to generate the access and consumer keys and then do stuff with SL4A on an android phone. The linux-mag.com tutorial link is using the previously available Basic Authentication, the tweepy module does all the heavy lifting to use Oauth (my previous caveats about implementing on lower spec platforms remain) instead. To use make a tweepy directory under sl4a/scripts and copy the *.py out of the tweepy egg or tgz.

The following reads your friends timeline into androids notification bar
#!/usr/bin/env python

import sys
import tweepy
import android
droid=android.Android()

CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'
ACCESS_KEY = 'paste your Access Key here'
ACCESS_SECRET = 'paste your Access Secret here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

for tweet in friend_tweets:
droid.notify(tweet.author.name,tweet.text)

To send a tweet ( something like this) :
#!/usr/bin/env python

import sys
import tweepy
import android
droid=android.Android()

CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'
ACCESS_KEY = 'paste your Access Key here'
ACCESS_SECRET = 'paste your Access Secret here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

tweet = droid.dialogGetInput('Twitter', 'Type your tweet here :').result
api.update_status(tweet)


No comments: