-
Notifications
You must be signed in to change notification settings - Fork 144
/
recipe__setwise_operations.py
123 lines (82 loc) · 3.99 KB
/
recipe__setwise_operations.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
import sys
import functools
import twitter
import locale
import redis
from recipe__make_twitter_request import make_twitter_request
from recipe__oauth_login import oauth_login
# A convenience function for consistently creating keys for a
# screen name, user id, or anything else you'd like
def get_redis_id(key_name, screen_name=None, user_id=None):
if screen_name is not None:
return 'screen_name$' + screen_name + '$' + key_name
elif user_id is not None:
return 'user_id$' + user_id + '$' + key_name
else:
raise Exception("No screen_name or user_id provided to get_redis_id")
if __name__ == '__main__':
SCREEN_NAME = sys.argv[1]
MAX_IDS = int(sys.argv[2])
r = redis.Redis()
t = oauth_login()
# Harvest some friend ids
get_friends_ids = functools.partial(make_twitter_request, t, t.friends.ids)
cursor = -1
ids = []
while cursor != 0:
# Use make_twitter_request via the partially bound callable...
response = get_friends_ids(screen_name=SCREEN_NAME, cursor=cursor)
# Add the ids to the set in redis with the sadd (set add) operator
rid = get_redis_id('friend_ids', screen_name=SCREEN_NAME)
[ r.sadd(rid, _id) for _id in response['ids'] ]
cursor = response['next_cursor']
print >> sys.stderr, \
'Fetched %i total friend ids for %s' % (r.scard(rid), SCREEN_NAME)
if r.scard(rid) >= MAX_IDS:
break
# Harvest some follower ids
get_followers_ids = functools.partial(make_twitter_request, t, t.followers.ids)
cursor = -1
ids = []
while cursor != 0:
# Use make_twitter_request via the partially bound callable...
response = get_followers_ids(screen_name=SCREEN_NAME, cursor=cursor)
# Add the ids to the set in redis with the sadd (set add) operator
rid = get_redis_id('follower_ids', screen_name=SCREEN_NAME)
[ r.sadd(rid, _id) for _id in response['ids'] ]
cursor = response['next_cursor']
print >> sys.stderr, \
'Fetched %i total follower ids for %s' % (r.scard(rid), SCREEN_NAME)
if r.scard(rid) >= MAX_IDS:
break
# Compute setwise operations the data in Redis
n_friends = r.scard(get_redis_id('friend_ids', screen_name=SCREEN_NAME))
n_followers = r.scard(get_redis_id('follower_ids', screen_name=SCREEN_NAME))
n_friends_diff_followers = r.sdiffstore('temp',
[get_redis_id('friend_ids',
screen_name=SCREEN_NAME),
get_redis_id('follower_ids',
screen_name=SCREEN_NAME)])
r.delete('temp')
n_followers_diff_friends = r.sdiffstore('temp',
[get_redis_id('follower_ids',
screen_name=SCREEN_NAME),
get_redis_id('friend_ids',
screen_name=SCREEN_NAME)])
r.delete('temp')
n_friends_inter_followers = r.sinterstore('temp',
[get_redis_id('follower_ids', screen_name=SCREEN_NAME),
get_redis_id('friend_ids', screen_name=SCREEN_NAME)])
r.delete('temp')
print '%s is following %s' % (SCREEN_NAME, locale.format('%d', n_friends, True))
print '%s is being followed by %s' % (SCREEN_NAME, locale.format('%d',
n_followers, True))
print '%s of %s are not following %s back' % (locale.format('%d',
n_friends_diff_followers, True), locale.format('%d', n_friends, True),
SCREEN_NAME)
print '%s of %s are not being followed back by %s' % (locale.format('%d',
n_followers_diff_friends, True), locale.format('%d', n_followers, True),
SCREEN_NAME)
print '%s has %s mutual friends' \
% (SCREEN_NAME, locale.format('%d', n_friends_inter_followers, True))