/
StreamController.py
87 lines (74 loc) · 2.8 KB
/
StreamController.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
import tweepy, sys, time, shutil
# Switching to NoEncodeBuilder until the encoding errors can be resolved
import NoEncodeBuilder
import FtpPush
from tweepy.streaming import StreamListener
from tweepy import Stream
from tweepy import OAuthHandler
# Basic listener the prints tweets to stdout
class StdOutListener(StreamListener):
def __init__(self, api=None):
super(StdOutListener, self).__init__()
self.num_tweets = 0
def on_data(self, data):
filter_file = open("filter.txt", "r")
filter_lines = [x.strip('\n') for x in filter_file.readlines()]
if any(x in data for x in filter_lines):
print ("Data failed Check moving to next")
else:
print (data)
#Editing to dump to text
if 'media_url' in data:
text_file = open("Output.txt", "a")
text_file.write(data)
text_file.close()
self.num_tweets = self.num_tweets + 1
if self.num_tweets < 8:
return True
else:
text_file.close()
filter_file.close()
NoEncodeBuilder.build_func()
#Disabling FTPPush by default.
#FtpPush.push_data()
#Commenting out pause to prevent timer
#print('Starting Timer')
#time.sleep(300)
#print('Sleep Complete')
#main()
return False
def on_error(self, status):
print (status)
def main():
config_file = open("Config.txt", "r")
CONSUMER_KEY = config_file.readline()
CONSUMER_KEY = CONSUMER_KEY.replace("CONSUMER_KEY:", "")
CONSUMER_KEY = CONSUMER_KEY.rstrip()
CONSUMER_SECRET = config_file.readline()
CONSUMER_SECRET = CONSUMER_SECRET.replace("CONSUMER_SECRET:","")
CONSUMER_SECRET = CONSUMER_SECRET.rstrip()
ACCESS_KEY = config_file.readline()
ACCESS_KEY = ACCESS_KEY.replace("ACCESS_KEY:", "")
ACCESS_KEY = ACCESS_KEY.rstrip()
ACCESS_SECRET = config_file.readline()
ACCESS_SECRET = ACCESS_SECRET.replace("ACCESS_SECRET:", "")
ACCESS_SECRET = ACCESS_SECRET.rstrip()
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
# Generating generic time based string for naming backup
timestr = time.strftime("%Y%m%d-%H%M%S")
# Backing up current file
text_file = open("Output.txt", "a")
text_file.close()
shutil.copy2('Output.txt', 'backup/' + timestr + '.txt')
# Clearing output file.
text_file = open("Output.txt", "w")
text_file.close()
# Handles Twitter Auth and the timeout between post
l = StdOutListener()
stream = Stream(auth, l, timeout=10)
# Filter Data here
stream.filter(track=['#happy', '#life', '#love'])
if __name__ == '__main__':
main()