This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
counts = dict((k, F.count(k)) for k in set(F)) | |
counts_items=counts.items() | |
counts_items=[(b,a) for (a,b) in counts_items] | |
counts_items.sort() | |
counts_items.reverse() | |
for i in counts_items: | |
print str(i[0])+' of your friends are already following '+i[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
TwitterEgoBuilder.py | |
Created by Drew Conway on 2009-02-23. | |
Copyright (c) 2009. All rights reserved. | |
The purpose of this script is to generate a | |
NetworkX DiGraph object based on the snowball |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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') |
OlderNewer