Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Twitter's most popular

View TwitterClient
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#!/usr/bin/env python2.6
 
import pycurl, json
 
 
user = "YOURTWITTERNAME"
password = "YOURTWITTERPASSWORD"
streamurl = "https://stream.twitter.com/1/statuses/filter.json"
track = "locations=-74,40,-73,41" # nyc
 
 
class TwitterClient:
# set curl options
def __init__(self):
self.persons = []
self.buffer = ""
self.connection = pycurl.Curl()
self.connection.setopt(pycurl.URL, streamurl)
self.connection.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_ANY)
self.connection.setopt(pycurl.USERPWD, "{0}:{1}".format(user, password))
self.connection.setopt(pycurl.POST, 1)
self.connection.setopt(pycurl.POSTFIELDS, track)
self.connection.setopt(pycurl.WRITEFUNCTION, self.curlWriteback)
 
# try to talk to the api
try:
self.connection.perform()
except pycurl.error as e:
print("ooops")
finally:
self.connection.close()
 
 
# handle respones from api
def curlWriteback(self, data):
self.buffer += data
 
if data.endswith("\r\n"):
content = json.loads(self.buffer)
user = content["user"]["screen_name"].encode("utf-8").replace("\n", " ")
followers = content["user"]["followers_count"]
 
self.processTuples(user, followers)
self.buffer = ""
 
 
# for now we put the tuples in a list; feel free to improve this
def processTuples(self, user, followers):
self.persons.append((user, followers))
 
# print the list every time we add 50 new tuples
if(len(self.persons) % 50 == 0):
self.showResults()
 
 
# remove duplicates, sort by followers and print the tuples
def showResults(self):
self.persons = sorted(dict(self.persons).items(), key=lambda x: x[1], reverse=True)
 
print("Twitter's most popular (looked at {0} so far)".format(len(self.persons)))
for name, followers in self.persons[:10]:
print("{0}, {1}".format(followers, name))
 
 
# entry
if __name__ == '__main__':
TwitterClient()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.