106 lines
3.8 KiB
Python
106 lines
3.8 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 order_time
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
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")
|
|
return
|
|
|
|
orders_str = "\n".join(orders_info['orders'])
|
|
|
|
due_by = "Proof of compliance is due by %s" % (
|
|
(datetime.datetime.combine(
|
|
datetime.datetime.today(),
|
|
order_time()
|
|
) + ORDER_TIMEOUT
|
|
).strftime("%H:%M %p")
|
|
)
|
|
|
|
m_post = "Here are today's orders for @%s -\n\n" % MASTODON_USERNAME
|
|
if "count" in orders_info and orders_info['count'] > 1:
|
|
m_post += f"These are the same orders from the last {orders_info['count']} days\n\n"
|
|
m_post += orders_str + "\n\n"
|
|
m_post += due_by
|
|
|
|
m = Mastodon(session)
|
|
m_status = await m.statusPost(m_post)
|
|
|
|
t_post = "Here are your orders -\n\n"
|
|
t_post += orders_str + "\n\n"
|
|
t_post += due_by + "\n\n"
|
|
t_post += m_status['url']
|
|
|
|
t = Telegram(session)
|
|
await t.message_send(t_post)
|
|
|
|
db = Database()
|
|
db.order_status_put(
|
|
m_status['id'],
|
|
m_status['created_at'],
|
|
m_post
|
|
)
|
|
|
|
async def order_check():
|
|
async with make_session() as session:
|
|
m = Mastodon(session)
|
|
t = Telegram(session)
|
|
db = Database()
|
|
|
|
outstanding_orders = db.order_status_outstanding()
|
|
for outstanding_order in outstanding_orders:
|
|
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['created_at']))
|
|
break
|
|
|
|
if confirmed_at is None:
|
|
logger.info('Order %s remains unconfirmed' % (outstanding_order['created_at']))
|
|
|
|
oo_created_at = datetime.datetime.fromisoformat(outstanding_order['created_at'])
|
|
if(oo_created_at + ORDER_TIMEOUT < datetime.datetime.now(datetime.UTC)):
|
|
logger.info('Time to issue a punishment for %s' % outstanding_order['created_at'])
|
|
|
|
punishment = generate_punishment()
|
|
punishment_str = "\n".join(punishment)
|
|
|
|
m_post = "@%s has failed to post proof of compliance. Here is the punishment -\n\n" % MASTODON_USERNAME
|
|
m_post += punishment_str
|
|
|
|
punishment_status = await m.statusPost(
|
|
m_post,
|
|
in_reply_to_id=outstanding_order['mastodon_id']
|
|
)
|
|
|
|
t_post = "You failed to show proof of compliance. Here is your punishment -\n\n"
|
|
t_post += punishment_str + "\n\n"
|
|
t_post += punishment_status['url']
|
|
await t.message_send(t_post)
|
|
|
|
db.punishment_status_put(
|
|
outstanding_order['id'],
|
|
punishment_status['id'],
|
|
punishment_status['created_at'],
|
|
m_post
|
|
)
|