From 094a961b5153cd0877e4d5a8eb1e953d4c9919b3 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 9 Apr 2019 10:39:19 +0200 Subject: [PATCH] Create dateparser command --- requirements.txt | 1 + royalgames.py | 5 +++-- .../commands/{debug_author.py => author.py} | 4 ++-- royalnet/commands/dateparser.py | 21 +++++++++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) rename royalnet/commands/{debug_author.py => author.py} (88%) create mode 100644 royalnet/commands/dateparser.py diff --git a/requirements.txt b/requirements.txt index c5203d50..7e107c29 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ pytest>=4.3.1 psycopg2-binary>=2.8 aiohttp>=3.5.4 Markdown>=3.1 +dateparser>=0.7.1 diff --git a/royalgames.py b/royalgames.py index 8589584e..b94ff602 100644 --- a/royalgames.py +++ b/royalgames.py @@ -3,7 +3,8 @@ import asyncio from royalnet.bots import TelegramBot from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand, DiarioCommand, RageCommand from royalnet.commands.debug_create import DebugCreateCommand -from royalnet.commands.debug_author import DebugAuthorCommand +from royalnet.commands.author import AuthorCommand +from royalnet.commands.dateparser import DateparserCommand from royalnet.commands.error_handler import ErrorHandlerCommand from royalnet.network import RoyalnetServer from royalnet.database.tables import Royal, Telegram @@ -11,7 +12,7 @@ from royalnet.database.tables import Royal, Telegram loop = asyncio.get_event_loop() commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, - DebugAuthorCommand, DiarioCommand, RageCommand] + AuthorCommand, DiarioCommand, RageCommand, DateparserCommand] master = RoyalnetServer("localhost", 1234, "sas") tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, os.environ["DB_PATH"], Royal, Telegram, "tg_id", error_command=ErrorHandlerCommand) diff --git a/royalnet/commands/debug_author.py b/royalnet/commands/author.py similarity index 88% rename from royalnet/commands/debug_author.py rename to royalnet/commands/author.py index 145b184b..188ec55f 100644 --- a/royalnet/commands/debug_author.py +++ b/royalnet/commands/author.py @@ -2,9 +2,9 @@ from ..utils import Command, Call from ..database.tables import Royal, Telegram -class DebugAuthorCommand(Command): +class AuthorCommand(Command): - command_name = "debug_author" + command_name = "author" command_description = "Ottieni informazioni sull'autore di questa chiamata." command_syntax = "" diff --git a/royalnet/commands/dateparser.py b/royalnet/commands/dateparser.py new file mode 100644 index 00000000..c2dc401f --- /dev/null +++ b/royalnet/commands/dateparser.py @@ -0,0 +1,21 @@ +import datetime +import dateparser +from ..utils import Command, Call, InvalidInputError + + +class DateparserCommand(Command): + + command_name = "dateparser" + command_description = "Legge e comprende la data inserita." + command_syntax = "(data)" + + @classmethod + async def common(cls, call: Call): + if len(call.args) == 0: + raise InvalidInputError("Missing arg") + text = " ".join(call.args) + date: datetime.datetime = dateparser.parse(text) + if date is None: + await call.reply("๐Ÿ•• La data inserita non รจ valida.") + return + await call.reply(f"๐Ÿ• La data inserita รจ {date.isoformat()}")