gear-orders/orders.py

154 lines
4.7 KiB
Python
Raw Normal View History

2025-11-14 04:03:20 +00:00
import logging
import datetime
from util import make_session
from generate import generate_order, generate_punishment
from db import Database
from mastodon import Mastodon
2025-11-14 04:03:20 +00:00
from telegram import Telegram
2025-11-14 04:03:20 +00:00
from settings import MASTODON_USERNAME, ORDER_TIMEOUT
from util import timezone
2025-11-14 04:03:20 +00:00
logger = logging.getLogger(__name__)
async def order_mastodon_post(session, orders_str, repeats, due_at):
post = "Here are today's orders for @%s -\n\n" % MASTODON_USERNAME
post += orders_str + "\n\n"
if repeats > 1:
post += f"These are the same orders from the last {repeats} days\n\n"
2025-11-14 04:03:21 +00:00
post += "Proof of compliance is due by " + due_at.strftime("%I:%M %p") + "\n\n"
post += "CC - @chicagogear @s10boi"
m = Mastodon(session)
return await m.statusPost(post)
async def order_telegram_post(session, orders_str, repeats, due_at, m_url):
post = "Here are your orders -\n\n"
post += orders_str + "\n\n"
if repeats > 1:
post += f"These are the same orders from the last {repeats} days\n\n"
2025-11-14 04:03:21 +00:00
post += "Proof of compliance is due by " + due_at.strftime("%I:%M %p") + "\n\n"
post += m_url
t = Telegram(session)
await t.message_send(post)
async def order_telegram_post_none(session):
t_post = "No orders for today"
t = Telegram(session)
await t.message_send(t_post)
2025-11-14 04:03:20 +00:00
async def order_issue():
2025-11-14 04:03:20 +00:00
async with make_session() as session:
orders_info = generate_order()
2025-11-14 04:03:20 +00:00
2025-11-14 04:03:20 +00:00
if 'orders' not in orders_info:
2025-11-14 04:03:20 +00:00
logger.info("No orders for today")
await order_telegram_post_none(session)
2025-11-14 04:03:20 +00:00
return
2025-11-14 04:03:20 +00:00
2025-11-14 04:03:20 +00:00
orders_str = "\n".join(orders_info['orders'])
created_at = datetime.datetime.now(tz=timezone())
due_at = created_at + ORDER_TIMEOUT
repeats_count = orders_info.get('count', 0)
m_status = await order_mastodon_post(
session,
orders_str,
repeats_count,
due_at
)
await order_telegram_post(
session,
orders_str,
repeats_count,
due_at,
m_status['url']
)
2025-11-14 04:03:20 +00:00
db = Database()
db.order_status_put(
m_status['id'],
created_at,
due_at,
orders_str
2025-11-14 04:03:20 +00:00
)
2025-11-14 04:03:20 +00:00
return due_at
async def punishment_mastodon_post(session, punishment_str, reply_id=None):
post = "@%s has failed to post proof of compliance. Here is the punishment -\n\n" % MASTODON_USERNAME
2025-11-14 04:03:21 +00:00
post += punishment_str + "\n\n"
post += "CC - @chicagogear @s10boi"
m = Mastodon(session)
return await m.statusPost(
post,
in_reply_to_id=reply_id
)
async def punishment_telegram_post(session, punishment_str, m_url):
post = "You failed to show proof of compliance. Here is your punishment -\n\n"
post += punishment_str + "\n\n"
post += m_url
t = Telegram(session)
await t.message_send(post)
async def punishment_issue(db, session, outstanding_order):
punishment = generate_punishment()
punishment_str = "\n".join(punishment)
punishment_status = await punishment_mastodon_post(
session,
punishment_str,
outstanding_order['mastodon_id'],
)
await punishment_telegram_post(
session,
punishment_str,
punishment_status['url']
)
db.punishment_status_put(
outstanding_order['id'],
punishment_status['id'],
punishment_status['created_at'],
punishment_str
)
2025-11-14 04:03:20 +00:00
async def order_check():
async with make_session() as session:
db = Database()
outstanding_orders = db.order_status_outstanding()
for outstanding_order in outstanding_orders:
m = Mastodon(session)
2025-11-14 04:03:20 +00:00
context = await m.statusContext(outstanding_order['mastodon_id'])
confirmed_at = None
for d in context['descendants']:
if (
d['in_reply_to_id'] == outstanding_order['mastodon_id'] and
d['account']['username'] == MASTODON_USERNAME and
len(d['media_attachments']) > 0
):
confirmed_at = d['created_at']
db.order_status_confirm(outstanding_order['id'], confirmed_at)
logger.info('Confirmed order %s' % (outstanding_order['id']))
2025-11-14 04:03:20 +00:00
break
if confirmed_at is None:
logger.info('Order %s remains unconfirmed' % (outstanding_order['id']))
due_at = datetime.datetime.fromisoformat(outstanding_order['due_at'])
if(due_at < datetime.datetime.now(datetime.UTC)):
logger.info('Time to issue a punishment for %s' % outstanding_order['id'])
await punishment_issue(db, session, outstanding_order)