Skip to content

@edumbill /friends.py
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Make a graph of your social network from those you follow
# 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:
pfile = open('friends.pickle', 'w')
pickle.dump(friend_ids, pfile)
print("Saved friends to pickle")
pfile.close()
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()
@edumbill
Owner

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

@periagoge

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
Something went wrong with that request. Please try again.