-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.py
More file actions
96 lines (76 loc) · 3.66 KB
/
Bot.py
File metadata and controls
96 lines (76 loc) · 3.66 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import tweepy
import time
#import os
import random
#from dotenv import load_dotenv
def retrieve(fileName):
textFile = open(fileName, 'r')
ID = int(textFile.read().strip())
textFile.close()
return ID
def store(ID, fileName):
textFile = open(fileName, 'w')
textFile.write(str(ID))
textFile.close()
return
def search():
global phrase
lastNice = retrieve(niceFileName) # Retrieve last seen tweet's ID
numberNices = retrieve(niceCountFileName) # Retrieve number of nices
nices = api.search_tweets(q = '#nice -filter:retweets', since_id = lastNice)
try:
for nice in reversed(nices): # Reverse to see first the older tweets
print('Found #nice: ' + nice.text + '\n\tID: ' + str(nice.id)
+ '\n\tUsername: ' + nice.user.screen_name + '\nResponding...')
lastNice = nice.id
# Storing now because it's safer
store(lastNice, niceFileName)
numberNices = numberNices + 1
store(numberNices, niceCountFileName)
phrase = random.randint(0, 3) # Prevent bot detection
if(phrase == 0):
api.update_status(status = '@' + nice.user.screen_name +
' Your nice hashtag was number ' + str(numberNices) +
' since 28/01/2022! Nice!', in_reply_to_status_id = nice.id)
elif(phrase == 1):
api.update_status(status = '@' + nice.user.screen_name +
' Since 28/01/2022, there have been ' + str(numberNices - 1) +
' nice hashtags. Yours was number ' + str(numberNices) + '. Nice!', in_reply_to_status_id = nice.id)
elif(phrase == 2):
api.update_status(status = '@' + nice.user.screen_name +
' With your nice hashtag, the number of nices said since 28/01/2022 has increased to ' +
str(numberNices) + '. Nice!', in_reply_to_status_id = nice.id)
elif(phrase == 3):
api.update_status(status = '@' + nice.user.screen_name +
' Before you, ' + str(numberNices - 1) + ' people tweeted a nice hashtag. Now there are ' +
str(numberNices) + ' nice hashtags in Twitter. Nice!', in_reply_to_status_id = nice.id)
elif(phrase == 4):
api.update_status(status = '@' + nice.user.screen_name +
' Thank you for being Nice! Since 28/01/2022, ' + str(numberNices - 1) + ' people tweeted a nice hashtag. Including you, that\'s ' +
str(numberNices) + ' nice hashtags!', in_reply_to_status_id = nice.id)
print('\n')
except:
numberNices = numberNices - 1
print("Error found, code = " + str(tweepy.TweepyException))
if(str(tweepy.TweepyException) == "385"): # 385 is "Deleted tweet"
print("Can continue")
else:
print("Waiting 20m...") # Maybe bot detection
time.sleep(60*19) # 19m plus the 1 in the infinite loop
niceFileName = 'NiceID.txt'
niceCountFileName = 'NiceCount.txt'
#load_dotenv(encoding = "utf8")
# You can find these keys in twitter developer
# Ye this should be in and .env but Windows sucks
APIKey = "6dEsiehLbX7kQUZvysRA169IF"
APISecret = "9HyxU5eeQnKOzlpOkRF1AC7H6XZvzAuHWWY9jBM6OIjv9u2aeN"
accessToken = "1249442524873146369-kUvmjcHsK5yJKOcnm3ikonbkgVB9gF"
accessSecret = "BorvGN4C85u0OMYDdZ4GSEQIb1O9THfOlXn3KdljDUIVL"
auth = tweepy.OAuthHandler(APIKey, APISecret)
auth.set_access_token(accessToken, accessSecret)
api = tweepy.API(auth)
while True: # Infinite loop, always responding
print('Checking for #nice...')
search()
print('Waiting 60 seconds')
time.sleep(60)