-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotbertFrost.py
98 lines (93 loc) · 2.21 KB
/
botbertFrost.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
import tweepy
CONSUMER_API='enter your own key'
CONSUMER_API_KEY='enter your own key'
SECRET_API='enter your own key'
SECRET_API_KEY='enter your own key'
def login():
#add credentials
auth = tweepy.OAuthHandler(CONSUMER_API,CONSUMER_API_KEY)
auth.set_access_token(SECRET_API,SECRET_API_KEY)
api = tweepy.API(auth)
try:
api.verify_credentials()
except:
print("Error during authentication")
# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
return api
def skipline(lines, line, i, useCase=1):
#return true if line to be skipped.
#usecase 1 - at start of tweet - skip empty lines
#usecase 2 - in middle of tweet - do not skip empty lines
if '---Title:' in line:
return True
elif not line.strip():
if useCase==1:
return True
else:
return False
elif i>0 and '---Title:' in lines[i-1]:
return True
else:
return False
def pickLines(fileName):
atStart=True
atEnd=False
tweet=''
savedLines=list()
#open poetry file
f = open(fileName,'r')
lines=f.readlines()
for i, line in enumerate(lines):
if atStart:
#Find tweet start point
if skipline(lines, line, i):
savedLines.append(line)
continue
else:
#tweet start point found
atStart=False
if not atEnd and len(tweet)<280:
#build tweet
if not skipline(lines, line, i, 2):
prevtweet=tweet
tweet+=line
savedLines.append(line)
else:
#terminate tweet
atEnd=True
if atEnd or len(tweet)>=280:
#terminating tweet
if len(tweet)>280:
#reduce tweet size
tweet=prevtweet
savedLines.pop()
break
else:
break
f.close()
#to delete lines processed for current tweet
del lines[:len(savedLines)]
#add deleted lines to the end
lines.extend(savedLines)
#restructure poetry file
f=open(fileName,'w+')
for line in lines:
f.write(line)
f.close()
return tweet
def runBot():
#login
api=login()
#pick lines
tweet = pickLines("botbertFrost.txt")
try:
#tweet
api.update_status(tweet)
except:
#notify of error
api.send_direct_message(api.get_user('thesecretholme').id_str,'Botbert is having an issue :/')
api.send_direct_message(api.get_user('quickhelpmepls').id_str,'Botbert is having an issue :/')
if __name__=="__main__":
runBot()