From 779cc2ccba970147b205c4f23fabd57f5a20deb0 Mon Sep 17 00:00:00 2001 From: stupidcomputer Date: Sun, 10 Nov 2024 22:42:37 -0600 Subject: [PATCH] crap --- .gitignore | 3 +- tfbbridge/__init__.py | 82 +++++++++++-------------------------------- tfbbridge/groupme.py | 39 ++++++++++++++++++-- tfbbridge/schema.sql | 4 +-- 4 files changed, 62 insertions(+), 66 deletions(-) diff --git a/.gitignore b/.gitignore index 5319c96..10fdc8e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ tfbbridge.sqlite3 __pycache__/ -*.pyc \ No newline at end of file +*.pyc +tfbbridge/config.json \ No newline at end of file diff --git a/tfbbridge/__init__.py b/tfbbridge/__init__.py index 67ba970..0b8e10b 100644 --- a/tfbbridge/__init__.py +++ b/tfbbridge/__init__.py @@ -1,70 +1,26 @@ from flask import Flask, request, render_template, g, redirect import requests from .dbutils import get_and_init_db, get_db -from .groupme import group_sharing_url_to_id +from .groupme import group_sharing_url_to_id, get_oauth_auth_url, custom_get_oauth_auth import json import uuid app = Flask(__name__) app.config.from_mapping( DATABASE_LOCATION = "tfbbridge.sqlite3", - BOT_ID = "4b841d36f2a0788192b5375335" ) - -@app.route("/") -def testing(): - return "testing" - -@app.route("/add_bot", methods=["GET", "POST"]) -def add_bot(): - if request.method == "GET": - return render_template("add_new.html") - elif request.method == "POST": - try: - url = request.form["chaturl"] - try: - group_id = group_sharing_url_to_id(url) - except: - return "Bad URL", 400 - - db = get_db() - try: - db.execute( - "INSERT INTO child_groups (group_id) VALUES (?)", - (group_id,), - ) - db.commit() - except db.IntegrityError: - return "This group is already registered." - - bot_id = app.config["BOT_ID"] - print(bot_id) - - requests.post("https://api.groupme.com/v3/bots?token={}".format(bot_id)) - - return url - - except KeyError: - return "Bad URL", 400 - else: - return "Invalid Method", 400 - -@app.route("/list_child_groups") -def list_child_groups(): - db = get_db() - cursor = db.cursor() - cursor.execute('SELECT group_id FROM child_groups') - results = cursor.fetchall() - groups = [i[0] for i in results] - return groups +app.config.from_file("config.json", load=json.load) @app.route("/groupme/oauth_callback") def oauth_callback(): token = request.args.get("access_token") - print(token) - r = requests.get("https://api.groupme.com/v3/groups?token={}".format(token)) - data = r.json()["response"] + try: + print(r.text) + print(token) + data = r.json()["response"] + except KeyError: + return "Couldn't find something", 500 data = [ { "group_id": i["group_id"], @@ -78,9 +34,6 @@ def oauth_callback(): def add_group(): token = request.form["token"] target_group = request.form["to_add"] - print(target_group) - - print(token, target_group) headers = { 'Content-Type': 'application/json', @@ -100,15 +53,22 @@ def add_group(): r = requests.post('https://api.groupme.com/v3/bots', params=params, headers=headers, json=json_data) payload = r.json() - bot_id = payload["response"]["bot_id"] + return payload + bot_id = payload["response"]["bot"]["bot_id"] + group_name = payload["response"]["bot"]["group_name"] + group_id = payload["response"]["bot"]["group_id"] + + db = get_db() + try: + db.execute( + "INSERT INTO channels (group_name, group_id, bot_id, chan_type, organization)" + ) return r.text @app.route("/groupme/authorize") def authorize(): - return redirect( - "https://oauth.groupme.com/oauth/authorize?client_id=SgAPMhA6H1UuBE2QD2O5JE1BM6WnysRu6AP1ib6jQsqrH3QA", - ) + return redirect(get_oauth_auth_url()) @app.route("/groupme/create_organization", methods=["GET", "POST"]) def create_organization(): @@ -165,5 +125,5 @@ def add_chan_to_org(org): cursor.execute('SELECT id, org_name FROM organizations WHERE addition_url=?', (org,)) org_id, name = cursor.fetchone() return redirect( - "https://oauth.groupme.com/oauth/authorize?client_id=SgAPMhA6H1UuBE2QD2O5JE1BM6WnysRu6AP1ib6jQsqrH3QA&state=" + org, - ) + "https://oauth.groupme.com/oauth/authorize?client_id=qAAAc8GSUlTA8C3Ypo9CMiFQwQCQnU8zPU5KGEtz3FYHDqP5&response_type=token" + ) \ No newline at end of file diff --git a/tfbbridge/groupme.py b/tfbbridge/groupme.py index 8fdedf7..b97cdae 100644 --- a/tfbbridge/groupme.py +++ b/tfbbridge/groupme.py @@ -1,6 +1,9 @@ # misc groupme utilities -from urllib.parse import urlparse +from urllib.parse import urlparse, urlencode +from flask import current_app + +from typing import Optional def group_sharing_url_to_id(url: str) -> str: """ @@ -16,4 +19,36 @@ def group_sharing_url_to_id(url: str) -> str: splitted = parsed.path.split('/') group_id = splitted[2] - return group_id \ No newline at end of file + return group_id + +def build_oauth_auth_url(client_id: str, state: Optional[str] = None, user_params: Optional[dict] = {}): + """ + Build the OAuth redirect url to GroupMe. + state is optional -- it is usually in reference to the organization in question. + If so, state should be set to the organization's `addition_url`. (see schema.sql for info) + """ + base = "https://oauth.groupme.com/oauth/authorize?" + params = {} + params["client_id"] = client_id + if state: + params["state"] = state + + params = params | user_params + + print(base + urlencode(params)) + print(params) + return base + urlencode(params) + +def get_oauth_auth_url(state: None | str = None): + return build_oauth_auth_url( + current_app.config["CLIENT_ID"], + state + ) + +def custom_get_oauth_auth(params): + return build_oauth_auth_url( + current_app.config["CLIENT_ID"], + None, + params + ) + \ No newline at end of file diff --git a/tfbbridge/schema.sql b/tfbbridge/schema.sql index 738adca..5a3c1bf 100644 --- a/tfbbridge/schema.sql +++ b/tfbbridge/schema.sql @@ -4,8 +4,8 @@ DROP TABLE IF EXISTS parent_groups; CREATE TABLE organizations ( id INTEGER PRIMARY KEY AUTOINCREMENT, org_name TEXT NOT NULL, - admin_url TEXT UNIQUE NOT NULL, - addition_url TEXT UNIQUE NOT NULL + admin_url TEXT UNIQUE NOT NULL, /* should be private */ + addition_url TEXT UNIQUE NOT NULL /* can be public */ ); CREATE TABLE channels (