80 lines
1.6 KiB
Python
80 lines
1.6 KiB
Python
import os
|
|
import json
|
|
import sys
|
|
import logging
|
|
import time
|
|
import traceback
|
|
|
|
import http.client as http
|
|
|
|
from urllib.parse import urlencode, urlparse
|
|
|
|
from mastodon import Mastodon
|
|
|
|
from .config import PAWSCONFIG, MASTOCONFIG, VERSION, stor_path
|
|
|
|
|
|
HEADERS = {
|
|
'User-Agent': f'PAWS/{VERSION}',
|
|
'Accept': 'application/json'
|
|
}
|
|
SCOPES = ['read:accounts']
|
|
REDIR_URI = f'https://{MASTOCONFIG["domain"]}/paws/auth'
|
|
WEBSITE = 'https://git.barkshark.xyz/izaliamae/paws'
|
|
|
|
|
|
def get_webhost(domain):
|
|
conn = http.HTTPSConnection(domain)
|
|
conn.request('GET', '/.well-known/host-meta', headers=HEADERS)
|
|
response = conn.getresponse()
|
|
|
|
if response.status == 301:
|
|
url = response.msg.get('Location')
|
|
return urlparse(url).netloc
|
|
|
|
elif response.status != 200:
|
|
return
|
|
|
|
return domain
|
|
|
|
|
|
def create_app(domain):
|
|
instance = get_webhost(domain)
|
|
|
|
try:
|
|
app_id, app_secret = Mastodon.create_app(
|
|
f'PAWS @ {MASTOCONFIG["domain"]}',
|
|
api_base_url=instance,
|
|
scopes=SCOPES,
|
|
redirect_uris=REDIR_URI,
|
|
website=WEBSITE
|
|
)
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
logging.error(f'Failed to create app: {e}')
|
|
return
|
|
|
|
client = Mastodon(
|
|
api_base_url=instance,
|
|
client_id=app_id,
|
|
client_secret=app_secret
|
|
)
|
|
|
|
return (app_id, app_secret, client.auth_request_url(redirect_uris=REDIR_URI, scopes=SCOPES))
|
|
|
|
def login(user, code):
|
|
fetch = Mastodon(
|
|
api_base_url=user['domain'],
|
|
client_id=user['appid'],
|
|
client_secret=user['appsecret']
|
|
)
|
|
|
|
token = fetch.log_in(redirect_uri=REDIR_URI, scopes=SCOPES, code=code)
|
|
|
|
client = Mastodon(api_base_url=user['domain'], access_token=token)
|
|
fetch_user = client.me()
|
|
|
|
return (token, fetch_user)
|
|
|
|
|