Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Make a graph of your social network from those you follow
# a messy hack written by Edd Dumbill.
# 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
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
me =
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)
friend_ids[me['id']] = api.friends.ids(screen_name=MY_SCREEN_NAME)
print("Enumerating %d friends" % len(my_friends))
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'])
print("Already loaded friends of %s, id %d" % (u['name'], u['id']))
except TwitterError:
print("Got Twitter HTTP Error")
pfile = open('friends.pickle', 'w')
pickle.dump(friend_ids, pfile)
print("Saved friends to pickle")
out = open("graph.graphml", "w")
out.write("""<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns=""
<key id="name" for="node""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']):
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

This comment has been minimized.

Copy link
Owner Author

commented Dec 9, 2010

Several people have pointed out that the Python NetworkX library would have made writing the GraphML easier.


This comment has been minimized.

Copy link

commented Jan 23, 2014

This is great, and I can't wait to try it, but it gives me an error because "Twitter REST API" is no longer active". Are you still using it and if so could you please share your updated script?

Thanks! Andre

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.