moved login to separate file

This commit is contained in:
Lynne Megido 2018-10-27 23:17:50 +10:00
parent 9f0150ad3a
commit 449f245fe4
Signed by: lynnesbian
GPG Key ID: FB7B970303ACE499
4 changed files with 41 additions and 24 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
config.json
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/

3
config.json.sample Normal file
View File

@ -0,0 +1,3 @@
{
"site":"fedi.lynnesbian.space"
}

25
login.py Normal file
View File

@ -0,0 +1,25 @@
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://git.lynnesbian.space/lynnesbian/lynnegle-assistant")
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+"))

35
main.py
View File

@ -1,33 +1,20 @@
#!/usr/bin/env python3
# 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/.
import requests
from mastodon import Mastodon
import json
scopes = ["read:statuses", "read:accounts", "read:follows", "write:statuses"]
cfg = json.load(open('config.json', 'r'))
client = Mastodon(
client_id=cfg['client']['id'],
client_secret = cfg['client']['secret'],
access_token=cfg['secret'],
api_base_url=cfg['site'])
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':