-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_followers_stats.py
46 lines (43 loc) · 1.88 KB
/
twitter_followers_stats.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
import sys
import json
from random import sample
import time
def usage():
print("Usage:")
print("python {} <username>".format(sys.argv[0]))
if __name__ == '__main__':
if len(sys.argv) != 2:
usage()
sys.exit(1)
screen_name = sys.argv[1]
followers_file = 'users/{}/followers.jsonl'.format(screen_name)
friends_file = 'users/{}/friends.jsonl'.format(screen_name)
with open(followers_file) as f1, open(friends_file) as f2:
t0 = time.time()
followers = []
friends = []
for line in f1:
profile = json.loads(line)
followers.append(profile['screen_name'])
for line in f2:
profile = json.loads(line)
friends.append(profile['screen_name'])
t1 = time.time()
mutual_friends = [user for user in friends if user in followers]
followers_not_following = [user for user in followers if user not in friends]
friends_not_following = [user for user in friends if user not in followers]
t2 = time.time()
print("============= Timing =============")
print("Initialize data: {}".format(t1-t0))
print("Set-based operations: {}".format(t2-t1))
print("Total time: {}".format(t2-t0))
print("============= Stats =============")
print("{} has {} followers".format(screen_name, len(followers)))
print("{} has {} friends".format(screen_name, len(friends)))
print("{} has {} mutual friends".format(screen_name, len(mutual_friends)))
print("{} friends are not following {} back".format(len(friends_not_following), screen_name))
print("{} followers are not followed back by {}".format(len(followers_not_following), screen_name))
# some_mutual_friends = ', '.join(sample(mutual_friends, 5))
print("Printing all mutual friends")
for friends in mutual_friends:
print (friends)