-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
54 lines (43 loc) · 1.45 KB
/
sample.py
File metadata and controls
54 lines (43 loc) · 1.45 KB
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
import sys
import re
import StreamChatAgent as sca # Import this.
# callback for getting YouTube chat items
# You can implement several processes in it.
# This example prints datetime, ahthor name, message, of each item.
def get_item_cb(c):
print(f"{c.datetime} [{c.author.name}]- {c.message}")
# pre putting queue filter
# You can edit YouTube chat items before putting internal queue.
# You can avoid putting internal queue by returning None.
# This example removes items whose message consists of stamps only.
def pre_filter_cb(c):
return None if re.match(r'^(:[^:]+:)+$', c.message) else c
# post getting queue filter
# You can edit YouTube chat items after popping internal queue.
# You can avoid sending items to get_item_cb by returning None.
# This example removes stamps from message of items.
def post_filter_cb(c):
c.message = re.sub(r':[^:]+:','', c.message)
return c
# Video ID is given from command line in this example.
if len(sys.argv) <= 1:
exit(0)
# Create StreamChatAgent instance.
params = sca.params(
video_id = sys.argv[1],
get_item_cb = get_item_cb,
pre_filter_cb = pre_filter_cb,
post_filter_cb = post_filter_cb
)
agent = sca.StreamChatAgent( params )
# Start async getting YouTube chat items.
# Then get_item_cb is called continuosly.
agent.start()
# Wait any key inputted from keyboad.
input()
# Finish getting items.
# Internal thread will stop soon.
agent.disconnect()
# Wait terminating internal threads.
agent.join()
del agent