Permalink
Browse files

minor fixes to smooth out inconsistencies and clarify what's going on

  • Loading branch information...
1 parent 86cd6f3 commit 8b424624b373083ab726b3d27fbd3a242b310857 @ptwobrussell committed Jan 28, 2011
Showing with 24 additions and 17 deletions.
  1. +1 −1 recipe__crawl.py
  2. +23 −16 recipe__create_friendship_graph.py
View
@@ -71,7 +71,7 @@ def get_all_followers_ids(user_id, limit):
_id = str(t.users.show(screen_name=SCREEN_NAME)['id'])
- crawl_followers(t, redis.Redis(), [_id], limit=10)
+ crawl_followers(t, redis.Redis(), [_id])
# The total number of nodes visited represents one measure of potential influence.
# You can also use the user => follower ids information to create a
@@ -1,50 +1,57 @@
# -*- coding: utf-8 -*-
-# XXX: TEST ME
-
import os
import sys
import networkx as nx
import redis
+import twitter
+
+from recipe__setwise_operations import get_redis_id
-from recipe__setwise_operations import getRedisId
+SCREEN_NAME = sys.argv[1]
-ID = sys.argv[1]
+t = twitter.Twitter(api_version='1', domain='api.twitter.com')
-g = nx.Graph() # An undirected graph
+_id = str(t.users.show(screen_name=SCREEN_NAME)['id'])
+
+g = nx.Graph() # An undirected graph
r = redis.Redis()
# Compute all ids for nodes appearing in the graph. Let's assume you've
-# used recipe__crawl to harvest *all of the frienships* that exist
-# for a group of users so that you can build a graph to see how these
-# friendships relate to one another in a graph
+# adapted recipe__crawl to harvest all of the friends and friends' friends
+# for a user so that you can build a graph to inspect how these
+# friendships relate to one another
-# Create a collection of ids for a seed user and all of this user's friends
+# Create a collection of ids for a person and all of this person's friends
-ids = [ID] + list(r.smembers(getRedisId('friend_ids', user_id=ID)))
+ids = [_id] + list(r.smembers(get_redis_id('friend_ids', user_id=_id)))
# Process each id in the collection such that edges are added to the graph
-# for each of current_id's friends, but only if those friends are also
-# friends of ID
+# for each of current_id's friends if those friends are also
+# friends of SCREEN_NAME. In the end, you get a "hub and spoke" graph of
+# SCREEN_NAME and SCREEN_NAME's friends, but you also see connections that
+# existing amongst SCREEN_NAME's friends as well
for current_id in ids:
+
print >> sys.stderr, 'Processing user with id', current_id
try:
- friend_ids = list(r.smembers(getRedisId('friend_ids', user_id=current_id)))
+ friend_ids = list(r.smembers(get_redis_id('friend_ids', user_id=current_id)))
friend_ids = [fid for fid in friend_ids if fid in ids]
except Exception, e:
print >> sys.stderr, 'Encountered exception. Skipping', current_id
for friend_id in friend_ids:
+ print >> sys.stderr, 'Adding edge %s => %s' % (current_id, friend_id,)
g.add_edge(current_id, friend_id)
# Optionally, pickle the graph to disk...
if not os.path.isdir('out'):
os.mkdir('out')
-filename = os.path.join('out', ID + 'friendships.gpickle')
-nx.write_gpickle(g, filename)
+f = os.path.join('out', SCREEN_NAME + '-friendships.gpickle')
+nx.write_gpickle(g, f)
-print 'Pickle file stored in: %s' % filename
+print >> sys.stderr, 'Pickle file stored in', f

0 comments on commit 8b42462

Please sign in to comment.