-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaming.py
45 lines (39 loc) · 1.37 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
# import packages
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Stream
from slistener import SListener
from urllib3.exceptions import ProtocolError
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database
from keys_secret import consumer_key, consumer_secret
from keys_secret import access_token, access_token_secret
# consumer key authentication
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# set up the API with the authentication handler
api = API(auth)
# set up words to hear
keywords_to_hear = ['aadhaar',
'aadhar',
'adhaar',
]
# instantiate the SListener object
listen = SListener(api)
# instantiate the stream object
stream = Stream(auth, listen)
# # create a engine to the database
#engine = create_engine("sqlite:///tweets.sqlite")
# # if the database does not exist
#if not database_exists(engine.url):
# # create a new database
# create_database(engine.url)
# begin collecting data
while True:
# maintain connection unless interrupted
try:
stream.filter(track=keywords_to_hear)
# reconnect automantically if error arise
# due to unstable network connection
except (ProtocolError, AttributeError):
continue