-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApiIntegration.py
70 lines (49 loc) · 1.86 KB
/
ApiIntegration.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
#Tweepy library imports
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import subprocess
#Json parsing imports
import json
#Credentials to access Twitter API
access_token = "897495635682893826-BewCW8BZtcWbn0v3FIrJSAwIisXqizw"
access_token_secret = "aggABxF67fsBSDJDkhzpBbYiUF15M0fQuPU1Xy5XqK8xT"
consumer_key = "McoZRCrbLbONTKGVHR65qLEmF"
consumer_secret = "ffNHti5vHkkzu5sehaGkpB4Pvntds8tKYZFMw1pQTXcqFCR05f"
restaurantName = input("Name of Restaurant or buisiness")
with open ('TwitterOutput.txt', 'w') as f:
f.close()
with open ('TwitterFormatted.txt', 'w') as f:
f.close()
#Override tweepy.StreamListner, print statuses to stdout
class StdOutListener(StreamListener):
#init
def __init__(self, api=None):
super(StdOutListener, self).__init__()
self.num_tweets = 0
def on_data(self, data):
all_data = json.loads(data)
tweet = all_data["text"]
tweet = tweet.replace('\n', '')
with open ('TwitterFormatted.txt', 'a') as f1:
try:
f1.write(str(tweet))
f1.write(';')
except IOError:
pass
f1.close
self.num_tweets += 1
if self.num_tweets < 5:
return True
else:
return False
def on_error(self, status):
print(status)
if __name__ == '__main__':
#Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=[restaurantName])
subprocess.call(['java', '-jar', 'readinput.jar'])