curious-greg/web.py

74 lines
2.2 KiB
Python
Raw Normal View History

2018-11-01 14:25:47 +00:00
#!/usr/bin/env python3
#Curious Greg - Curious Cat to Mastodon crossposter
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
2018-11-02 02:35:40 +00:00
import requests, sqlite3, json
2018-11-01 14:25:47 +00:00
from mastodon import Mastodon
2018-11-04 23:17:28 +00:00
from flask import Flask, render_template, request, session, redirect, url_for
2018-11-06 09:11:03 +00:00
import urllib
2018-11-01 14:25:47 +00:00
2018-11-02 02:35:40 +00:00
cfg = json.load(open("meta.json"))
2018-11-01 14:25:47 +00:00
db = sqlite3.connect("database.db")
c = db.cursor()
2018-11-06 09:11:03 +00:00
c.execute("CREATE TABLE IF NOT EXISTS `data` (secret TEXT NOT NULL, appid TEXT NOT NULL, appsecret TEXT NOT NULL, cc VARCHAR, latest_post VARCHAR)")
app = Flask(cfg['name'])
2018-11-02 03:13:08 +00:00
app.secret_key = cfg['flask_key']
2018-11-02 03:13:08 +00:00
@app.route('/')
def main():
2018-11-04 23:17:28 +00:00
if 'acct' not in session:
return render_template("landing_page.html")
else:
return redirect(url_for('home'))
@app.route('/home')
def home():
2018-11-06 09:11:03 +00:00
if 'acct' in session:
acct = session['acct']
return render_template("home.html", acct=acct)
else:
return redirect(url_for('main'))
@app.route('/internal/auth_a')
def internal_auth_a():
2018-11-04 11:36:25 +00:00
2018-11-06 09:11:03 +00:00
session['instance_url'] = request.args.get('url', default='mastodon.social', type=str)
if not session['instance_url'].startswith("https://"):
session['instance_url'] = "https://{}".format(session['instance_url'])
session['client_id'], session['client_secret'] = Mastodon.create_app(cfg['name'],
api_base_url=session['instance_url'],
scopes=["write:statuses"],
website=cfg['website'],
redirect_uris=['https://cg.lynnesbian.space/internal/auth_b']
2018-11-04 23:17:28 +00:00
)
2018-11-06 09:11:03 +00:00
params = {
"client_id": session['client_id'],
"client_secret":session['client_secret'],
"scope":"write:statuses",
"redirect_uri": "https://cg.lynnesbian.space/internal/auth_b",
"response_type":"code",
}
url = "{}/oauth/authorize?{}".format(session['instance_url'], urllib.parse.urlencode(params))
2018-11-04 23:17:28 +00:00
return url
2018-11-02 07:52:33 +00:00
2018-11-06 09:11:03 +00:00
@app.route('/internal/auth_b')
def internal_auth_b():
session['secret'] = request.args.get('code')
#write details to DB
c.execute("INSERT INTO data (secret, appid, appsecret) VALUES (?, ?, ?, ?)", (session['secret'], session['client_id'], session['client_secret']))
db.commit()
@app.route('/debug')
def print_debug_info():
return json.dumps(session._get_current_object())
2018-11-04 23:17:28 +00:00
# return(json.dumps(client_info))