tootgopher/tootgopher.py

106 lines
3.1 KiB
Python
Raw Permalink Normal View History

2020-10-14 23:43:37 +00:00
#!/usr/bin/env python3
from mastodon import Mastodon
import json
import os
import tempfile
from subprocess import call
from xdg import BaseDirectory
2020-10-14 23:13:02 +00:00
2020-10-14 23:43:37 +00:00
# load JSON config file
def load_config() -> dict:
with open(f"{BaseDirectory.xdg_config_home}/tootgopher/config.json") as f:
2020-10-14 23:43:37 +00:00
config_data = json.load(f)
return config_data
# create Mastodon object using stored API secrets
2020-10-15 00:59:04 +00:00
# TODO: load config from $XDG_CONFIG_HOME
2020-10-14 23:43:37 +00:00
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:
2020-10-22 19:48:37 +00:00
base_path = ''
if phlog_file == '':
base_path = f"{config_data['gopher']}"
else:
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
# spawn $EDITOR or nano by default to edit the post text
def edit_post(post: str) -> str:
2020-10-22 20:14:57 +00:00
# Fetch OS editor
EDITOR = os.environ.get('EDITOR', 'nano')
2020-10-22 20:14:57 +00:00
# Temporary File
tf_file = tempfile.NamedTemporaryFile(suffix = '.tmp', delete = False)
2020-10-22 20:14:57 +00:00
# Paste in the sample post with links generated
with open(tf_file.name, 'w') as tf:
tf.write(post)
2020-10-22 20:14:57 +00:00
# Spawn subprocess on a temporary file
call([EDITOR, tf_file.name])
2020-10-22 20:14:57 +00:00
# Read the file for the final post
with open(tf_file.name, 'r') as tf:
post = tf.read()
tf.close()
2020-10-22 20:14:57 +00:00
# Delete the temp file
os.unlink(tf_file.name)
return post
2020-10-15 00:59:04 +00:00
# display post contents and verify with the user
2020-10-22 20:26:45 +00:00
def post_check(post: str, cw: str) -> bool:
2020-10-15 00:59:04 +00:00
print('You are about to post')
2020-10-22 20:26:45 +00:00
# If content warning is given
if (cw != ''):
print()
print(f"CW: \"{cw}\"")
2020-10-15 00:59:04 +00:00
print()
print(post)
print()
2020-10-22 19:48:37 +00:00
will_continue = input('Continue? [y/N] ')
2020-10-15 00:59:04 +00:00
print()
return will_continue.upper() in ['Y', 'YES']
2020-10-14 23:43:37 +00:00
def main():
2020-10-15 00:59:04 +00:00
# load config.json
# TODO: handle command line switches to override defaults
2020-10-14 23:43:37 +00:00
config_data = load_config()
m = gen_masto_obj(config_data)
2020-10-15 00:59:04 +00:00
# generate URLs
2020-10-22 19:48:37 +00:00
phlog_file = input('Post location (leave blank for root): ')
urls = gen_url(config_data, phlog_file)
2020-10-22 20:26:45 +00:00
# Read content warning
cw = input('Content Warning? (Leave blank for none): ')
# Edit post
post = f"<Replace with your post description>\n\nGopher: {urls['gopher']}\nHTML Proxy: {urls['proxy']}"
post = edit_post(post)
2020-10-22 20:26:45 +00:00
print()
# Verify the post with the user and post it
if post_check(post, cw):
# Check if content warning is given
if (cw == ''):
toot_dict = m.status_post(post, visibility = config_data['visibility'])
else:
toot_dict = m.status_post(post, visibility = config_data['visibility'], spoiler_text = cw)
2020-10-15 00:59:04 +00:00
print(f"Posted at {toot_dict['url']}")
else:
print('Aborted.')
2020-10-14 23:43:37 +00:00
if __name__ == '__main__':
main()