forked from Tal-Ahmed/YouBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYouBotTwitter.py
More file actions
71 lines (50 loc) · 2.14 KB
/
YouBotTwitter.py
File metadata and controls
71 lines (50 loc) · 2.14 KB
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
import tweepy
from textwrap import TextWrapper
import lyricize
class YouBotStreamListener(tweepy.StreamListener):
searchParameters = []
totalText = ""
numberOfTweets = 0
_status_wrapper = TextWrapper(width=60, initial_indent=' ', subsequent_indent=' ')
def __init__(self, search_parameters):
super().__init__()
self.searchParameters = search_parameters
def on_status(self, status):
try:
text = self._status_wrapper.fill(status.text)
if not status.retweeted and 'RT @' not in text and text[0] != "@":
text = text.replace('@', '')
print(text)
self.totalText += text
self.numberOfTweets += 1
if self.numberOfTweets >= 15:
self.numberOfTweets = 0
self.print_parsed()
except:
pass
def on_error(self, status_code):
print('An error has occured! Status code = %s' % status_code)
return True
def on_timeout(self):
print('Application has timed out.')
def main(self, name):
consumer_key = "lPLCslO8xPQXstH0r0BD4BFNA"
consumer_secret = "74FUgxGLFSN0goq2lxNqoEeWX4TzXTU7R1vpPmVHfRLd6NLwVK"
access_token = "709039187069165568-rEDLr7ezG3hrHI1Qzi53LZOjt2yjULg"
access_token_secret = "Pux8Y8rHCXYXobavYdNvmIBpQjBPzfZZ1xC4cdHat2jJz"
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
self.auth = auth
self.name = name
'''timeline = tweepy.API(self.auth).user_timeline(count=300)
print("Found: %d" % (len(timeline)))
print("Removing...")
# Delete tweets one by one
for t in timeline:
tweepy.API(self.auth).destroy_status(t.id)
'''
stream = tweepy.Stream(auth, self, timeout=None)
stream.filter(track=self.searchParameters)
def print_parsed(self):
api = tweepy.API(self.auth)
api.update_status(lyricize.main(self.totalText) + self.name)