-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWaterBot_2_0.py
55 lines (45 loc) · 1.51 KB
/
WaterBot_2_0.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
# Twitter bot 2.0
# jams286
# import credentials and tweepy
from Api import Api_Key, Api_Key_Secret, Access_Token, Access_Token_Secret
import tweepy
import json
import datetime
# tweet msg
msg = 'Tweet de prueba '
# twitter authentication
auth = tweepy.OAuthHandler(Api_Key, Api_Key_Secret)
auth.set_access_token(Access_Token, Access_Token_Secret)
# api
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
def randtext():
''' enumerate tweets'''
# open conf.json
with open('WaterBot2.0/conf.json', 'r') as f:
conf = json.load(f)
# reset numeration
if len(conf['serie2']) > 4:
conf['serie2'] = '0000'
# create numeration string
num = str(int(conf['serie2'])+1).zfill(len(conf['serie2']))
text = conf['serie1'] + num
# save file
with open('WaterBot2.0/conf.json', 'w') as f:
conf['serie2'] = num
json.dump(conf, f)
return " "+text
if __name__ == '__main__':
try:
# send tweet
api.update_status(msg + randtext())
# save success on log
with open('WaterBot2.0/logs.txt', 'a') as l:
current_time = datetime.datetime.now()
now = current_time.strftime("%d-%m-%y %I:%M:%S")
l.write("\n"+now+" Success!")
except Exception as e:
# save error on log
with open('WaterBot2.0/logs.txt', 'a') as l:
current_time = datetime.datetime.now()
now = current_time.strftime("%d-%m-%y %I:%M:%S")
l.write("\n"+now+" "+str(e))