-
Notifications
You must be signed in to change notification settings - Fork 4
/
consumers.py
444 lines (324 loc) · 10.6 KB
/
consumers.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
from __future__ import print_function
import gzip
import logging
import sys
import time
import csv
try:
from Queue import Full
except ImportError:
from queue import Full
from collections import OrderedDict, Counter
from contextlib import contextmanager
from itertools import chain
from pprint import pprint as _pprint
from poultry.tweet import Tweet, TweetValueError
logger = logging.getLogger(__name__)
def consumer(func):
"""A decorator function that takes care of starting a coroutine automatically on call.
See http://www.dabeaz.com/generators/ for more details.
"""
def start(*args, **kwargs):
cr = func(*args, **kwargs)
next(cr)
return cr
return start
@consumer
def nop(*args, **kwargs):
item = None
while True:
item = yield item
@consumer
def show(output=None, template=u'{t}\n'):
"""Print tweet text and meta information."""
if output is None:
output=sys.stdout
while True:
try:
print(template.format(t=(yield)), file=output)
except KeyError:
pass
@consumer
def print_(output=None, template=u'{}\n'):
"""Print stripped items."""
if output is None:
output = sys.stdout
while True:
item = yield
print(template.format((item).strip('\n')), file=output)
@consumer
def pprint():
"""Pretty print tweet's json object."""
while True:
tweet = (yield)
_pprint(tweet.parsed)
@consumer
def print_text(output=None):
"""Print only tweet's text."""
if output is None:
output = sys.stdout
while True:
tweet = yield
print(tweet.text.replace(u'\n', u' '), file=output)
@consumer
def counter_printer(output=None):
if output is None:
output = sys.stdout
while True:
counter = yield
for key, value in sorted(counter.items()):
output.write('{} {}\n'.format(key, value))
@consumer
def to_tweet(target):
"""Convert the input items to tweets."""
result = None
with closing(target):
while True:
item = yield result
try:
tweet = Tweet(item)
except TweetValueError:
result = SendNext
else:
result = target.send(tweet)
@consumer
def group(
file_name_template='%Y-%m-%d-%H.gz',
max_open_files=1,
mode='a',
):
"""Group tweets to files by date according to the file_name_template."""
files = OrderedDict()
try:
while True:
tweet = yield
created_at = tweet.created_at.strftime(file_name_template)
try:
f = files[created_at]
except KeyError:
print(created_at)
if len(files) > max_open_files:
files = OrderedDict(sorted(files.items()))
_, first = files.popitem(last=False)
first.close()
f = gzip.open(created_at, '{}t'.format(mode))
files[created_at] = f
f.write("{t}\n".format(t=tweet.raw))
finally:
for f in files.values():
f.close()
@consumer
def filter(streams, dustbin=None, send_to_all=True):
"""Filter items to flows by filtering predicates.
:param streams: sequence of `(target, predicate)` pairs.
:param send_to_all: if set to `False` then in case an item
satisfies several predicates it will be sent
to only one.
`predicate` is a function: current, first --> Bool
"""
def update_all_targets():
all_targets = [t for t, _ in streams]
if dustbin is not None:
all_targets.append(dustbin)
return all_targets
all_targets = update_all_targets()
with closing(mutable_targets=all_targets):
current = first = yield
while True:
sent = False
for target, predicate in streams:
if predicate(current, first):
target.send(current)
sent = True
if not send_to_all:
break
if dustbin is not None and not sent:
dustbin.send(current)
current = yield
all_targets = update_all_targets()
@consumer
def uniq(target, seen_ids=None):
"""Omit repeated tweets."""
seen_ids = set(seen_ids) if seen_ids is not None else set()
with closing(target):
while True:
tweet = yield
id_ = tweet.id
if id_ not in seen_ids:
seen_ids.add(id_)
target.send(tweet)
@consumer
def split(*targets):
"""Send the input items to each target."""
with closing(*targets):
while True:
item = yield
for target in targets:
target.send(item)
@consumer
def to_simple_queue(queue):
"""Put items to a simple queue."""
while True:
item = yield
size = queue.qsize()
if size > 10:
logger.warn('Queue size is %s.', size)
try:
queue.put(item, timeout=1)
except Full:
break
@consumer
def count(*counters, **kwargs):
"""Universal element counter.
:param counters: counters to update.
:param target: an optional target to which the cached local counts
are sent per each batch.
:param provider: a function which provides elements that have to
be counted given an input item.
"""
provider = kwargs.get('provider', None)
target = kwargs.get('target', None)
targets = [target] if target else []
if provider is None:
provider = lambda x: [x]
if not counters:
counters = Counter(),
with closing(*targets):
while True:
with lazy_counter(*counters, target=target) as c:
while True:
item = yield
c.update(provider(item))
def count_tokens(*counters, **kwargs):
"""Count tokens in a tweet."""
if kwargs.pop('distinguish_hashtags', True):
kwargs['provider'] = lambda tweet: chain(tweet.tokens,
(u'#{}'.format(h) for h in tweet.hashtags),
)
else:
kwargs['provider'] = lambda tweet: chain(tweet.tokens, tweet.hashtags)
return count(*counters, **kwargs)
def timeline(*counters, **kwargs):
"""Count tweet's creation time."""
window = kwargs.pop('window', '%Y-%m-%d-%H')
kwargs['provider'] = lambda tweet: [tweet.created_at.strftime(window)]
return count(*counters, **kwargs)
@consumer
def batch(target, flow_name=None, splitter=None):
"""Batch a stream of tweets to chunks defined by `splitter`.
:param target: a coroutine tweets are sent to.
:param flow_name: an optional flow name, which is used mainly for logging.
:param splitter: a function which decides whether a new batch has started.
:type splitter: (datetime, Tweet) --> bool
"""
batch_size = 0
if splitter is None:
def splitter(current, first):
return current != first
with closing(target):
current = first = yield
while True:
batch_size += 1
if splitter(current, first):
logger.debug('Sending a batch of %s items. (%s) '
'first: %s current: %s',
batch_size, flow_name, first, current)
target.throw(BatchEndException(current, batch_size))
batch_size = 0
first = current
result = target.send(current)
current = yield result
@contextmanager
def lazy_counter(*counters, **kwargs):
"""Delay the update of the counter."""
target = kwargs.get('target')
local_counter = Counter()
try:
yield local_counter
except BatchEndException:
pass
finally:
if target:
target.send(local_counter)
for counter in counters:
counter.update(local_counter)
@consumer
def delay(target, speedup=100.0, max_delay=2):
last = None
with closing(target):
while True:
tweet = yield
if speedup and last is not None and last < tweet.created_at:
delay = tweet.created_at - last
delay = delay.total_seconds() / speedup
delay = min([delay, max_delay])
time.sleep(delay)
last = tweet.created_at
target.send(tweet.raw)
@consumer
def mutate(target, mutator=None):
if mutator is None:
def mutator(current, first):
return current
result = None
with closing(target):
first = current = yield result
while True:
result = target.send(mutator(current, first))
current = yield result
@contextmanager
def closing(*targets, **kwargs):
"""Throw exceptions to targets and close targets on exit."""
mutable_targets = kwargs.get('mutable_targets', [])
try:
yield
finally:
for target in chain(targets, mutable_targets):
target.close()
class PropogatedException(Exception):
""""""
class BatchEndException(PropogatedException):
"""
Is thrown by batch() to the target generator on the end of the
current batch.
"""
def __init__(self, last_item, batch_size):
self.last_item = last_item
self.batch_size = batch_size
class SendNext(object):
"""
Returned a consumer if the sent value is ignored, and the next value
should be sent immediately.
"""
@consumer
def extract_retweets(target):
"""Extract retweets.
Note that it doesn't keep track of duplicates and the order.
"""
while True:
raw_tweet = yield
tweet = Tweet(raw_tweet)
retweeted_status = tweet.parsed.get('retweeted_status', None)
if retweeted_status:
target.send(retweeted_status)
target.send(raw_tweet)
@consumer
def print_media(output=None):
"""Print media items."""
if output is None:
output = sys.stdout
field_names = 'tweet_id', 'index', 'media_id', 'type', 'media_url'
writer = csv.DictWriter(output, fieldnames=field_names)
while True:
tweet = yield
media = tweet.parsed.get('extended_entities', {}).get('media', [])
for i, m in enumerate(media):
writer.writerow(
{
'tweet_id': tweet.id,
'index': i,
'media_id': m['id'],
'type': m['type'],
'media_url': m['media_url'],
}
)