Skip to content

Instantly share code, notes, and snippets.

@paulozullu
Created July 7, 2016 22:46
Show Gist options
  • Save paulozullu/4b43f08fdf0b20c715a114bc1fd9fb69 to your computer and use it in GitHub Desktop.
Save paulozullu/4b43f08fdf0b20c715a114bc1fd9fb69 to your computer and use it in GitHub Desktop.
Get unique followers rate for Twitter
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 7 18:58:34 2016
@author: paulozullu
"""
from pymongo import MongoClient
''' Creates a connection to MongoDB '''
def connect_mongo(server, db):
try:
print "Connecting server %s" %(server)
client = MongoClient(server, 27017)
mongo = client[db]
print "Succesfully connected!"
except:
print "Couldn't connect with server %s" %(server)
exit(1)
return mongo
def rates():
limits = [10, 20, 50, 80, 94]
for limit in limits:
w10 = mongo['twitter_follower'].find({})[0:limit]
sum_followers = 0
unique_followers = []
for w in w10:
sum_followers += len(w['followers'])
for f in w['followers']:
if not f['id'] in unique_followers:
unique_followers.append(f['id'])
rate = float(len(unique_followers)) / float(sum_followers)
print "Para %d users temos %d / %d = %f" % (limit, len(unique_followers), sum_followers, rate)
# Main Function #
if __name__ == '__main__':
print "Starting script..."
mongo = connect_mongo('server_ip', 'db_name')
rates()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment