mirror of
https://github.com/Lynnesbian/FediBooks/
synced 2024-11-25 16:48:58 +00:00
Compare commits
4 commits
d835604b16
...
798b5a484b
Author | SHA1 | Date | |
---|---|---|---|
798b5a484b | |||
8b26dfbfc0 | |||
f4505012ae | |||
ded8fac097 |
4 changed files with 84 additions and 11 deletions
|
@ -95,14 +95,14 @@ def make_post(args):
|
|||
print("No posts to learn from.")
|
||||
return
|
||||
|
||||
model = nlt_fixed(posts)
|
||||
tries = 0
|
||||
post = None
|
||||
|
||||
if bot['fake_mentions'] == 'never':
|
||||
# remove all mentions from the training data before the markov model sees it
|
||||
posts = re.sub(r"(?<!\S)@\w+(@[\w.]+)?\s?", "", posts)
|
||||
|
||||
model = nlt_fixed(posts)
|
||||
tries = 0
|
||||
post = None
|
||||
|
||||
# even with such a high tries value for markovify, it still sometimes returns none.
|
||||
# so we implement our own tries function as well, and try ten times.
|
||||
while post is None and tries < 10:
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from flask import session, render_template, request, redirect
|
||||
import requests
|
||||
from mastodon import Mastodon
|
||||
import re
|
||||
|
||||
def bot_accounts_add(mysql):
|
||||
def bot_accounts_add(mysql, cfg):
|
||||
if request.method == 'POST':
|
||||
if session['step'] == 1:
|
||||
if request.form['account'] == session['bot']:
|
||||
|
@ -79,6 +80,27 @@ def bot_accounts_add(mysql):
|
|||
c.execute("INSERT INTO `bot_learned_accounts` (`bot_id`, `fedi_id`) VALUES (%s, %s)", (session['bot'], request.form['account']))
|
||||
c.close()
|
||||
mysql.connection.commit()
|
||||
|
||||
if 'account' in cfg:
|
||||
client = Mastodon(
|
||||
cfg['account']['client_id'],
|
||||
cfg['account']['client_secret'],
|
||||
cfg['account']['secret'],
|
||||
"https://{}".format(cfg['account']['instance'])
|
||||
)
|
||||
status = """
|
||||
Hi, {user}. Someone has created a FediBooks bot that learns from your posts. The bot's username is {bot}. Your public posts are now being downloaded and stored for use by this bot.
|
||||
If you do not want {bot} to learn from your posts, click here: {overview}
|
||||
If you want to ensure that nobody can use {home} to create bots that use your post history, click here: {blacklist}
|
||||
""".format(
|
||||
user = request.form['account'],
|
||||
bot = session['bot'].replace("@", "@\u200B"),
|
||||
overview = "{}/overview/{}".format(cfg['base_uri'], request.form['account']),
|
||||
blacklist = "{}/blacklist".format(cfg['base_uri']),
|
||||
home = cfg['base_uri']
|
||||
)
|
||||
|
||||
client.status_post(status)
|
||||
return redirect("/bot/accounts/{}".format(session['bot']), 303)
|
||||
else:
|
||||
error = "Couldn't access ActivityPub outbox. {} may require authenticated fetches, which FediBooks doesn't support yet.".format(instance)
|
||||
|
|
51
app/setup.py
Executable file
51
app/setup.py
Executable file
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from mastodon import Mastodon
|
||||
import json
|
||||
|
||||
cfg = json.load(open("config.json"))
|
||||
|
||||
scopes = ["write:statuses"]
|
||||
|
||||
print("FediBooks needs access to an account to notify users when they've been added to bots.")
|
||||
print("What instance would you like FediBooks' account to be on?")
|
||||
instance = input("https://")
|
||||
client_id, client_secret = Mastodon.create_app(
|
||||
"FediBooks",
|
||||
api_base_url="https://{}".format(instance),
|
||||
scopes=scopes,
|
||||
website=cfg['base_uri']
|
||||
)
|
||||
|
||||
client = Mastodon(
|
||||
client_id=client_id,
|
||||
client_secret=client_secret,
|
||||
api_base_url="https://{}".format(instance)
|
||||
)
|
||||
|
||||
url = client.auth_request_url(
|
||||
client_id=client_id,
|
||||
scopes=scopes
|
||||
)
|
||||
print("Create an account on {}, then click this link to give FediBooks access to the account: {}".format(instance, url))
|
||||
print("Authorise FediBooks to access the account, then paste the code below.")
|
||||
code = input("Code: ")
|
||||
|
||||
print("Authenticating...")
|
||||
|
||||
secret = client.log_in(
|
||||
code = code,
|
||||
scopes=scopes
|
||||
)
|
||||
client.status_post("FediBooks has successfully been set up to use this account.")
|
||||
|
||||
cfg['account'] = {
|
||||
'client_id': client_id,
|
||||
'client_secret': client_secret,
|
||||
'secret': secret,
|
||||
'instance': instance
|
||||
}
|
||||
|
||||
json.dump(cfg, open('config.json', 'w'))
|
||||
|
||||
print("Done! Thanks for using FediBooks!")
|
12
app/webui.py
12
app/webui.py
|
@ -127,7 +127,7 @@ def bot_accounts(id):
|
|||
|
||||
@app.route("/bot/accounts/add", methods = ['GET', 'POST'])
|
||||
def render_bot_accounts_add():
|
||||
return bot_accounts_add(mysql)
|
||||
return bot_accounts_add(mysql, cfg)
|
||||
|
||||
@app.route("/bot/accounts/toggle/<id>")
|
||||
def bot_accounts_toggle(id):
|
||||
|
@ -163,13 +163,13 @@ def render_bot_create():
|
|||
@app.route("/bot/create/back")
|
||||
def bot_create_back():
|
||||
session['step'] -= 1
|
||||
return redirect(url_for("bot_create"), 303)
|
||||
return redirect(url_for("render_bot_create"), 303)
|
||||
|
||||
@app.route("/do/authenticate_bot")
|
||||
def do_authenticate_bot():
|
||||
session['code'] = request.args.get('code')
|
||||
session['step'] = 4
|
||||
return redirect(url_for("bot_create"), 303)
|
||||
return redirect(url_for("render_bot_create"), 303)
|
||||
|
||||
@app.route("/push/<id>", methods = ['POST'])
|
||||
def push(id):
|
||||
|
@ -246,12 +246,12 @@ def do_signup():
|
|||
|
||||
# success!
|
||||
session['user_id'] = user_id
|
||||
return redirect(url_for('home'))
|
||||
return redirect(url_for('render_home'))
|
||||
|
||||
@app.route("/do/signout")
|
||||
def do_signout():
|
||||
session.clear()
|
||||
return redirect(url_for("home"))
|
||||
return redirect(url_for("render_home"))
|
||||
|
||||
@app.route("/do/login", methods=['POST'])
|
||||
def do_login():
|
||||
|
@ -266,7 +266,7 @@ def do_login():
|
|||
|
||||
if bcrypt.checkpw(pw_hashed, data['password']):
|
||||
session['user_id'] = data['id']
|
||||
return redirect(url_for("home"))
|
||||
return redirect(url_for("render_home"))
|
||||
|
||||
else:
|
||||
session['error'] = "Incorrect login information."
|
||||
|
|
Loading…
Reference in a new issue