· 6 years ago · Jul 31, 2019, 09:08 PM
1from twython import Twython, TwythonStreamer, TwythonError
2import time
3import random
4import os
5
6print("Listening for tweets...")
7
8APP_KEY = "QzwoyfUAJ4XbT7Ft5XgIvLw5j"
9APP_SECRET = "JWH4Ps7eh5oUTwjKSUnhgwZ07qtNDVk1XDEDDCYTQTSX8tQX80"
10OAUTH_TOKEN = "1151979735041368064-OfpzeQvWZmOEzVLDFWYbCo4die37aO"
11OAUTH_TOKEN_SECRET = "L9unaal8UUqOnBkNenbRaaik7r4I0klzmJa4MU2265OmF"
12
13
14def twitter_api():
15 """ Authenticate credentials"""
16
17 # Replace the placeholder below with your credentials
18 twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
19 return twitter
20
21
22def reply(data):
23 api = twitter_api()
24 tweet = data.get('text').lower()
25 tweet_text = "Have you checked out our independent reviews on the Top C60 brands? Could help you decide which C60 is best for you!!! Visit www.c60reviews.com"
26 if tweet[0] != "@" and "RT" not in tweet and tweet.count("#") <= 3:
27 try:
28 handle = data.get('user').get('screen_name')
29 tweet_id = data.get('id')
30 status = f"@{handle} {tweet_text}"
31 api.update_status(status=status, in_reply_to_status_id=tweet_id)
32 print("Tweet successfully sent!")
33 except TwythonError as e:
34 print(e)
35
36
37class MyStreamer(TwythonStreamer):
38 def on_success(self, data):
39
40 tweetText = data.get('text')
41 print(tweetText)
42 reply(data)
43
44 def on_error(self, status_code, data):
45 print("Twitter Error Status code", status_code)
46
47 # self.disconnect()
48
49
50stream = MyStreamer(APP_KEY, APP_SECRET,
51 OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
52
53hashtags = ["C60", "Carbon60", "Carbon 60"]
54stream.statuses.filter(track=hashtags)