-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTweetRead.py
55 lines (41 loc) · 1.58 KB
/
TweetRead.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
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
import socket
import json
# Set up your credentials
consumer_key='8PUob9bqOpIEz5bZkwB3P5yu1'
consumer_secret='CcZd08ik3BbTzfXXLATNDM1nXPadJPcLWc1e7W1PUCXsjR1lhC'
access_token ='4452590836-q0n8PcE43xOtREE57JrV99PNXtohIrjPViEYito'
access_secret='Ox58p7OHIFvCV4SkKrVjQ8p7A5wycQeMc6eOK0Kj7AsQ9'
class TweetsListener(StreamListener):
def __init__(self, csocket):
self.client_socket = csocket
def on_data(self, data):
try:
msg = json.loads( data )
print( msg['text'].encode('utf-8') )
self.client_socket.send( msg['text'].encode('utf-8') )
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print(status)
return True
def sendData(c_socket):
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitter_stream = Stream(auth, TweetsListener(c_socket))
twitter_stream.filter(track=['Covid-19'])
if __name__ == "__main__":
s = socket.socket() # Create a socket object
host = "127.0.0.1" # Get local machine name
port = 5555 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
print("Listening on port: %s" % str(port))
s.listen(5) # Now wait for client connection.
c, addr = s.accept() # Establish connection with client.
print( "Received request from: " + str( addr ) )
sendData( c )