tootgopher/tootgopher.py

43 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
from mastodon import Mastodon
import json
# load JSON config file
def load_config() -> dict:
with open('config.json') as f:
config_data = json.load(f)
return config_data
# create Mastodon object using stored API secrets
def gen_masto_obj(config_data: dict) -> Mastodon:
m = Mastodon(
access_token = config_data['token'],
api_base_url = config_data['instance']
)
return m
# generate phlog URLs for linking in the post
def gen_url(config_data: dict, phlog_file: str) -> dict:
base_path = f"{config_data['gopher']}/{config_data['phlog_format']}/{phlog_file}"
gopher_url = f"gopher://{base_path}"
proxy_url = f"https://{config_data['proxy']}/{base_path}"
urls = {'gopher':gopher_url, 'proxy':proxy_url}
return urls
def main():
# TODO: handle command line switches to override defaults
config_data = load_config()
m = gen_masto_obj(config_data)
phlog_file = 'test.txt'
urls = gen_url(config_data, phlog_file)
# TODO: Spawn $EDITOR, content warnings
text = "Test post, please ignore"
post = f"{text}\n\nGopher: {urls['gopher']}\nHTML Proxy: {urls['proxy']}"
m.status_post(post, visibility=config_data['visibility'])
if __name__ == '__main__':
main()