txtbooks/main.py

68 lines
2.1 KiB
Python
Raw Permalink Normal View History

2019-05-30 10:22:11 +00:00
#!/usr/bin/env python3
# Copyright (C) 2019 @LynnearSoftware@lynnesbian.fedi.space
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from mastodon import Mastodon
from os import path
import shutil, os, sqlite3, signal, sys, json
import markovify
# import re
scopes = ["write:statuses"]
cfg = {
"site": "https://botsin.space",
"cw": None
}
try:
cfg.update(json.load(open('config.json', 'r')))
2019-05-30 10:25:10 +00:00
except:
pass
2019-05-30 10:22:11 +00:00
if "client" not in cfg:
print("No application info -- registering application with {}".format(cfg['site']))
2019-05-30 10:33:29 +00:00
client_id, client_secret = Mastodon.create_app("txtbooks",
2019-05-30 10:22:11 +00:00
api_base_url=cfg['site'],
scopes=scopes,
website="https://git.lynnesbian.space/Lynnesbian/txtbooks")
cfg['client'] = {
"id": client_id,
"secret": client_secret
}
if "secret" not in cfg:
print("No user credentials -- logging in to {}".format(cfg['site']))
client = Mastodon(client_id = cfg['client']['id'],
client_secret = cfg['client']['secret'],
api_base_url=cfg['site'])
print("Open this URL and authenticate to give the script access to your bot's account: {}".format(client.auth_request_url(scopes=scopes)))
cfg['secret'] = client.log_in(code=input("Secret: "), scopes=scopes)
json.dump(cfg, open("config.json", "w+"))
print("Generating markovify data")
class TextFixed(markovify.Text):
def test_sentence_input(self, sentence):
return True #all sentences are valid <3
with open("corpus.txt") as f:
text = f.read()
model = TextFixed(text)
open("model.json", "w").write(model.to_json())