Skip to content

Instantly share code, notes, and snippets.

@jvns
Created January 19, 2023 21:45
Show Gist options
  • Save jvns/8ffa5c7e1ceabecf5d23fcc6b0d18565 to your computer and use it in GitHub Desktop.
Save jvns/8ffa5c7e1ceabecf5d23fcc6b0d18565 to your computer and use it in GitHub Desktop.
import datetime
from mastodon import Mastodon
import json
import os
import requests
def already_tooted(page, mastodon):
my_id = 109684299536552582
toots = mastodon.account_statuses(my_id, limit=10)
first_word = page["content"].split()[0]
for toot in toots:
if first_word in toot["content"]:
return True
def toot_page(page, mastodon):
image_url = page["url"]
r = requests.get(page["url"], allow_redirects=True)
path = f"{page['id']}.png"
with open(path, "wb") as f:
f.write(r.content)
media_dict = mastodon.media_post(
path, r.headers["Content-Type"], focus=(0, 1), description=page["caption"]
)
mastodon.status_post(page["content"], media_ids=[media_dict])
def get_mastodon():
client_id = os.environ["MASTO_KEY"]
client_secret = os.environ["MASTO_SECRET"]
access_token = os.environ["MASTO_TOKEN"]
return Mastodon(
client_id, client_secret, access_token, api_base_url="https://social.jvns.ca"
)
def main():
with open("pages.json") as f:
pages = json.load(f)
today = datetime.date.today().isoformat()
print(today)
if today not in pages:
print("No page for today")
return
page = pages[today]
mastodon = get_mastodon()
if already_tooted(page, mastodon):
print("Already tooted that page")
return
toot_page(page, mastodon)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment