allow users to specify custom config.json location

This commit is contained in:
Lynne 2019-08-07 13:46:57 +10:00
parent 32fa2302aa
commit 1fbaf17e3e
4 changed files with 29 additions and 18 deletions

View File

@ -5,11 +5,9 @@
import markovify
from bs4 import BeautifulSoup
import re, multiprocessing, sqlite3, shutil, os, json, html
import re, multiprocessing, sqlite3, shutil, os, html
cfg = json.load(open('config.json'))
def make_sentence(output):
def make_sentence(output, cfg):
class nlt_fixed(markovify.NewlineText): #modified version of NewlineText that never rejects sentences
def test_sentence_input(self, sentence):
return True #all sentences are valid <3
@ -49,13 +47,10 @@ def make_sentence(output):
output.send(sentence)
def make_toot(force_markov = False, args = None):
return make_toot_markov()
def make_toot_markov(query = None):
def make_toot(cfg):
toot = None
pin, pout = multiprocessing.Pipe(False)
p = multiprocessing.Process(target = make_sentence, args = [pout])
p = multiprocessing.Process(target = make_sentence, args = [pout, cfg])
p.start()
p.join(5) #wait 5 seconds to get something
if p.is_alive(): #if it's still trying to make a toot after 5 seconds

6
gen.py
View File

@ -8,12 +8,14 @@ import argparse, json, re
import functions
parser = argparse.ArgumentParser(description='Generate and post a toot.')
parser.add_argument('-c', '--cfg', dest='cfg', action='', default='config.json', nargs='?',
help="Specify a custom location for config.json.")
parser.add_argument('-s', '--simulate', dest='simulate', action='store_true',
help="Print the toot without actually posting it. Use this to make sure your bot's actually working.")
args = parser.parse_args()
cfg = json.load(open('config.json'))
cfg = json.load(open(args.cfg))
client = None
@ -25,7 +27,7 @@ if not args.simulate:
api_base_url=cfg['site'])
if __name__ == '__main__':
toot = functions.make_toot()
toot = functions.make_toot(cfg)
if cfg['strip_paired_punctuation']:
toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
if not args.simulate:

16
main.py
View File

@ -7,24 +7,32 @@
from mastodon import Mastodon
from os import path
from bs4 import BeautifulSoup
import os, sqlite3, signal, sys, json, re, shutil
import os, sqlite3, signal, sys, json, re, shutil, argparse
import requests
import functions
parser = argparse.ArgumentParser(description='Log in and download posts.')
parser.add_argument('-c', '--cfg', dest='cfg', action='', default='config.json', nargs='?',
help="Specify a custom location for config.json.")
args = parser.parse_args()
scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses", "read:notifications", "write:accounts"]
#cfg defaults
cfg = {
"site": "https://botsin.space",
"cw": None,
"instance_blacklist": ["bofa.lol", "witches.town", "knzk.me"],
"instance_blacklist": ["bofa.lol", "witches.town", "knzk.me"], # rest in piece
"learn_from_cw": False,
"mention_handling": 1,
"max_thread_length": 15,
"strip_paired_punctuation": False
}
cfg.update(json.load(open('config.json', 'r')))
cfg.update(json.load(open(args.cfg, 'r')))
print("Using {} as configuration file".format(args.cfg))
if "client" not in cfg:
print("No application info -- registering application with {}".format(cfg['site']))
@ -47,7 +55,7 @@ if "secret" not in cfg:
print("Open this URL and authenticate to give mstdn-ebooks 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+"))
json.dump(cfg, open(args.cfg, "w+"))
def extract_toot(toot):
toot = functions.extract_toot(toot)

View File

@ -4,11 +4,17 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import mastodon
import random, re, json
import random, re, json, argparse
import functions
from bs4 import BeautifulSoup
cfg = json.load(open('config.json', 'r'))
parser = argparse.ArgumentParser(description='Reply service. Leave running in the background.')
parser.add_argument('-c', '--cfg', dest='cfg', action='', default='config.json', nargs='?',
help="Specify a custom location for config.json.")
args = parser.parse_args()
cfg = json.load(open(args.cfg, 'r'))
client = mastodon.Mastodon(
client_id=cfg['client']['id'],
@ -67,7 +73,7 @@ class ReplyListener(mastodon.StreamListener):
else:
print("User is not valid")
else:
toot = functions.make_toot(True) #generate a toot
toot = functions.make_toot(cfg) #generate a toot
toot = acct + " " + toot #prepend the @
print(acct + " says " + mention) #logging
visibility = notification['status']['visibility']