-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_tweets.py
47 lines (36 loc) · 1.54 KB
/
extract_tweets.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
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
from configparser import ConfigParser
class Authenticate():
def __init__(self, consumer_key, consumer_secret, access_token, access_secret):
self.auth = OAuthHandler(consumer_key, consumer_secret)
self.auth.set_access_token(access_token, access_secret)
self.api = tweepy.API(self.auth)
class TwitterListener(StreamListener):
def __init__(self, filename):
self.filename = filename
def on_data(self, data):
try:
with open(self.filename, 'a') as file:
file.write(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print(status)
return True
if __name__ == "__main__":
conf = ConfigParser()
conf.read("config/keys.ini")
consumer_key = conf.get('keys', 'consumer_key')
consumer_secret = conf.get('keys', 'consumer_secret')
access_token = conf.get('keys', 'access_token')
access_secret = conf.get('keys', 'access_secret')
authenticate = Authenticate(consumer_key, consumer_secret, access_token, access_secret)
twitter_listener = TwitterListener('euro_python.json')
twitter_stream = Stream(authenticate.auth, twitter_listener)
#Looking for the tweets to scrape, will scrape tweets with '#europython','#EuroPython'
twitter_stream.filter(track=['#europython','#EuroPython'])