-
Notifications
You must be signed in to change notification settings - Fork 5
/
twitter_network.py
50 lines (36 loc) · 1.35 KB
/
twitter_network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import glob
import os
import json
import sys
from collections import defaultdict
users = {}
for f in glob.glob('twitter-users/*.json'):
with open(f) as data_file:
data = json.load(data_file)
screen_name = data['screen_name']
users[screen_name] = { 'followers': data['followers_count'] }
SEED = 'ToferC'
def process_follower_list(screen_name, edges=[], depth=0, max_depth=2):
f = os.path.join('following', screen_name + '.csv')
if not os.path.exists(f):
return edges
with open(f) as data_file:
followers = [line.strip().split('\t') for line in data_file]
for follower_data in followers:
if len(follower_data) < 2:
continue
screen_name_2 = follower_data[1]
# use the number of followers for screen_name as the weight
weight = users[screen_name]['followers']
edges.append([screen_name, screen_name_2, weight])
if depth+1 < max_depth:
process_follower_list(screen_name_2, edges, depth+1, max_depth)
return edges
edges = process_follower_list(SEED, max_depth=3)
with open('twitter_network.csv', 'w') as outf:
edge_exists = {}
for edge in edges:
key = ','.join([str(x) for x in edge])
if not(key in edge_exists):
outf.write('%s\t%s\t%d\n' % (edge[0], edge[1], edge[2]))
edge_exists[key] = True