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 asyncio
import json
@ -16,7 +22,7 @@ messages = {}
async def new_message(event):
text = event.raw_text
sender = event.sender_id
print(sender, text)
logging.info('{} {}'.format(sender, text))
if sender != TANNER:
return
@ -30,12 +36,17 @@ async def new_message(event):
await event.reply('Message ID not found. Did the bot restart?')
return
entry = '--------------------------\n\n{} - {}'.format(data['date'], data['name'])
entry = '============================================================\n\n{} - {}'.format(data['date'], data['name'])
website = data['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
entry += ' (<a href="{0}" target="_blank" rel="noreferrer noopener">{0}</a>)'.format(website)
entry += '\n\n{}\n\n'.format(data['message'])
@ -44,14 +55,14 @@ async def new_message(event):
f.write(entry)
await event.reply('Entry added to t0.vc/g')
print('Added', data)
logging.info('Added: {}'.format(data))
async def submit(request):
data = dict(await request.post())
data['date'] = str(datetime.today().date())
mid = str(uuid4()).split('-')[0]
print(mid, data)
logging.info('{} {}'.format(mid, data))
try:
name = data['name']
@ -70,9 +81,10 @@ async def submit(request):
if len(message) > 1000:
raise web.HTTPBadRequest(reason='Message is too long.')
report = 'Name: {}\n\nWebsite: {}\n\nMessage: {}\n\n/allow_{}'
await bot.send_message(TANNER, message=report.format(name, website, message, mid))
messages[mid] = data
if name not in ['Devinwex']:
report = 'Name: {}\n\nWebsite: {}\n\nMessage: {}\n\n/allow_{}'
await bot.send_message(TANNER, message=report.format(name, website, message, mid))
messages[mid] = data
with open('data/messages.log', 'a') as f:
f.write(json.dumps(data)+'\n')