-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrawlboard.py
304 lines (244 loc) · 9.37 KB
/
crawlboard.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -*- coding: utf-8 -*-
import urllib
import time, os
from datetime import datetime
TOTAL_SCORE = 30000
NOMINATE = {'chunzai':2}
SKIP_AUTHORS = ['CAQ9', 'deliver', 'SYSOP']
def print_dictionary(dictionary, keys_sorted, translate_keys=None, topk=-1):
result = []
if topk == 0:
return result
count = 0
l = len(keys_sorted)
lastvalue = None
enough = False
for key in keys_sorted:
value = dictionary[key]
if lastvalue != value and enough:
break
lastvalue = value
count += 1
print '\t',
if translate_keys is None:
print ('未知' if key is None else key),
if key is not None:
result.append(key)
else:
if key in translate_keys:
print translate_keys[key],
result.append(translate_keys[key])
else:
print '未知',
#result.append(None)
print '\t', value,
if topk > 0:
print ''
if count >= topk:
enough = True
else:
# print '\t', int(count * 10000.0 / l + 0.5) / 100.0, '%'
print ''
return result
def add_to_dict(dictionary, key, value=1):
if key in dictionary:
dictionary[key] += value
else:
dictionary[key] = value
def crawl(filename):
f = open(filename, 'w')
f.write('# postid, gid, author, flag, posttime, title, size, imported, is_tex\n')
postinfos = []
# Start at the latest page
page_str = ''
while True:
# Crawl and parse the page
print 'Crawling',
conn = urllib.urlopen('http://www.newsmth.net/bbsdoc.php?board=ClassicalMusic&ftype=0' + page_str)
content_gb2312 = conn.read()
content_utf8 = content_gb2312.decode('gb2312', 'ignore').encode('utf-8')
# var c = new docWriter('ClassicalMusic',24,21750,0,0,726,21779,'/groups/rec.faq/ClassicalMusic',1,1);
ind0 = content_utf8.find('new docWriter(')
boardinfo = content_utf8[content_utf8.find('(', ind0) + 1 : content_utf8.find(')', ind0)].strip()
# 'ClassicalMusic',24,21750,0,0,726,21779,'/groups/rec.faq/ClassicalMusic',1,1
(boardname, num1, current_page_start_num, num2, num3, current_page, therest) = boardinfo.split(',', 6)
current_page = int(current_page)
print 'page', current_page,
# c.o(78035,78035,'CAQ9','d ',1316404312,'新浪微博 @水木古典音乐 欢迎关注 ',168,0,0);
ind1 = content_utf8.find('c.o(')
ind2 = content_utf8.rfind('c.t();')
posts = content_utf8[ind1 : ind2].strip().split('c.o')
page_mintime = -1
page_maxtime = -1
stored_count = 0
for post in posts:
# (78035,78035,'CAQ9','d ',1316404312,'新浪微博 @水木古典音乐 欢迎关注 ',168,0,0);
if not post.startswith('('):
continue
t = post.strip()
t = t[1 : len(t) - 2]
# 78035,78035,'CAQ9','d ',1316404312,'新浪微博 @水木古典音乐 欢迎关注 ',168,0,0
(postid, gid, author, flag, posttime, therest) = t.split(',', 5)
(title, size, imported, is_tex) = therest.rsplit(',', 3)
# Note we do it in 2 steps because title may contain commas
# Refine the values
postid = int(postid)
gid = int(gid)
author = author[1 : -1]
flag = flag[1 : -1]
posttime = datetime.fromtimestamp(int(posttime))
title = title[1 : -1]
size = int(size)
imported = not (imported == '0')
is_tex = not (is_tex == '0')
if flag.find('d') >= 0:
# Sticky bottom
continue
if page_mintime == -1 or posttime < page_mintime:
page_mintime = posttime
if page_maxtime == -1 or posttime > page_maxtime:
page_maxtime = posttime
if posttime >= startdate and posttime < enddate:
fields = [postid, gid, author, flag, posttime, title, size, imported, is_tex]
postinfos.append(fields)
line = '\t'.join([x if type(x) == 'unicode' else str(x) for x in fields])
f.write(line + '\n')
stored_count += 1
print stored_count, 'posts.'
if page_mintime < startdate:
# Stop crawling
break
page_str = '&page=' + str(current_page - 1)
f.flush()
time.sleep(1)
# Now all posts have been crawled
f.close()
return postinfos
# MAIN
# Determine the time range of the posts
now = datetime.now()
#startdate = datetime(now.year + (now.month - 3) / 12, (now.month - 3) % 12 + 1, 1, 0, 0, 0, 0)
#enddate = datetime(now.year + (now.month - 2) / 12, (now.month - 2) % 12 + 1, 1, 0, 0, 0, 0)
startdate = datetime(2013, 1, 1, 0, 0, 0, 0)
enddate = datetime(2013, 7, 1, 0, 0, 0, 0)
filename = 'posts/' + startdate.strftime('%Y%m') + '-' + enddate.strftime('%Y%m') + '.txt'
# Read the posts information from file or crawling
postinfos = []
if not os.path.exists(filename):
postinfos = crawl(filename)
else:
f = open(filename)
for line in f:
if line.find('#') == 0:
continue
fields = line.strip().split('\t')
postinfos.append(fields)
f.close()
# Analysis begins
print filename
print '********************'
print '原始数据如下:'
print
print '该时期内帖子总数:', len(postinfos)
numposts = {} # author - num of posts
originals = {} # author - num of original posts
gid_author = {} # gid - author
markedposts = {} # author - num of marked posts
longposts = {} # author - num of long posts
LONG_THRESHOLD = 100
for postinfo in postinfos:
# [postid, gid, author, flag, posttime, title, size, imported, is_tex]
(postid, gid, author, flag, posttime, title, size, imported, is_tex) = tuple(postinfo)
if author not in SKIP_AUTHORS:
add_to_dict(numposts, author)
if postid == gid:
gid_author[gid] = author
if author not in SKIP_AUTHORS:
add_to_dict(originals, author)
if flag.find('m') >= 0:
if author not in SKIP_AUTHORS:
add_to_dict(markedposts, author)
if int(size) > LONG_THRESHOLD:
if author not in SKIP_AUTHORS:
add_to_dict(longposts, author)
original_replies = {} # author - num of posts replied by others
for postinfo in postinfos:
(postid, gid, author, flag, posttime, title, size, imported, is_tex) = tuple(postinfo)
if postid == gid:
continue
original_author = gid_author[gid] if gid in gid_author else None
if author == original_author or original_author in SKIP_AUTHORS:
continue
add_to_dict(original_replies, original_author)
reward_post = []
numposts_sorted = sorted(numposts, key=lambda x: -numposts[x])
print '发文数',
print '(发文作者人数', len(numposts), ')'
rewarded = print_dictionary(numposts, numposts_sorted, topk=10)
for r in rewarded:
reward_post.append(r)
originals_sorted = sorted(originals, key=lambda x: (-originals[x], x))
print '原创数',
print '(原创作者人数', len(originals), ')'
rewarded = print_dictionary(originals, originals_sorted, topk=20)
for r in rewarded:
reward_post.append(r)
original_replies_sorted = sorted(original_replies, key=lambda x: -original_replies[x])
print '发表原创引来的其他人回复数',
print '(该部分作者人数', len(original_replies), ')'
rewarded = print_dictionary(original_replies, original_replies_sorted, topk=10)
for r in rewarded:
reward_post.append(r)
markedposts_sorted = sorted(markedposts, key=lambda x: -markedposts[x])
print '被m的帖子数',
print '(作者人数', len(markedposts), ')'
rewarded = print_dictionary(markedposts, markedposts_sorted)
for r in rewarded:
reward_post.append(r)
longposts_sorted = sorted(longposts, key=lambda x: -longposts[x])
print '长文数(长度 >', LONG_THRESHOLD, ')',
print '(该部分作者人数', len(longposts), ')'
rewarded = print_dictionary(longposts, longposts_sorted, topk=10)
for r in rewarded:
reward_post.append(r)
print '********************'
print '本次奖励积分数:', TOTAL_SCORE
print '发帖奖励总人次:', len(reward_post)
nominate_sum = sum(NOMINATE[x] for x in NOMINATE)
print '提名指标:', nominate_sum
zhuban = []
f = open('zhuban.txt')
for line in f:
zhuban.append(line.strip())
f.close()
print '驻版指标:', 1, ',由', len(zhuban), '人共享'
unit_score = TOTAL_SCORE / (len(reward_post) + nominate_sum + 1)
print
print '每个指标', unit_score, '分,',
reward_result = {}
for r in reward_post:
add_to_dict(reward_result, r)
for r in NOMINATE:
add_to_dict(reward_result, r, value=NOMINATE[r])
for r in zhuban:
add_to_dict(reward_result, r, value=(1.0 / len(zhuban)))
print '奖励共', len(reward_result), '人,名单如下:'
reward_result_sorted = sorted(reward_result, key=lambda x:x.lower())
total1 = 0
total2 = 0
print 'ID\t指标数\t积分'
for r in reward_result_sorted:
value1 = int(reward_result[r] * 100.0 + 0.5) / 100.0
value2 = int(reward_result[r] * unit_score)
print r, '\t', value1, '\t', value2
total1 += value1
total2 += value2
print '总计\t', total1, '\t', total2
print '********************'
print '提名ID\t加权系数'
for r in NOMINATE:
print r, '\t', NOMINATE[r]
print
print '当前驻版:',
for r in zhuban:
print r,