2024-11-09 23:36:26 -06:00
|
|
|
# misc groupme utilities
|
|
|
|
|
2024-11-10 22:42:37 -06:00
|
|
|
from urllib.parse import urlparse, urlencode
|
|
|
|
from flask import current_app
|
|
|
|
|
|
|
|
from typing import Optional
|
2024-11-09 23:36:26 -06:00
|
|
|
|
|
|
|
def group_sharing_url_to_id(url: str) -> str:
|
|
|
|
"""
|
|
|
|
Convert a GroupMe group sharing url to an ID.
|
|
|
|
|
|
|
|
For example, given a URL https://groupme.com/join_group/91994372/901HvAj3, the
|
|
|
|
returned ID should be 91994372.
|
|
|
|
|
|
|
|
The return type is a string because of BigNum future concerns.
|
|
|
|
"""
|
|
|
|
|
|
|
|
parsed = urlparse(url)
|
|
|
|
splitted = parsed.path.split('/')
|
|
|
|
group_id = splitted[2]
|
|
|
|
|
2024-11-10 22:42:37 -06:00
|
|
|
return group_id
|
|
|
|
|
2024-11-10 22:42:39 -06:00
|
|
|
def build_oauth_auth_url(state: Optional[str] = None, user_params: Optional[dict] = {}):
|
2024-11-10 22:42:37 -06:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
"""
|
2024-11-10 22:42:39 -06:00
|
|
|
base = current_app.config["REDIRECT_URL"]
|
2024-11-10 22:42:37 -06:00
|
|
|
params = {}
|
|
|
|
if state:
|
|
|
|
params["state"] = state
|
|
|
|
|
|
|
|
params = params | user_params
|
2024-11-10 22:42:39 -06:00
|
|
|
urlencoded = urlencode(params)
|
2024-11-10 22:42:37 -06:00
|
|
|
|
2024-11-10 22:42:39 -06:00
|
|
|
if urlencoded:
|
|
|
|
base += "&"
|
|
|
|
|
|
|
|
return base + urlencoded
|
2024-11-10 22:42:37 -06:00
|
|
|
|
|
|
|
def get_oauth_auth_url(state: None | str = None):
|
|
|
|
return build_oauth_auth_url(
|
|
|
|
state
|
|
|
|
)
|
|
|
|
|
|
|
|
def custom_get_oauth_auth(params):
|
|
|
|
return build_oauth_auth_url(
|
|
|
|
None,
|
|
|
|
params
|
|
|
|
)
|
|
|
|
|