153 lines
4.7 KiB
Python
153 lines
4.7 KiB
Python
import logging
|
|
import datetime
|
|
|
|
from util import make_session
|
|
from generate import generate_order, generate_punishment
|
|
from db import Database
|
|
from mastodon import Mastodon
|
|
from telegram import Telegram
|
|
from settings import MASTODON_USERNAME, ORDER_TIMEOUT
|
|
from util import timezone
|
|
|
|
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"
|
|
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"
|
|
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)
|
|
|
|
async def order_issue():
|
|
async with make_session() as session:
|
|
orders_info = generate_order()
|
|
|
|
if 'orders' not in orders_info:
|
|
logger.info("No orders for today")
|
|
await order_telegram_post_none(session)
|
|
return
|
|
|
|
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']
|
|
)
|
|
|
|
db = Database()
|
|
db.order_status_put(
|
|
m_status['id'],
|
|
created_at,
|
|
due_at,
|
|
orders_str
|
|
)
|
|
|
|
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
|
|
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
|
|
)
|
|
|
|
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)
|
|
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']))
|
|
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)
|