February 15, 2010

Visualize your own twitter graph – Part 2

Here is real (less shameful) version of my twitter graph generator. In fact, the last one was just a little test. This version actually generate a real graph with all the nodes adjacents to the twitter user. The final graph containt around 6200+ nodes. You can get a sample (4597 × 4699) of the image here. The real botteneck of the script are in the memory consumption of the PyGraphviz package used to draw the image of the graph, which is is a Python interface to the Graphviz graph layout and visualization package.

# Import pygraph
from pygraph.classes.graph import graph
from pygraph.classes.digraph import digraph
from pygraph.algorithms.searching import breadth_first_search
from pygraph.algorithms.traversal import traversal
from pygraph.readwrite.dot import write
 
# Import pygraphviz
from pygraphviz import *
 
# Import twython
import twython.core as twython
 
 
def getUserData(userID):
	'''
	Retrieve the social graph info from the user
	'''
	followingList = twitter.getFriendsIDs(id=userID)['ids']
	followersList = twitter.getFollowersIDs(id=userID)['ids']
	return [followingList, followersList]
 
 
def addUserToGraph(twGraph, userID):
	'''
	Add the user to the digraph
	'''	
	[followingList, followersList] = getUserData(userID)
 
	for twitterUser in followingList:
		if twitterUser not in twGraph.nodes():
			twGraph.add_node(twitterUser)
		if (userID, twitterUser) not in twGraph.edges():
			twGraph.add_edge([userID, twitterUser])
 
	for twitterUser in followersList:
		if twitterUser not in twGraph.nodes():
			twGraph.add_node(twitterUser)
		if (twitterUser, userID) not in twGraph.edges():
			twGraph.add_edge([twitterUser, userID])
 
	print str(len(twGraph)) + ' nodes'
 
 
# Some Parameters
useScreenName = False
Depth = 1
twitter = twython.setup(username="YOURUSERNAME", password="YOURPASSWORD")
 
# List of users to graph
myName = 'mlaprise'
myID = twitter.showUser(screen_name=myName)['id']
[followingList, followersList] = getUserData(myID)
 
# Graph creation
twitterGraph = digraph()
twitterGraph.add_node(myID)
addUserToGraph(twitterGraph, myID)
 
# Graph traversal
for d in range(Depth):
	retrievalItr = traversal(twitterGraph, myID, 'post')
	try:	
		while 1:
			userID=retrievalItr.next()
			addUserToGraph(twitterGraph, userID)
	except StopIteration:
		print 'Depth ' +str(d) ' Done !'
 
# Construct the image of the graph
dot = write(twitterGraph)
twitterGraphViz = AGraph(string=dot)
twitterGraphViz.graph_attr['label']='Twitter Graph of ' + myName
twitterGraphViz.graph_attr['dpi'] = '2'
twitterGraphViz.graph_attr['overlap'] = 'scale'
twitterGraphViz.node_attr['shape']='circle'
twitterGraphViz.node_attr['label']= ''
twitterGraphViz.node_attr['color']= 'blue'
twitterGraphViz.node_attr['style']= 'filled'
twitterGraphViz.edge_attr['color']='black'
twitterGraphViz.layout()
 
# Draw as PNG
twitterGraphViz.draw(myName + '_graph.png')

Leave a Reply