1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Diario author parsing improvements

This commit is contained in:
Steffo 2019-03-05 10:18:22 +01:00
parent 4f7029baae
commit 9e32ecbac4

View file

@ -217,18 +217,20 @@ def cmd_balurage(bot: telegram.Bot, update: telegram.Update, session: db.Session
bot.send_message(update.message.chat.id, f"😡 Stai sfogando la tua ira sul bot!") bot.send_message(update.message.chat.id, f"😡 Stai sfogando la tua ira sul bot!")
def find_author(session: db.Session, text: str): def parse_diario(session: db.Session, text: str):
author_match = re.match(r".*(?:—|-{1,2}) ?@?([A-Za-z0-9_]+)$", text) match = re.match(r'"?(.*)"? (?:—|-{1,2}) ?@?([A-Za-z0-9_]+)$', text)
if author_match is None: if match is None:
return None return None
author_string = author_match.group(1).lower() text_string = match.group(1)
author = session.query(db.Royal).filter(db.func.lower(db.Royal.username) == author_string).first().telegram[0] author_string = match.group(2).lower()
if author is not None: royal = session.query(db.Royal).filter(db.func.lower(db.Royal.username) == author_string).first()
return author if royal is not None:
author = royal.telegram[0]
return author, text_string
author = session.query(db.Telegram).filter(db.func.lower(db.Telegram.username) == author_string).first() author = session.query(db.Telegram).filter(db.func.lower(db.Telegram.username) == author_string).first()
if author is not None: if author is not None:
return author return author, text_string
return None return None, text_string
@command @command
@ -241,7 +243,7 @@ def cmd_diario(bot: telegram.Bot, update: telegram.Update, session: db.Session):
try: try:
text = update.message.text.split(" ", 1)[1] text = update.message.text.split(" ", 1)[1]
saver = session.query(db.Telegram).filter_by(telegram_id=update.message.from_user.id).one_or_none() saver = session.query(db.Telegram).filter_by(telegram_id=update.message.from_user.id).one_or_none()
author = find_author(session, text) author, actual_text = parse_diario(session, text)
except IndexError: except IndexError:
if update.message.reply_to_message is None: if update.message.reply_to_message is None:
reply(bot, update, strings.DIARIO.ERRORS.INVALID_SYNTAX) reply(bot, update, strings.DIARIO.ERRORS.INVALID_SYNTAX)
@ -262,7 +264,7 @@ def cmd_diario(bot: telegram.Bot, update: telegram.Update, session: db.Session):
diario = db.Diario(timestamp=datetime.datetime.now(), diario = db.Diario(timestamp=datetime.datetime.now(),
saver=saver, saver=saver,
author=author, author=author,
text=text) text=actual_text)
session.add(diario) session.add(diario)
session.commit() session.commit()
reply(bot, update, strings.DIARIO.SUCCESS, ignore_escaping=True, diario=diario.to_telegram()) reply(bot, update, strings.DIARIO.SUCCESS, ignore_escaping=True, diario=diario.to_telegram())