ask user for authentication details
This commit is contained in:
parent
5dfee7f1ff
commit
55c0733a40
2 changed files with 61 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
/__pycache__/
|
||||
/__pycache__/
|
||||
config.json
|
||||
|
|
59
main.py
59
main.py
|
@ -1 +1,60 @@
|
|||
#!/usr/bin/env python3
|
||||
from mastodon import Mastodon
|
||||
import twitter
|
||||
|
||||
import sqlite3, json
|
||||
|
||||
cfg = {
|
||||
"cw":None,
|
||||
"mark_sensitive":True
|
||||
}
|
||||
try:
|
||||
j = json.load(open("config.json"))
|
||||
for key, value in j.items():
|
||||
cfg[key] = value
|
||||
except:
|
||||
print("No config.json, using default configuration")
|
||||
|
||||
scopes = ["read:accounts", "write:statuses"]
|
||||
|
||||
if "client" not in cfg:
|
||||
print("No application info -- registering application with {}".format(cfg['site']))
|
||||
client_id, client_secret = Mastodon.create_app("Twitter Image Poster",
|
||||
api_base_url=cfg['site'],
|
||||
scopes=scopes,
|
||||
website="https://git.lynnesbian.space/lynnesbian/Twitter_Image_Poster")
|
||||
|
||||
cfg['client'] = {
|
||||
"id": client_id,
|
||||
"secret": client_secret
|
||||
}
|
||||
|
||||
if "secret" not in cfg:
|
||||
print("No user credentials -- logging in to {}".format(cfg['site']))
|
||||
client = Mastodon(client_id = cfg['client']['id'],
|
||||
client_secret = cfg['client']['secret'],
|
||||
api_base_url=cfg['site'])
|
||||
|
||||
print("Open this URL and authenticate to give Twitter Image Poster access to your bot's account: {}".format(client.auth_request_url(scopes=scopes)))
|
||||
cfg['secret'] = client.log_in(code=input("Secret: "), scopes=scopes)
|
||||
|
||||
if "twitter" not in cfg:
|
||||
print("No Twitter credentials")
|
||||
print("Please create a Twitter app by using this page: https://developer.twitter.com/en/apps/create")
|
||||
cfg['twitter'] = {
|
||||
"consumer_key": None,
|
||||
"consumer_secret": None,
|
||||
"access_token_key": None,
|
||||
"access_token_secret": None
|
||||
}
|
||||
for i in cfg['twitter'].keys():
|
||||
cfg['twitter'][i] = input("{}: ".format(i))
|
||||
|
||||
json.dump(cfg, open("config.json", "w+"))
|
||||
|
||||
client = Mastodon(
|
||||
client_id=cfg['client']['id'],
|
||||
client_secret = cfg['client']['secret'],
|
||||
access_token=cfg['secret'],
|
||||
api_base_url=cfg['site'])
|
||||
|
||||
|
|
Reference in a new issue