public
Last active — forked from edumbill/friends.py

  • Download Gist
friends.py
Python
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
# a messy hack written by Edd Dumbill. http://twitter.com/edd
 
# You may need to rerun this script if you hit a Twitter Error because you
# use up API rate limiting. That's why we pickle the results, so we can resume
# where we left off.
 
# get the twitter module using 'easy_install twitter'
 
from twitter.api import Twitter, TwitterError
from twitter.oauth import OAuth
from xml.sax.saxutils import escape
import time
import pickle
import os
 
CONSUMER_KEY = 'XXXXXXXXXXXXXXXXXXXXXXX'
CONSUMER_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
MY_SCREEN_NAME = 'edd'
 
def all_friends(api, screen_name):
friends = []
cursor = -1
while cursor != 0:
print("Cursor is %d: name is %s" % (cursor, screen_name))
r = api.statuses.friends(cursor=cursor, screen_name=screen_name)
if len(r['users']) > 0:
friends = friends + r['users']
cursor = r['next_cursor']
return friends
 
api = Twitter(
auth=OAuth('12345-jhkdgfjdshgfdsjhdsjhgf', # replace with your OAuth key
'DKJHfkjdshkjdshfLKDJHSFKSDJHFdjsfhs', # replace with OAuth token
CONSUMER_KEY, CONSUMER_SECRET), # use your own, as defined above
api_version='1',
domain='api.twitter.com')
 
me = api.users.show(screen_name=MY_SCREEN_NAME)
my_friends = all_friends(api, MY_SCREEN_NAME)
friend_ids = {}
 
if os.path.exists('friends.pickle'):
pfile = open('friends.pickle', 'r')
friend_ids = pickle.load(pfile)
print(friend_ids.keys())
pfile.close()
 
friend_ids[me['id']] = api.friends.ids(screen_name=MY_SCREEN_NAME)
 
print("Enumerating %d friends" % len(my_friends))
 
try:
for u in my_friends:
if not friend_ids.has_key(u['id']):
print("Finding friends of %s, id %d" % (u['name'], u['id']))
friend_ids[u['id']] = api.friends.ids(user_id=u['id'])
time.sleep(1)
else:
print("Already loaded friends of %s, id %d" % (u['name'], u['id']))
except TwitterError:
print("Got Twitter HTTP Error")
finally:
friend_ids = dict((k,list(v)) for k,v in friend_ids.iteritems())
pfile = open('friends.pickle', 'w')
pickle.dump(friend_ids, pfile)
print("Saved friends to pickle")
pfile.close()
 
_escape = escape
escape = lambda s: _escape(s.encode('utf8'))
 
out = open("graph.graphml", "w")
out.write("""<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
 
<key id="name" for="node" attr.name="name" attr.type="string" />
 
<graph edgedefault="directed">
""")
 
out.write("<node id='friend%d'><data key='name'>%s</data></node>\n" % (me['id'], escape(me['name'])))
# making a set of the friends as Twitter read process sometimes includes dupes
friends_set=set([(x['id'],x['name']) for x in my_friends])
for (f_id, f_name) in friends_set:
out.write("<node id='friend%d'><data key='name'>%s</data></node>\n" % (f_id, escape(f_name)))
 
edge_id = 0
my_fids = set(friend_ids[me['id']])
written = {}
for u in my_friends:
if written.has_key(u['id']):
break
else:
written[u['id']] = True
out.write("<!-- friends of %s -->\n" % escape(u['name']))
# write an edge for my relationship to them
out.write("<edge id='edge%d' source='friend%d' target='friend%d' />\n" % (edge_id, me['id'], u['id']))
edge_id = edge_id + 1
# now write edges for each of their connections within my follower network
common_friend_ids = my_fids.intersection(set(friend_ids[u['id']]))
for c in common_friend_ids:
out.write("<edge id='edge%d' source='friend%d' target='friend%d' />\n" % (edge_id, u['id'], c))
edge_id = edge_id + 1
 
out.write("""</graph></graphml>""")
out.close()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.