#!/usr/bin/env python # encoding: utf-8
import tweepy
import networkx
from networkx import core
import time
import sys
import json
import codecs
import os
consumer_key=""
consumer_secret=""
access_token=""
access_token_secret=""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
AN_HOUR = 3630
TWENTY_MIN = 20*60
THIRTY_MIN = 30*60
LIMIT = 500
def create_egonet(twitter_api, seed_user):
try:
egonet = networkx.DiGraph()
if (api.rate_limit_status()['remaining_hits'] < 5):
print(api.rate_limit_status())
time.sleep(TWENTY_MIN)
friends = tweepy.Cursor(api.friends, id=seed_user).items(LIMIT)
print "processing user" + str(seed_user)
count = 0
for fr in friends:
count = count + 1
egonet.add_edge(seed_user, str(fr.screen_name))
print str(count) + ' edges added'
return egonet
except tweepy.error.TweepError as e:
print e.reason
return False
def build_friends_of_friends_network(twitter_api, cur_network):
min_degree = 1
users = nodes_at_degree(cur_network, min_degree)
for user in users:
time.sleep(1)
new_network = create_egonet(twitter_api, user)
if new_network != False:
cur_network = networkx.compose(cur_network, new_network)
print(str(networkx.info(cur_network)))
return cur_network
def nodes_at_degree(network, min_degree):
d = network.degree()
d = d.items()
return [(user) for (user,degree) in d if (degree >= min_degree)]
def save_network(network, original_seed):
network._name = "original_seed_network"
networkx.write_edgelist(network, path="twitter_network_edgelist.csv", delimiter='\t')
def core_the_network(new_network, k):
G = new_network
kcores = core.find_cores(G)
core_items = kcores.items()
two_core_or_higher = [(a) for (a,b) in core_items if b>(k-1)]
K = networkx.subgraph(G, two_core_or_higher)
print(str(networkx.info(K)))
return K
def find_core_friends(cored_network, original_seed):
K = cored_network
# get your current friends from K
N = K.neighbors(original_seed)
F = []
# for each of your current friends, gather their friends. There hopefully will be repeats.
for u in N:
D = K.neighbors(u)
for i in D:
if N.count(i) < 1 and i is not original_seed:
F.append(i)
return F
def order_new_friends(friend_counts):
F = friend_counts
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()
ff = open('suggested_friends', 'w')
for i in counts_items:
ff.write(str(i[0])+' of your friends are already following '+i[1] + '\n')
print(str(i[0])+' of your friends are already following '+i[1])
from operator import itemgetter
def find_highest_degree_friends(K):
items = K.in_degree().items()
items.sort(key=itemgetter(1))
items.reverse()
print(str(items[0:20]))
def highest_centrality(cent_dict):
cent_items=[(b,a) for (a,b) in cent_dict.iteritems()]
cent_items.sort()
cent_items.reverse()
return tuple(reversed(cent_items[0:20]))
终端执行
import TweetRecs as tr
import networkx as nx
main_net=tr.create_egonet(tr.api,'gilad')
Traceback(最近一次调用最后):
文件“”,第 1 行,在
文件“TweetRecs.py”中,第 59 行,在 create_egonet
if (api.rate_limit_status()['remaining_hits'] < 5):
KeyError: 'remaining_hits'