parse card names and reply

This commit is contained in:
Holly McFarland 2021-03-09 00:08:20 -05:00
parent b754023395
commit ef9dd33318
1 changed files with 27 additions and 1 deletions

View File

@ -1,6 +1,8 @@
import os import os
import shutil import shutil
import asyncio import asyncio
import re
import json
import atoot import atoot
@ -57,7 +59,31 @@ async def listen(c, me):
log('Listening...') log('Listening...')
async with c.streaming('user') as stream: async with c.streaming('user') as stream:
async for msg in stream: async for msg in stream:
print(msg.json()) status = json.loads(msg.json()['payload'])
try:
# two events come in for the statuses, one of them has the status nested deeper
# just ignore that one
if 'status' in status: continue
except:
# ignore any events we don't know how to handle
continue
status_id = status['id']
status_author = '@' + status['account']['acct']
status_text = status['content']
status_visibility = status['visibility']
cards = re.findall(r'\[\[(.+?)\]\]', status_text)
# ignore any statuses without cards in them
if not cards: continue
reply_text = status_author + ' ' + ', '.join(cards)
reply_visibility = min(('unlisted', status_visibility), key=['direct', 'private', 'unlisted', 'public'].index)
log('Sending reply...')
await c.create_status(status=reply_text, in_reply_to_id=status_id, visibility=reply_visibility)
# https://stackoverflow.com/a/55505152/2114129 # https://stackoverflow.com/a/55505152/2114129
async def repeat(interval, func, *args, **kwargs): async def repeat(interval, func, *args, **kwargs):