mcornick.com source
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

31 lines
1.1 KiB

import csv
import io
import re
import subprocess
import urllib.parse
import urllib.request
def toot_most_recent(section, include_text=False):
subprocess.check_output(["hugo"])
stream = io.StringIO(
subprocess.run(["hugo", "list", "all"], stdout=subprocess.PIPE).stdout.decode(
"utf-8"
)
)
posts = [
p
for p in csv.DictReader(stream)
if re.match(r"^https://mcornick.com/{}/".format(section), p["permalink"])
]
last_post = sorted(posts, key=lambda p: p["date"])[-1]
status = "{}: {}".format(last_post["title"], last_post["permalink"])
if include_text:
text = "".join(open(last_post["path"]).readlines()[7:])
status += "\n\n{}".format(text)
token = open("usercred.secret").read().strip()
url = "https://mastodon.sdf.org/api/v1/statuses"
auth = {"Authorization": "Bearer {}".format(token)}
params = urllib.parse.urlencode({"status": status}).encode("ascii")
request = urllib.request.Request(url, data=params, headers=auth, method="POST")
urllib.request.urlopen(request)