-
Notifications
You must be signed in to change notification settings - Fork 0
/
textbot.py
executable file
·60 lines (45 loc) · 1.34 KB
/
textbot.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
#!/usr/bin/env python3
import os.path
from botclient import Bot
class TextBot(Bot):
"""
Generic filereader bot.
Tweets a line at a time from a text file, and saves the last line sent in
a state file to remember where it's up to.
Just run 'textbot.py' with the standard config files, or to give it extra
functionality, import TextBot and subclass it.
The method get_next() returns the next line.
Default config variables:
textfile
lastfile
"""
def get_next(self):
last = self.read_last()
nextline = None
with open(self.cf['textfile']) as bf:
if not last:
nextline = next(bf)
else:
for line in bf:
if line == last:
nextline = next(bf)
if nextline:
self.write_last(nextline)
return nextline
def read_last(self):
if os.path.exists(self.cf['lastfile']):
with open(self.cf['lastfile']) as lf:
last = next(lf)
return last
return None
def write_last(self, lastline):
with open(self.cf['lastfile'], 'w') as lf:
lf.write(lastline)
if __name__ == '__main__':
bot = TextBot()
bot.configure()
tweet = bot.get_next()
if tweet:
bot.post(tweet)
else:
print("Error")