-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackwardsBot.py
85 lines (64 loc) · 2.46 KB
/
BackwardsBot.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
# Import Library to Twitter API
import tweepy
# Import Twitter API Tokens from secret.py
from secret import *
# Change which user you want to bombard with replies (without @)
UserToTrack = "pewdiepie"
# Connecting to Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
auth.set_access_token(access_key, access_secret)
# Twitter Stream Class
class UserTracker(tweepy.StreamListener):
# Initializing Class
def __init__(self, api):
self.api = api
self.me = api.me()
# Tweet met the criteria
def on_status(self, tweet):
print("Tweet found")
# Checking if Tweet is a reply
if tweet.in_reply_to_status_id == None:
tweet_handler(tweet)
else:
print("Tweet is a reply")
print("\n")
# Error Handling
def on_error(self, status):
print("Error detected: " + str(status))
# Function to reply to Original Tweet
def tweet_handler(tweet):
# Instantiate Backwards Tweet and Original Tweet
backwardsTweet = ""
original = tweet.text
# Replace characters that went wrong
original = original.replace("&", "&")
original = original.replace(">", ">")
original = original.replace("<", "<")
# Split Tweet on newline
tweetList = original.split("\n")
# Split newlines on spaces
for i in range(len(tweetList)):
tweetList[i] = tweetList[i].split(" ")
# Shuffle the letters and put back spaces and newlines
for i in range(len(tweetList)):
for j in range(len(tweetList[i])):
# Check if word contains a #, mention or URL
if "http" in tweetList[i][j] or "#" in tweetList[i][j] or "@" in tweetList[i][j]:
for k in range(len(tweetList[i][j])):
backwardsTweet += tweetList[i][j][k]
else:
for k in range(len(tweetList[i][j])):
backwardsTweet += tweetList[i][j][-1 - k]
backwardsTweet += " "
backwardsTweet += "\n"
# Reply with Backwards Tweet
api.update_status(backwardsTweet, in_reply_to_status_id=tweet.id,
auto_populate_reply_metadata=True)
print("Reply Sent")
print("\n")
# Instantiating Twitter Stream
twitterStream = tweepy.Stream(api.auth, UserTracker(api))
# Waiting for Tweet meeting the criteria
user = api.get_user(UserToTrack)
twitterStream.filter(follow=[str(user.id)])