allow users to specify custom config.json location
This commit is contained in:
		
							parent
							
								
									32fa2302aa
								
							
						
					
					
						commit
						1fbaf17e3e
					
				
							
								
								
									
										13
									
								
								functions.py
								
								
								
								
							
							
						
						
									
										13
									
								
								functions.py
								
								
								
								
							| 
						 | 
					@ -5,11 +5,9 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import markovify
 | 
					import markovify
 | 
				
			||||||
from bs4 import BeautifulSoup
 | 
					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, cfg):
 | 
				
			||||||
 | 
					 | 
				
			||||||
def make_sentence(output):
 | 
					 | 
				
			||||||
	class nlt_fixed(markovify.NewlineText): #modified version of NewlineText that never rejects sentences
 | 
						class nlt_fixed(markovify.NewlineText): #modified version of NewlineText that never rejects sentences
 | 
				
			||||||
		def test_sentence_input(self, sentence):
 | 
							def test_sentence_input(self, sentence):
 | 
				
			||||||
			return True #all sentences are valid <3
 | 
								return True #all sentences are valid <3
 | 
				
			||||||
| 
						 | 
					@ -49,13 +47,10 @@ def make_sentence(output):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	output.send(sentence)
 | 
						output.send(sentence)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def make_toot(force_markov = False, args = None):
 | 
					def make_toot(cfg):
 | 
				
			||||||
	return make_toot_markov()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def make_toot_markov(query = None):
 | 
					 | 
				
			||||||
	toot = None
 | 
						toot = None
 | 
				
			||||||
	pin, pout = multiprocessing.Pipe(False)
 | 
						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.start()
 | 
				
			||||||
	p.join(5) #wait 5 seconds to get something
 | 
						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
 | 
						if p.is_alive(): #if it's still trying to make a toot after 5 seconds
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										6
									
								
								gen.py
								
								
								
								
							
							
						
						
									
										6
									
								
								gen.py
								
								
								
								
							| 
						 | 
					@ -8,12 +8,14 @@ import argparse, json, re
 | 
				
			||||||
import functions
 | 
					import functions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
parser = argparse.ArgumentParser(description='Generate and post a toot.')
 | 
					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',
 | 
					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.")
 | 
						help="Print the toot without actually posting it. Use this to make sure your bot's actually working.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
args = parser.parse_args()
 | 
					args = parser.parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
cfg = json.load(open('config.json'))
 | 
					cfg = json.load(open(args.cfg))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
client = None
 | 
					client = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,7 +27,7 @@ if not args.simulate:
 | 
				
			||||||
	  api_base_url=cfg['site'])
 | 
						  api_base_url=cfg['site'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
	toot = functions.make_toot()
 | 
						toot = functions.make_toot(cfg)
 | 
				
			||||||
	if cfg['strip_paired_punctuation']:
 | 
						if cfg['strip_paired_punctuation']:
 | 
				
			||||||
		toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
 | 
							toot = re.sub(r"[\[\]\(\)\{\}\"“”«»„]", "", toot)
 | 
				
			||||||
	if not args.simulate:
 | 
						if not args.simulate:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										16
									
								
								main.py
								
								
								
								
							
							
						
						
									
										16
									
								
								main.py
								
								
								
								
							| 
						 | 
					@ -7,24 +7,32 @@
 | 
				
			||||||
from mastodon import Mastodon
 | 
					from mastodon import Mastodon
 | 
				
			||||||
from os import path
 | 
					from os import path
 | 
				
			||||||
from bs4 import BeautifulSoup
 | 
					from bs4 import BeautifulSoup
 | 
				
			||||||
import os, sqlite3, signal, sys, json, re, shutil
 | 
					import os, sqlite3, signal, sys, json, re, shutil, argparse
 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
import functions
 | 
					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"]
 | 
					scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses", "read:notifications", "write:accounts"]
 | 
				
			||||||
#cfg defaults
 | 
					#cfg defaults
 | 
				
			||||||
 | 
					
 | 
				
			||||||
cfg = {
 | 
					cfg = {
 | 
				
			||||||
	"site": "https://botsin.space",
 | 
						"site": "https://botsin.space",
 | 
				
			||||||
	"cw": None,
 | 
						"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,
 | 
						"learn_from_cw": False,
 | 
				
			||||||
	"mention_handling": 1,
 | 
						"mention_handling": 1,
 | 
				
			||||||
	"max_thread_length": 15,
 | 
						"max_thread_length": 15,
 | 
				
			||||||
	"strip_paired_punctuation": False
 | 
						"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:
 | 
					if "client" not in cfg:
 | 
				
			||||||
	print("No application info -- registering application with {}".format(cfg['site']))
 | 
						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)))
 | 
						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)
 | 
						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):
 | 
					def extract_toot(toot):
 | 
				
			||||||
	toot = functions.extract_toot(toot)
 | 
						toot = functions.extract_toot(toot)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										12
									
								
								reply.py
								
								
								
								
							
							
						
						
									
										12
									
								
								reply.py
								
								
								
								
							| 
						 | 
					@ -4,11 +4,17 @@
 | 
				
			||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
					# file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import mastodon
 | 
					import mastodon
 | 
				
			||||||
import random, re, json
 | 
					import random, re, json, argparse
 | 
				
			||||||
import functions
 | 
					import functions
 | 
				
			||||||
from bs4 import BeautifulSoup
 | 
					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 = mastodon.Mastodon(
 | 
				
			||||||
  client_id=cfg['client']['id'],
 | 
					  client_id=cfg['client']['id'],
 | 
				
			||||||
| 
						 | 
					@ -67,7 +73,7 @@ class ReplyListener(mastodon.StreamListener):
 | 
				
			||||||
					else:
 | 
										else:
 | 
				
			||||||
						print("User is not valid")
 | 
											print("User is not valid")
 | 
				
			||||||
			else:
 | 
								else:
 | 
				
			||||||
				toot = functions.make_toot(True) #generate a toot
 | 
									toot = functions.make_toot(cfg) #generate a toot
 | 
				
			||||||
				toot = acct + " " + toot #prepend the @
 | 
									toot = acct + " " + toot #prepend the @
 | 
				
			||||||
				print(acct + " says " + mention) #logging
 | 
									print(acct + " says " + mention) #logging
 | 
				
			||||||
				visibility = notification['status']['visibility']
 | 
									visibility = notification['status']['visibility']
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Reference in New Issue