Skip to content

Instantly share code, notes, and snippets.

@newbyca
Created January 4, 2012 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newbyca/1558434 to your computer and use it in GitHub Desktop.
Save newbyca/1558434 to your computer and use it in GitHub Desktop.
find most followed user to send tweet with a given hashtag
#****************************************************
#a simple script that searchs all tweets with a given hashtag
#and finds which of the users sending those tweets has the most followers
#
#how is this useful? it probably isn't ...
#
#****************************************************
#required python packages:
#http://pypi.python.org/pypi/setuptools
#http://cheeseshop.python.org/pypi/simplejson
#http://code.google.com/p/httplib2/
#http://github.com/simplegeo/python-oauth2
#http://code.google.com/p/python-twitter/
#
#****************************************************
#you also will need a twitter account and an app registered with twitter
#registering an app with twitter you will grant you the security keys needed to query the twitter api
#
import sys
import time
import twitter
import httplib
import urllib
import json
hashtag = '#NDAA2012'
consumerKey = '<your twitter app consumer key>'
consumerSecret = '<your twitter app consumer secret>'
accessTokenKey = '<your twitter app access token key>'
accessTokenSecret = '<your twitter app access token secret>'
api = twitter.Api(
consumer_key=consumerKey,
consumer_secret=consumerSecret,
access_token_key=accessTokenKey,
access_token_secret=accessTokenSecret
)
c = httplib.HTTPConnection('search.twitter.com')
c.request('GET', '/search.json?result_type=recent&rpp=100&' + urllib.urlencode({'q':hashtag}))
result = json.loads(c.getresponse().read())
c.close()
topUserFollowCount = 0
topUser = None
while True:
for t in result['results']:
print 'checking ', t['from_user']
try:
u = api.GetUser(t['from_user_id_str'])
if u.followers_count > topUserFollowCount:
topUserFollowCount = u.followers_count
topUser = u
except:
print 'error ', sys.exc_info()[0]
if 'next_page' in result:
nextpage = result['next_page']
print 'nextpage: ', nextpage
c.request('GET', '/search.json' + nextpage)
result = json.loads(c.getresponse().read())
c.close()
else:
break
print 'user with most followers-----------------------------'
print topUser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment