-
Notifications
You must be signed in to change notification settings - Fork 1
/
weibolauncher.py
310 lines (231 loc) · 6.9 KB
/
weibolauncher.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
305
306
307
308
309
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Author: Xihao Liang
Created: 2016.03.06
'''
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from optparse import OptionParser
import re
import json
import time
import threading
import commdatica
import weiboparser
from weiboparser import WeiboParser
from utils.urlopener import UrlOpener
from utils.logger import Logger
logger = Logger(sys.stderr)
JSONS_COMMENT = 'data/comments.txt'
def load(fname):
'''
load all the comments downloaded and saved in JSONS_COMMENT
'''
if not os.path.exists(fname):
return []
fobj = open(fname, 'r')
comments = []
for line in fobj:
comments.append(json.loads(line))
fobj.close()
return comments
def downloaded_mids(fname):
'''
get the list of mids from JSONS_COMMENT
which are the mids of the blogs whose comments have been downloaded
'''
comments = load(fname)
mids = [] if comments == [] else [comm['mid'] for comm in comments]
return mids
class ListIterator:
'''
an iterator for multi-threading
'''
def __init__(self, data, iter_round = False):
self.data = data
self.datalen = len(data)
self.idx = 0
self.iter_round = iter_round
self.lock = threading.Lock()
def restart(self):
self.lock.acquire()
self.idx = 0
self.lock.release()
def next(self):
self.lock.acquire()
if self.idx == self.datalen:
if not self.iter_round:
self.lock.release()
return None, None
else:
self.idx = 0
this_idx = self.idx
datum = self.data[self.idx]
self.idx += 1
self.lock.release()
return datum, this_idx
class SharedOutFile:
'''
a write file with threading lock though I don't know if it is necessary
'''
def __init__(self, fname, ftype = 'w'):
self.fobj = open(fname, ftype)
self.lock = threading.Lock()
def write(self, line):
self.lock.acquire()
self.fobj.write(line)
self.lock.release()
def close(self):
self.fobj.close()
class WeiboLauncher:
'''
a launcher for parsing weibo comments by multi-threading
'''
def __init__(self):
pass
def load_accounts(self, accounts):
'''
initialize a round iterator for accounts
'''
self.iter_account = ListIterator(accounts, iter_round = True)
def load_bloginfo(self, bloginfo):
'''
initialize a iterator for bloginfos
'''
self.iter_bloginfo = ListIterator(bloginfo)
def init_outfile(self, fname, ftype):
'''
initialize a output file object for threading
'''
self.outfile = SharedOutFile(fname, ftype)
def close_outfile(self):
'''
close the output file
'''
if self.outfile is not None:
self.outfile.close()
def thread_parse(self, interval, thread_name = None):
'''
an instance for parseing weibo comments
which sleep for $interval seconds between every loop
'''
flag = True
wbparser = WeiboParser()
bloginfo, bidx = self.iter_bloginfo.next()
account, _ = self.iter_account.next()
flag = not (bloginfo is None or account is None)
fail_count = 0
while flag and not self.thread_interrupt:
#weibolauncher.thread_parse:
logger.info('<BLOG %d>: thread_%s downloading (%s %s)'%(
bidx, thread_name, bloginfo.uid, bloginfo.mid))
wbparser.set_account(account)
ret = wbparser.parse(bloginfo.uid, bloginfo.mid)
if ret == None:
logger.info('<BLOG %d>: thread_%s failed'%(bidx, thread_name))
fail_count += 1
if fail_count > 10:
print 'fail > 10'
break
else:
comm, ids = ret
blog = {}
blog.update(bloginfo.todict())
blog['comments_count'] = len(comm)
blog['comments'] = comm
blog['ids'] = ids
self.outfile.write(json.dumps(blog).replace('\n', ' ') + '\n')
if fail_count > 0:
fail_count -= 1
if self.thread_interrupt:
logger.info('thread_%s interrupted'%(thread_name))
break
bloginfo, bidx = self.iter_bloginfo.next()
account, _ = self.iter_account.next()
flag = not (bloginfo is None or account is None)
if flag:
time.sleep(interval)
def launch(self, n_instance, interval):
'''
parse weibo comments by $n_instance threads
accounts and bloginfo must be initialized by init_account and init_bloginfo in advance
'''
intr_lock = threading.Lock()
self.thread_interrupt = False
threads = []
for i in range(n_instance):
thread = threading.Thread(target = self.thread_parse, args = (interval, i, ))
thread.start()
threads.append(thread)
try:
for thread in threads:
thread.join()
except KeyboardInterrupt:
self.thread_interrupt = True
print 'after keyboardinterrupt'
for thread in threads:
thread.join()
def test():
all_accounts = weiboparser.load_accounts()
accounts = all_accounts[:25]
all_bloginfo = commdatica.load()
# do not download comments for the same blog again
mids = set(downloaded_mids())
filtered_bloginfo = [bloginfo for bloginfo in all_bloginfo if not bloginfo.mid in mids]
bloginfo = filtered_bloginfo[:8]
launch(JSONS_COMMENT, accounts, bloginfo, 4, 'w')
def launch(outfile, accounts, bloginfo, ftype, n_instance, interval):
launcher = WeiboLauncher()
launcher.load_accounts(accounts)
launcher.load_bloginfo(bloginfo)
launcher.init_outfile(outfile, ftype)
launcher.launch(n_instance, interval)
launcher.close_outfile()
def main():
# get parameters from terminal
optparser = OptionParser()
optparser.add_option('-i', '--input', action = 'store', type = 'string', dest = 'infile')
optparser.add_option('-o', '--output', action = 'store', type = 'string', dest = 'outfile')
optparser.add_option('-a', '--account', action = 'store', type = 'string', dest = 'acc_range')
optparser.add_option('-n', '--instance', action = 'store', type = 'int', dest = 'n_instance', default = 5)
optparser.add_option('-r', '--restart', action = 'store_true', dest = 'restart', default = False)
optparser.add_option('-t', '--interval', action = 'store', type = 'int', dest = 'interval', default = 3)
opts, args = optparser.parse_args()
if not opts.infile:
print '-i infile not specified'
return
if not opts.outfile:
print '-o outfile not specified'
return
if not opts.acc_range:
print '-a (start_idx,end_idx) not specified'
return
else:
m = re.match('(\d+),(\d+)', opts.acc_range)
if not m:
print '-a start_idx,end_idx should contain no space'
return
else:
opts.acc_range = (int(m.group(1)), int(m.group(2)))
ftype = 'w' if opts.restart else 'a'
# prepare the accounts
all_accounts = weiboparser.load_accounts()
accounts = all_accounts[opts.acc_range[0]:opts.acc_range[1] + 1]
# prepare the
all_bloginfo = commdatica.load(opts.infile)
# filter the blogs whose comments have been downloaded
if opts.restart:
mids = set()
else:
mids = set(downloaded_mids(opts.outfile))
logger.info('%d downloaded in %s'%(len(mids), opts.outfile))
bloginfos = [bloginfo for bloginfo in all_bloginfo if not bloginfo.mid in mids]
# for test
# bloginfos = bloginfos[:20]
launch(opts.outfile, accounts, bloginfos, ftype, opts.n_instance, opts.interval)
if __name__ == '__main__':
#test()
main()