From 1d74ef776081eceaf2bbffae62029b73d58bcd38 Mon Sep 17 00:00:00 2001 From: ado Date: Fri, 14 Jun 2024 10:17:16 -0400 Subject: [PATCH] Example of lionseat --- lionseat.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lionseat.py diff --git a/lionseat.py b/lionseat.py new file mode 100644 index 0000000..9db927e --- /dev/null +++ b/lionseat.py @@ -0,0 +1,57 @@ +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")