-
Notifications
You must be signed in to change notification settings - Fork 0
/
reputation.py
172 lines (141 loc) · 4.73 KB
/
reputation.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""Script containing method to extract a user's reputation (extract_reputation).
"""
from lxml import html
import pickle
import requests
import re
import time
from collections import defaultdict
import csv
import pdb
import random
def extract_reputation(uid):
"""Lookup a user's reputation.
Args:
uid: StackOverflow user id.
Return:
That user's reputation.
"""
page = requests.get('http://stackoverflow.com/users/%d' % uid)
tree = html.fromstring(page.content)
try:
reputation = int(re.sub(',', '', tree.xpath('//div[@title="reputation"]/text()')[0].strip()))
except:
print "PROBLEM EXTRACTING REPUTATION"
reputation = -1
return reputation
def gen_uids_reps():
with open('uids_questions.pkl', 'rb') as infile:
uids = pickle.load(infile)
uids = [i for i in uids]
random.shuffle(uids)
uids_reps = [uids, []]
with open('uids_reps.pkl', 'wb') as outfile:
pickle.dump(uids_reps, outfile)
def update_avg_reputation():
"""Call this function iteratively once StackOverflow allows more requests.
"""
uid_rep_fn = 'uids_reps.pkl'
with open(uid_rep_fn, 'rb') as infile:
uids_reps = pickle.load(infile)
reps = uids_reps[1]
if reps:
avg_rep = sum(reps) / len(reps)
print 'AVERAGE REPUTATION BEFORE'
print avg_rep
while True:
if not uids_reps[0]:
print "NO MORE UIDS"
break
uid = uids_reps[0].pop()
rep = extract_reputation(uid)
print rep
if rep == -1:
break
uids_reps[1].append(rep)
with open(uid_rep_fn, 'wb') as outfile:
pickle.dump(uids_reps, outfile)
reps = uids_reps[1]
avg_rep = float(sum(reps)) / float(len(reps))
print 'AVERAGE REPUTATION AFTER'
print avg_rep
with open('reputation_question_avg.pkl', 'wb') as outfile:
pickle.dump(avg_rep, outfile)
def average_reputation(num_to_avg=120, in_fn='uids_questions.pkl'):
"""Calculate the average repuation of question askers in the data dump.
"""
with open(in_fn, 'rb') as infile:
uids = pickle.load(infile)
uids = [i for i in uids]
choices = random.sample(xrange(len(uids)), num_to_avg)
selected_uids = [uids[i] for i in choices]
reps = []
for uid in selected_uids:
rep = extract_reputation(uid)
print rep
if rep >= 0:
reps.append(float(rep))
avg_rep = sum(reps) / len(reps)
print 'AVERAGE REPUTATION'
print avg_rep
with open('reputation_question_avg.pkl', 'wb') as outfile:
pickle.dump(avg_rep, outfile)
class ReputationCalculator:
def __init__(self):
self.questions_fn = '../pythonquestions/Questions.csv'
self.answers_fn = '../pythonquestions/Answers.csv'
self.uids = set()
self.uid2reputation = defaultdict(int)
def parse_uids(self):
"""Extract all uids.
"""
self.process_csv(True)
"""
print 'processed questions'
self.process_csv(False)
"""
print len(self.uids)
with open('uids_questions.pkl', 'wb') as outfile:
pickle.dump(self.uids, outfile)
def process_csv(self, questions=True):
"""Extract uids from the Questions/Answers file.
Args:
questions: True if processing Questions.csv. False if processing Answers.csv.
"""
in_fn = self.questions_fn if questions else self.answers_fn
cur_time = time.time()
with open(in_fn, 'rb') as infile:
reader = csv.reader(infile)
count = 0
for line in reader:
count += 1
if count == 1:
continue
if count % 100000 == 0:
print count
print 'time to process 100000: %.2f' % (time.time()-cur_time)
cur_time = time.time()
_, uid, _, _, _, _ = line
if uid != 'NA':
self.uids.add(int(uid))
print 'read %d lines' % count
def register_reputations(self):
counter = 0
cur_time = time.time()
for uid in self.uids:
counter += 1
if counter % (len(self.uids) / 10) == 0:
print 'chunk %d in %.2fs' % (counter / (len(self.uids) / 10), time.time() - cur_time)
cur_time = time.time()
self.uid2reputation[uid] = extract_reputation(uid)
with open('uid2reputation.pkl', 'wb') as outfile:
pickle.dump(self.uid2reputation, outfile)
def main():
update_avg_reputation()
return
average_reputation()
r = ReputationCalculator()
r.parse_uids()
r.register_reputations()
if __name__ == '__main__':
main()