Monday, October 22, 2012

Simple Twitter Bot

So OK, after long interruption I'm here again and ready to show some cool stuff. I want to show you very minimalistic twitter bot, that can do few things. First, it can grab most popular hash tags, trends. Second, It can post the most popular trend of the day. First of all you need to create new twitter app here Twitter Dev.

Then go to OAuth settings and copy two fields "Consumer key" and "Consumer Secret". These keys you'll need to authorize your app for data exchange with twitter. After this go to Application type -> Access and choose "Read and Write". OK, after these steps you have registered app and it's credentials(keys). But it can not post tweets by itself. It will be twitting from your account, so you should authorize. You have 2 ways. First, go to "Your Access Token" and click "Create access token". You will see two long strings. Access token and Access Token Secret. Copy them and save with consumer keys. Second way is to run a script to get these tokens. We will use tweepy library to work with twitter. It is rather simple and good library to work with. You can get it here Tweepy @ google Code and here Tweepy @ GitHub. Download, install and we can use it. Here is code example to get access tokens

import tweepy, webbrowser

CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth_url = auth.get_authorization_url()
verifier = raw_input('PIN: ').strip()
print "ACCESS_KEY = '%s'" % auth.access_token.key

print "ACCESS_SECRET = '%s'" % auth.access_token.secret

You'll get your tokens. So, now we can start developing our app :)
First of all lets write a function to authorize in twitter:

import tweepy

def initApi():
consumer_key = "" # your key
consumer_secret = "" # your secret key
access_key = "" # your token
access_secret = "" # your secret token
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
return tweepy.API(auth)

api = initApi()

Now we have API object. It gives us access to twitter api with easy and comfortable interface.

trends = api.trends_daily()['trends']

So, according to API Reference trends_daily function returns json object. It looks like this: {Date: [list of query dicts like {query:"", name:"",..}, {...}], Date2:[{...}, {...}], ...and so on}. We need to parse it to get tags. Here is simple function:

def tags():
   hourly_tags = []
   for value in trends.values():
       for item in value:
   return hourly_tags

It returns 240 tags, 20 trending tags per last 24 hours. Pretty simple, isn't it?
Now we have all, we could ask. All we need is to count unique tags and sort them.
First of all let's write sorting function. We'll then use it in our parsing function. We'll be sorting dictionary {tag:count} by count, or by value.

def sbv(d, reverse=False):
   return sorted(d. iteritems(), key=itemgetter(1), reverse=True)

This sotrByValue function returns list of tuples (tag, count). It's time to use it in our parse function. For example, this function returns top 10 tweets:

def getUniqueCounter():
   tagList = getTags()
   tagCount = [tagList.count(x) for x in tagList]
   tagDict = dict(zip(tagList, tagCount))

   topTen = dict(sbv(tagDict)[:10])
   return topTen

Or we can change topTen = dict(sbv(tagDict)[:10]) with topTen = dict(sbv(tagDict)[:1]) and get the most popular tweet.
Finally we need to post it.

   tweet = "Top trend of the day: " + getUniqueCounter()

This will work with top trend, not with top ten, you should remember, twitter status is 140 letters max.

No comments:

Post a Comment