lynnegle-assistant/main.py

46 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
import requests
from mastodon import Mastodon
import json
scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses"]
cfg = json.load(open('config.json', 'r'))
if "client" not in cfg:
print("No client credentials, registering application")
client_id, client_secret = Mastodon.create_app("lynnegle-assistant",
api_base_url=cfg['site'],
scopes=scopes,
website="https://github.com/Lynnesbian/mstdn-ebooks")
cfg['client'] = {
"id": client_id,
"secret": client_secret
}
if "secret" not in cfg:
print("No user credentials, logging in")
client = Mastodon(client_id = cfg['client']['id'],
client_secret = cfg['client']['secret'],
api_base_url=cfg['site'])
print("Open this URL: {}".format(client.auth_request_url(scopes=scopes)))
cfg['secret'] = client.log_in(code=input("Secret: "), scopes=scopes)
json.dump(cfg, open("config.json", "w+"))
kind = 'general'
if kind == 'images':
r = requests.get("https://searx.lynnesbian.space/?category_images=1&q={}&format=json".format(q))
else:
r = requests.get("https://searx.lynnesbian.space/?q={}&format=json".format(q))
j = r.json()
if kind == 'images':
text = "Here's what I found by searching for images with the query \"{}\".".format(q)
else:
result = j['results'][0]
text = result['title'] + "\n" + result['url'] + "\n" + result['content'] + "\n" + "(Score: {})".format(result['score'])
print(text)