Skip to content

Instantly share code, notes, and snippets.

import twitter
import networkx as NX
# Create an authenticated Api object in order to download user data
api_user='drewconway'
api_pswd='peugeot'
api=twitter.Api(username=api_user,password=api_pswd)
# The seed user to be analyzed
seed='drewconway'
import twitter
import networkx as NX
import time
# Create an authenticated Api object in order to download user data
api_user='your_twitter_id'
api_pswd='your_twitter_password'
api=twitter.Api(username=api_user,password=api_pswd)
# The seed user to be analyzed
def snowball_build(twitter_api,seed_user,rounds):
# Main network building loop
for r in range(0,rounds):
if r<1:
# Create initial egonet
net=create_egonet(twitter_api,seed_user)
if net.size()>99:
print 'Running this script will exceed your Twitter API rate limit. You already have enough friends!'
break
else:
def snowball_search(network,twitter_api,seed_user,cur_round):
# Snowball uses create_egonet to generate new structure
users=nodes_at_degree(network,cur_round) # Get all the users at the current round degree
for u in users:
time.sleep(5) # Wait five seconds in between to not hammer Twitter servers (H/T: @amyiris)
search_results=create_egonet(twitter_api,u)
network=NX.compose(network,search_results)
return network
def create_egonet(twitter_api,seed_user):
#Store network data as plain text file to be analyzed later
twitter_net._name=seed+'_k'+str(k) # Name the network after the seed and the number of snowball rounds
NX.write_edgelist(twitter_net,path='drewconway_k2.edgelist',delimiter='\t')
#Store network data as plain text file to be analyzed later
twitter_net._name=seed+'_k'+str(k) # Name the network after the seed and the number of snowball rounds
NX.write_edgelist(twitter_net,path='drewconway_k2.edgelist',delimiter='\t')
#Store network data as plain text file to be analyzed later
twitter_net._name=seed+'_k'+str(k) # Name the network after the seed and the number of snowball rounds
NX.write_edgelist(twitter_net,path='your_net.edgelist',delimiter='\t')
from networkx import *
import os
os.chdir('/Where/You/Stored/TheData/File/')
G=read_edgelist('your_net.edgelist',delimiter='\t',create_using=Graph(),nodetype=str)
info(G)
from networkx import core
kcores=core.find_cores(G)
core_items=kcores.items()
two_core=[(a) for (a,b) in core_items if b>1]
K=subgraph(G,two_core)
info(K)
N=K.neighbors('your_twitter_id')
F=[]
for u in N:
D=K.neighbors(u)
for i in D:
if N.count(i)<1 and i is not 'your_twitter_id':
F.append(i)