/
generate.py
59 lines (46 loc) · 1.31 KB
/
generate.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
import json
import sqlite3
import sys
import markovify
import config
import utils
class SimulatorText(markovify.Text):
def sentence_split(self, text):
return text.split('<>')
def get_recent_tweets():
pass
def generate_random_tweet():
db_connection = sqlite3.connect(config.SQLITE_DB)
db_cursor = db_connection.cursor()
db_cursor.execute('SELECT * FROM tweets ORDER BY id DESC LIMIT 150')
dataset = ''
for tweet in db_cursor.fetchall():
dataset += tweet[1]
dataset += '<>' # delimiter
db_connection.close()
model = SimulatorText(dataset, state_size=2)
tweet = model.make_short_sentence(140, tries=100)
if tweet is None:
sys.exit(0)
tweet = tweet.replace('&', '&')
tweet = tweet.replace('<', '<')
tweet = tweet.replace('>', '>')
return tweet
def send_random_tweet():
tweet = generate_random_tweet()
with open(config.RECENT_DB) as f:
recent = json.load(f)
while tweet in recent:
tweet = generate_random_tweet()
api = utils.get_api()
try:
api.update_status(status=tweet)
except:
pass
recent.append(tweet)
with open(config.RECENT_DB, 'w') as f:
json.dump(recent, f)
return tweet
if __name__ == '__main__':
t = send_random_tweet()
#print(len(t), t)