-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
128 lines (108 loc) · 3.17 KB
/
main.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
# -*- coding: utf-8 -*-
from twython import Twython, TwythonError
import sys
import requests
import time
from config import CONFIG
class Bot:
def __init__(self):
# -- initialize twython
self.twitter = Twython(
CONFIG["TWITTER_CONSUMER_KEY"],
CONFIG["TWITTER_CONSUMER_SECRET"],
CONFIG["TWITTER_ACCESS_TOKEN"],
CONFIG["TWITTER_ACCESS_TOKEN_SECRET"],
)
self.twitter_screen_name = self.twitter.verify_credentials()["screen_name"]
# -- initialize ODIN
response = requests.post("https://odin.deadtired.me/api/auth", json={
"username": CONFIG["ODIN_USER"],
"password": CONFIG["ODIN_PASS"],
})
try:
data = response.json()
self._odin_token = data["token"]
except:
print("ERROR:")
print(response.text)
sys.exit(1)
def fetch_generated_text(self):
response = requests.post(f"https://odin.deadtired.me/api/models/{CONFIG['ODIN_MODEL']}",
json={
"include_prefix": False,
"length": 183,
"temperature": 1.0,
"top_k": 40,
"truncate": "<|endoftext|>",
},
headers={
"X-API-KEY": self._odin_token,
},
)
try:
data = response.json()
content = data["data"][0]
except:
print("ERROR GENERATING TEXT:")
print(response.text)
sys.exit(1)
return content
def generate_post(self):
print("Generating post... ", end=""); sys.stdout.flush()
response = self.fetch_generated_text()
# split all generated lines by newline into array, discarding last leftovers after last newline
response_split = response.split("\n")
if len(response_split) > 1:
response_split = response_split[:-1]
# clean out empty lines and split too long lines into two
lines = []
for line in response_split:
if not line or line.isspace(): continue
if len(line) > 280:
buffer = ""
while line:
if len(buffer) >= 240:
lines.append(buffer)
buffer = ""
split = line.split(" ", 1)
if len(split) == 1:
word, line = split[0], ""
else:
word, line = line.split(" ", 1)
buffer += word + " "
if buffer: lines.append(buffer)
else:
lines.append(line)
# generation failure, try again
if not lines: return False
# save generated poem to master file to read through later
with open("generated-poems.txt", 'a') as f:
poem = "\n".join(lines)
print(poem)
f.write(poem+"\n---------------\n")
# separate poem into 280 or less character tweets
tweets = []
buffer = ""
for line_num, line in enumerate(lines):
if len(buffer) + len(line) + 1 < 280:
buffer += "\n" + line
else:
tweets.append(buffer)
buffer = "@{} ".format(self.twitter_screen_name) + line
# if we have 8 lines so far and we have a line that ends with punctuation,
# terminate tweet here
if line_num >= 8 and line[-1] in [".", "!", "?"]: break
if buffer: tweets.append(buffer)
# post each tweet and chain into a thread
tweet = tweets.pop(0)
parent_tweet = self.twitter.update_status(status=tweet)
for tweet in tweets:
parent_tweet = self.twitter.update_status(status=tweet, in_reply_to_status_id=parent_tweet["id"])
print("Generated and posted.")
return True
def main():
client = Bot()
while not client.generate_post():
time.sleep(1)
if __name__ == "__main__":
main()