-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbots.py
executable file
·134 lines (117 loc) · 2.71 KB
/
bots.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json
import math
import os
import random
import tweepy
# Constants
FILE_CREDENTIALS = 'credentials.json'
FILE_STATE = 'state.json'
KEY_NUM = 'num'
KEY_BOT = 'bot'
KEY_ID = 'id'
BOT_NAMES = ['@AlicePrimes', '@BobPrimes']
def find_next_prime(act):
while True:
act += 1
is_prime = True
for i in range(2, (int)(math.sqrt(act)) + 2):
if act % i == 0:
is_prime = False
break
if is_prime:
return act
if not os.path.isfile(FILE_CREDENTIALS):
print('Credential file "%s" does not exist!!!' % FILE_CREDENTIALS)
exit(1)
with open(FILE_CREDENTIALS) as credentials_file:
credentials = json.load(credentials_file)
# I have started conversation manually
# https://twitter.com/AlicePrimes/status/899195226459426816
state = {KEY_BOT: 0, KEY_NUM: 2, KEY_ID: '899195226459426816'}
if os.path.isfile(FILE_STATE):
with open(FILE_STATE) as state_file:
state = json.load(state_file)
print("Current state: " + repr(state))
next_prime = find_next_prime(state[KEY_NUM])
print("Next prime will be: " + (str)(next_prime))
# Pick next bot
next_bot = state[KEY_BOT] ^ 1
# Authenticate
act_c = credentials[next_bot]
auth = tweepy.OAuthHandler(
act_c['consumer_key'],
act_c['consumer_secret']
)
auth.set_access_token(
act_c['access_token'],
act_c['access_token_secret']
)
api = tweepy.API(auth)
# Construct tweet
tweet = ""
tweet += random.choice([
'Not bad',
'Good try',
'Wow,',
'I didn\'t expected that',
'You were lucky'
])
tweet += " "
tweet += BOT_NAMES[state[KEY_BOT]]
tweet += ". "
tweet += random.choice([
'Try beat ',
'I am saying ',
'You cannot beat ',
'My wining number is '
])
tweet += (str)(next_prime)
tweet += " (https://prime-numbers.info/number/" + (str)(next_prime) + ")."
# Put random tags at the end
tags = [
'#math',
'#mathchat',
'#primes',
'#PrimeNumbers',
'#numberphile',
'#number',
'#school',
'#MathIsFun',
'#numbertheory',
'#TechEd',
'#STEM'
]
random.shuffle(tags)
for tag in tags:
if (len(tweet) + len(tag) > 150):
break
tweet += " " + tag
print(
"Tweeting as " +
BOT_NAMES[next_bot] +
" (" + (str)(len(tweet)) + "): " +
tweet
)
# like previous status
api.create_favorite(state[KEY_ID])
# Tweet
result = api.update_status(
status=tweet,
in_reply_to_status_id=state[KEY_ID]
)
print(
"Tweet URL: http://twitter.com/" +
BOT_NAMES[next_bot][1:] +
"/status/" +
result.id_str
)
# Update state
with open(FILE_STATE, 'w') as state_file:
json.dump(
{
KEY_BOT: next_bot,
KEY_NUM: next_prime,
KEY_ID: result.id_str
},
state_file
)