summaryrefslogtreecommitdiff
path: root/generate.py
blob: 9e94910e38231809c9c4cc6c090d5df4094198e6 (plain)
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
import chain
import sys
import requests
import json
import os

import secrets__
import config__

dir_path = os.path.dirname(os.path.realpath(__file__))

# Import previous post log

log_path = dir_path + "/posts_log.json"

try:
    with open(log_path, "r") as infile:
        posts_log = json.loads(infile.read())
except FileNotFoundError:
    with open(log_path, "w") as outfile:
        outfile.write('[""]')
    posts_log = []

# Import Markov model

with open(dir_path + "/" + config__.model_file) as model_f:
    model = chain.Text.from_json(model_f.read())

# Generate sentence

generated = False
text = None

while not generated:
    text = model.make_short_sentence(config__.character_limit, tries=900, min_words=3)
    generated = text is not None
    if generated:
        if text in posts_log:
            generated = False

# Output

posts_log.append(text)
with open(log_path, "w") as outfile:
  outfile.write(json.dumps(posts_log[-config__.log_length:]))

if(config__.emojis == True):
    emojis = json.load(open(dir_path + "/emojis.json"))
    for emoji in emojis:
        text = text.replace(emoji, ":"+emoji+":")

text = text.replace('@','@​').replace('#','#​')
print(text)

# Post to instance

apicreate = "https://" + config__.instance_url + "/api/notes/create"

if(config__.cw == ""):
    requests.post(apicreate, json={
        'i': secrets__.TOKEN,

        'visibility': 'home',
        'noExtractMentions': True,
        'noExtractHashtags': True,

        'text': text,
    })
else:
    requests.post(apicreate, json={
        'i': secrets__.TOKEN,

        'visibility': 'home',
        'noExtractMentions': True,
        'noExtractHashtags': True,

        'text': text,
        'cw': config__.cw
    })