import os import random from mastodon import Mastodon import sqlite3 def get_food(): foods = [ "no spoilers", ] food = foods[random.randint(0, len(foods) - 1)] return food def get_cur_and_ensure_table(): db_con = sqlite3.connect("lionseat.db") cur = db_con.cursor() cur.execute("CREATE TABLE IF NOT EXISTS used(used TEXT)") return cur def get_unique_food(cursor): max_attempts = 10 current_attempt = 1 while True and current_attempt < max_attempts: current_attempt = current_attempt + 1 my_food = get_food() cursor.execute("SELECT used FROM Used WHERE used = ?", (my_food,)) result = cursor.fetchone() if not result: return my_food def get_masto_client_and_login(): client_id = os.environ["LIONSEAT_CLIENTID"] client_secret = os.environ["LIONSEAT_CLIENTSECRET"] mastodon_instance_url = os.environ["LIONSEAT_MASTO_URL"] mastodon = Mastodon( client_id=client_id, client_secret=client_secret, api_base_url=mastodon_instance_url, ) mastodon.log_in( username=os.environ["LIONSEAT_USER"], password=os.environ["LIONSEAT_PASS"], to_file="liomseatbot.secret", scopes=["read", "write"], ) return mastodon food = get_unique_food(cursor=get_cur_and_ensure_table()) if food is not None: status = f"Lions will eat {food}" mastodon = get_masto_client_and_login() mastodon.status_post(status, visibility="public")