-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitter-relationships.py
executable file
·87 lines (72 loc) · 2.18 KB
/
twitter-relationships.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# encoding: utf-8
import sys
import argparse
import json
FOLLOWER = '<-'
FOLLOWING = '->'
RELATIONS = [
{FOLLOWER, FOLLOWING},
{FOLLOWER},
{FOLLOWING}
]
def relation_to_string(rel, reverse=False):
d1 = FOLLOWER in rel
d2 = FOLLOWING in rel
if reverse:
(d1, d2) = (d2, d1)
s1 = "<" if d1 else "="
s2 = ">" if d2 else "="
return f"{s1}=={s2}"
# Returns true if all items in `items` are also in `col`,
# otherwise, returns false
def all_in(items):
def f(col):
for item in items:
if not item in col:
return False
return True
return f
def reverse(txt: str):
return txt[::-1]
parser = argparse.ArgumentParser()
parser.add_argument('user1', type=str)
parser.add_argument('user2', type=str)
args = parser.parse_args()
data = json.load(sys.stdin)
all_users = {}
tgtuser1, tgtuser2 = tgtusers = [args.user1, args.user2]
for tgtuser in tgtusers:
for follower in data[tgtuser]['followers']:
all_users.setdefault(follower, {})
all_users[follower].setdefault(tgtuser, set())
all_users[follower][tgtuser].add(FOLLOWER)
for following in data[tgtuser]['followings']:
all_users.setdefault(following, {})
all_users[following].setdefault(tgtuser, set())
all_users[following][tgtuser].add(FOLLOWING)
common_users = {
user: value
for (user,value) in all_users.items()
if all_in(tgtusers)(value.keys())
}
# from pprint import pprint
# pprint(common_users)
if tgtuser1 in all_users and tgtuser2 in all_users[tgtuser1]:
rel = relation_to_string(all_users[tgtuser1][tgtuser2], reverse=True)
print(f"{tgtuser1} {rel} {tgtuser2}")
print()
for r1 in RELATIONS:
for r2 in RELATIONS:
group_users = []
for (user, value) in common_users.items():
if value[tgtuser1] == r1 and value[tgtuser2] == r2:
group_users.append(user)
if not group_users:
continue
rels1 = relation_to_string(r1)
rels2 = relation_to_string(r2, reverse=True)
print(f"{tgtuser1} {rels1} X {rels2} {tgtuser2}")
for user in group_users:
print(user)
print()