From 1fbaf17e3e4cd0ac46d958118a48ee56acd54c1d Mon Sep 17 00:00:00 2001 From: Lynne Date: Wed, 7 Aug 2019 13:46:57 +1000 Subject: [PATCH] allow users to specify custom config.json location --- functions.py | 13 ++++--------- gen.py | 6 ++++-- main.py | 16 ++++++++++++---- reply.py | 12 +++++++++--- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/functions.py b/functions.py index 419c8d8..c19fd6c 100755 --- a/functions.py +++ b/functions.py @@ -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 diff --git a/gen.py b/gen.py index 8bf68a4..4a036b4 100755 --- a/gen.py +++ b/gen.py @@ -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: diff --git a/main.py b/main.py index 663aa93..3ddcb51 100755 --- a/main.py +++ b/main.py @@ -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) diff --git a/reply.py b/reply.py index 80d4e58..b32ad81 100755 --- a/reply.py +++ b/reply.py @@ -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']