Skip to content

Instantly share code, notes, and snippets.

@barraponto
Created March 23, 2012 15:29
Show Gist options
  • Select an option

  • Save barraponto/2171798 to your computer and use it in GitHub Desktop.

Select an option

Save barraponto/2171798 to your computer and use it in GitHub Desktop.
Get most used hashtags from a Twitter profile's last 200 tweets.
import json
from urllib import urlencode
from urllib2 import urlopen
from collections import OrderedDict
profiletags = {}
api = 'https://api.twitter.com/1/statuses/user_timeline.json?'
# TODO: take options from CLIparameters
params = {
'include_entities': 'true', # required hashtag data
'screen_name': 'barraponto', # might change, user_id is more reliable
'count': '200', # 200 is the maximum value
'trim_user': 'true' # just to make things lighter
}
request = urlopen(api + urlencode(params))
data = json.loads(request.read())
for tweet in data:
for hashtag in tweet['entities']['hashtags']:
hashtag = hashtag['text']
if hashtag in profiletags:
profiletags[hashtag] = profiletags[hashtag] + 1
else:
profiletags[hashtag] = 1
for hashtag, count in OrderedDict(sorted(profiletags.items(), key=lambda t: t[1], reverse=True)).iteritems():
print '{0}: {1}'.format(hashtag, count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment