forked from CruiseDevice/twweet-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Listener.py
92 lines (65 loc) · 2.69 KB
/
Listener.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
88
89
90
91
92
import tweepy
from Twweeter import Twweeter
'''STREAM'''
class StreamListener(tweepy.StreamListener):
# Decided I would keep all the overridable functions from the BaseClass so we know what we have to play with.
def __init__(self, twweeterObj, time_limit=60):
self.twweeterObj = twweeterObj
super(StreamListener, self).__init__(self.twweeterObj.api)
def on_status(self, status):
print('@{} => {}'.format(status.user.screen_name, status.text.replace("\n", " ")))
def on_error(self, status_code):
print('AN ERROR: {}'.format(status_code))
# read the docs and handle different errors
def keep_alive(self):
"""Called when a keep-alive arrived"""
return
def on_exception(self, exception):
"""Called when an unhandled exception occurs."""
return
def on_delete(self, status_id, user_id):
"""Called when a delete notice arrives for a status"""
return
def on_event(self, status):
"""Called when a new event arrives"""
return
def on_direct_message(self, status):
"""Called when a new direct message arrives"""
return
def on_friends(self, friends):
"""Called when a friends list arrives.
friends is a list that contains user_id
"""
return
def on_limit(self, track):
"""Called when a limitation notice arrives"""
return
def on_timeout(self):
"""Called when stream connection times out"""
return
def on_disconnect(self, notice):
"""Called when twitter sends a disconnect notice
Disconnect codes are listed here:
https://dev.twitter.com/docs/streaming-apis/messages#Disconnect_messages_disconnect
"""
return
def on_warning(self, notice):
"""Called when a disconnection warning message arrives"""
return
class Listener():
def __init__(self, twweeterObj):
self.twweeterObj = twweeterObj
self.streamListenerOB = StreamListener(self.twweeterObj)
# authorise the stream listener
def authStreamer(self):
stream = tweepy.Stream(auth=self.twweeterObj.api.auth, listener=self.streamListenerOB)
return stream
#Listen for tweets on the current users timeline
def streamYourTL(self):
stream = self.authStreamer()
stream.userstream(_with='following', async=True)
#listen for tweets containing a specific word or hashtag (a phrase might work too)
def streamWordOrHashtag(self, wordsList):
stream = self.authStreamer()
stream.filter(track=wordsList, async=True)
'''END STREAM'''