-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming.py
82 lines (67 loc) · 2.62 KB
/
streaming.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
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Stream
from slistener import SListener
from urllib3.exceptions import ProtocolError
# consumer (API) key authentication
consumer_key = 'YOUR CONSUMER KEY HERE'
consumer_secret = 'YOUR CONSUMER SECRET HERE'
auth = OAuthHandler(consumer_key, consumer_secret)
# access key authentication
access_token = 'YOUR ACCESS TOKEN HERE'
access_token_secret = 'YOUR ACCESS TOKEN SECRET HERE'
auth.set_access_token(access_token, access_token_secret)
#print(auth.access_token)
# set up the API with the authentication handler
api = API(auth)
# Search Terms for Elections
keywords_to_hear = [
'#2022 general elections',
'#2022 presidential elections',
'#NCIC_Kenya',
'#ElectionsBilaNoma',
'#luo', '#luos',
'#kikuyu', '#kikuyus',
'#luhya', '#luhyas',
'#kalenjin', '#kalenjins',
'#kamba', '#kambas',
'#Ruto', '#Raila',
'#Kalonzo', '#Uhuru Kenyatta',
'#Moses Kuria', '#Aden Duale',
'#Miguna', '#Babu Owino',
'#Oscar Sudi', '#Aisha Jumwa',
'#Kipchumba Murkomen', '#Johnson Sakaja',
'#Millie Odhiambo', '#Hassan Joho',
'#Ndindi Nyoro', '#Charles Njagua',
'#kieleweke', '#Tanga Tanga',
'#AzimioLaUmoja',
'#Azimio', '#Azimio La Umoja',
'#Ufisadi Daima Alliance', '#Sheikh Ngao',
'#Juma Ngao',
'#PoliceBrutalityke',
'#EngageTheIG',
'#MissingVoicesKE',
'#IG_NPS',
'#NPSC_KE',
'#NPSOfficial_KE',
'#DCI_Kenya',
'#APSKenya',
'#WakoWapi',
'#CrimeWatch254',
'#Ministry of Interior', '#ODPP_KE',
'#PrisonsKe', '#Kenya Prisons Service'
]
# instantiate the SListener object
listen = SListener(api)
# instantiate the stream object
stream = Stream(auth, listen)
#stream = Stream(consumer_key, consumer_secret, access_token, access_token_secret)
# begin collecting data
while True:
# maintain connection unless interrupted
try:
stream.filter(track=keywords_to_hear)
# reconnect automatically if error arise
# due to unstable network connection
except (ProtocolError, AttributeError):
continue