Improve logging, url prefixes

This commit is contained in:
Tanner Collin 2022-01-07 00:19:01 +00:00
parent 08d9de8784
commit b420720a2c

View File

@ -1,3 +1,9 @@
import os
import logging
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO)
import settings import settings
import asyncio import asyncio
import json import json
@ -16,7 +22,7 @@ messages = {}
async def new_message(event): async def new_message(event):
text = event.raw_text text = event.raw_text
sender = event.sender_id sender = event.sender_id
print(sender, text) logging.info('{} {}'.format(sender, text))
if sender != TANNER: if sender != TANNER:
return return
@ -30,12 +36,17 @@ async def new_message(event):
await event.reply('Message ID not found. Did the bot restart?') await event.reply('Message ID not found. Did the bot restart?')
return return
entry = '--------------------------\n\n{} - {}'.format(data['date'], data['name']) entry = '============================================================\n\n{} - {}'.format(data['date'], data['name'])
website = data['website'] website = data['website']
if website: if website:
if not website.startswith('http://') and not website.startswith('https://'): prefixes = ['http://', 'https://', 'gemini://', 'gopher://']
for prefix in prefixes:
if website.startswith(prefix):
break
else: # for loop
website = 'http://' + website website = 'http://' + website
entry += ' (<a href="{0}" target="_blank" rel="noreferrer noopener">{0}</a>)'.format(website) entry += ' (<a href="{0}" target="_blank" rel="noreferrer noopener">{0}</a>)'.format(website)
entry += '\n\n{}\n\n'.format(data['message']) entry += '\n\n{}\n\n'.format(data['message'])
@ -44,14 +55,14 @@ async def new_message(event):
f.write(entry) f.write(entry)
await event.reply('Entry added to t0.vc/g') await event.reply('Entry added to t0.vc/g')
print('Added', data) logging.info('Added: {}'.format(data))
async def submit(request): async def submit(request):
data = dict(await request.post()) data = dict(await request.post())
data['date'] = str(datetime.today().date()) data['date'] = str(datetime.today().date())
mid = str(uuid4()).split('-')[0] mid = str(uuid4()).split('-')[0]
print(mid, data) logging.info('{} {}'.format(mid, data))
try: try:
name = data['name'] name = data['name']
@ -70,9 +81,10 @@ async def submit(request):
if len(message) > 1000: if len(message) > 1000:
raise web.HTTPBadRequest(reason='Message is too long.') raise web.HTTPBadRequest(reason='Message is too long.')
report = 'Name: {}\n\nWebsite: {}\n\nMessage: {}\n\n/allow_{}' if name not in ['Devinwex']:
await bot.send_message(TANNER, message=report.format(name, website, message, mid)) report = 'Name: {}\n\nWebsite: {}\n\nMessage: {}\n\n/allow_{}'
messages[mid] = data await bot.send_message(TANNER, message=report.format(name, website, message, mid))
messages[mid] = data
with open('data/messages.log', 'a') as f: with open('data/messages.log', 'a') as f:
f.write(json.dumps(data)+'\n') f.write(json.dumps(data)+'\n')