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

Progress on the diario

This commit is contained in:
Steffo 2019-04-04 00:10:41 +02:00
parent 202e33bdf0
commit 72599e1c65
5 changed files with 37 additions and 19 deletions

View file

@ -1,7 +1,7 @@
import os import os
import asyncio import asyncio
from royalnet.bots import TelegramBot from royalnet.bots import TelegramBot
from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand, DiarioCommand
from royalnet.commands.debug_create import DebugCreateCommand from royalnet.commands.debug_create import DebugCreateCommand
from royalnet.commands.debug_author import DebugAuthorCommand from royalnet.commands.debug_author import DebugAuthorCommand
from royalnet.network import RoyalnetServer from royalnet.network import RoyalnetServer
@ -10,7 +10,7 @@ from royalnet.database.tables import Royal, Telegram
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand, commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand,
DebugAuthorCommand] DebugAuthorCommand, DiarioCommand]
master = RoyalnetServer("localhost", 1234, "sas") master = RoyalnetServer("localhost", 1234, "sas")
tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, os.environ["DB_PATH"], Royal, Telegram, "tg_id") tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, os.environ["DB_PATH"], Royal, Telegram, "tg_id")

View file

@ -5,7 +5,8 @@ from .smecds import SmecdsCommand
from .ciaoruozi import CiaoruoziCommand from .ciaoruozi import CiaoruoziCommand
from .color import ColorCommand from .color import ColorCommand
from .sync import SyncCommand from .sync import SyncCommand
from .diario import DiarioCommand
__all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", __all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand",
"SyncCommand"] "SyncCommand", "DiarioCommand"]

View file

@ -1,5 +1,5 @@
from ..utils import Command, CommandArgs, Call from ..utils import Command, CommandArgs, Call, asyncify
from ..database.tables import Royal from ..database.tables import Royal, Alias
class DebugCreateCommand(Command): class DebugCreateCommand(Command):
@ -7,10 +7,12 @@ class DebugCreateCommand(Command):
command_name = "debug_create" command_name = "debug_create"
command_title = "Create a new Royalnet user account" command_title = "Create a new Royalnet user account"
require_alchemy_tables = {Royal} require_alchemy_tables = {Royal, Alias}
async def common(self, call: Call, args: CommandArgs): async def common(self, call: Call, args: CommandArgs):
royal = call.alchemy.Royal(username=args[0], role="Member") royal = call.alchemy.Royal(username=args[0], role="Member")
call.session.add(royal) call.session.add(royal)
call.session.commit() alias = call.alchemy.Alias(royal=royal, alias=royal.username)
call.session.add(alias)
await asyncify(call.session.commit)
await call.reply(f"✅ Utente <code>{royal}</code> creato!") await call.reply(f"✅ Utente <code>{royal}</code> creato!")

View file

@ -2,6 +2,7 @@ import re
import datetime import datetime
from ..utils import Command, CommandArgs, Call, InvalidInputError from ..utils import Command, CommandArgs, Call, InvalidInputError
from ..database.tables import Royal, Diario, Alias from ..database.tables import Royal, Diario, Alias
from ..utils import asyncify
class DiarioCommand(Command): class DiarioCommand(Command):
@ -13,17 +14,35 @@ class DiarioCommand(Command):
async def common(self, call: Call, args: CommandArgs): async def common(self, call: Call, args: CommandArgs):
# Recreate the full sentence # Recreate the full sentence
text = " ".join(args) text = " ".join(args.args)
# Pass the sentence through the diario regex # Pass the sentence through the diario regex
match = re.match(r'["«‘“‛‟❛❝〝"`]([^"]+)["»’”❜❞〞"`] *(?:(?:-{1,2}|—) *(\w+))?(?:,? *([^ ].*))?', text) match = re.match(r'(!)? *["«‘“‛‟❛❝〝"`]([^"]+)["»’”❜❞〞"`] *(?:(?:-{1,2}|—) *([\w ]+))?(?:, *([^ ].*))?', text)
# Find the corresponding matches # Find the corresponding matches
if match is None: if match is None:
await call.reply(f"✅ Comando skippato per frase non valida")
raise InvalidInputError("No match found.") raise InvalidInputError("No match found.")
text = match.group(1) spoiler = bool(match.group(1))
quoted = match.group(2) text = match.group(2)
context = match.group(3) quoted = match.group(3)
context = match.group(4)
timestamp = datetime.datetime.now() timestamp = datetime.datetime.now()
# Find if there's a Royalnet account associated with the quoted name # Find if there's a Royalnet account associated with the quoted name
quoted_alias = call.session.query(call.alchemy.Alias).filter_by(alias=quoted).one_or_none() if quoted is not None:
quoted_alias = await asyncify(call.session.query(call.alchemy.Alias).filter_by(alias=quoted.lower()).one_or_none)
else:
quoted_alias = None
quoted_account = quoted_alias.royal if quoted_alias is not None else None quoted_account = quoted_alias.royal if quoted_alias is not None else None
# Find the creator of the quote # Find the creator of the quotes
creator = await call.get_author()
# Create the diario quote
diario = call.alchemy.Diario(creator=creator,
quoted_account=quoted_account,
quoted=quoted,
text=text,
context=context,
timestamp=timestamp,
media_url=None,
spoiler=spoiler)
call.session.add(diario)
await asyncify(call.session.commit)
await call.reply(f"✅ Aggiunto al diario: <code>{repr(diario)}</code>")

View file

@ -13,7 +13,6 @@ class Diario:
__tablename__ = "diario" __tablename__ = "diario"
diario_id = Column(Integer, primary_key=True) diario_id = Column(Integer, primary_key=True)
creator_id = Column(Integer, ForeignKey("royals.uid")) creator_id = Column(Integer, ForeignKey("royals.uid"))
quoted_account_id = Column(Integer, ForeignKey("royals.uid")) quoted_account_id = Column(Integer, ForeignKey("royals.uid"))
quoted = Column(String) quoted = Column(String)
@ -27,7 +26,4 @@ class Diario:
quoted_account = relationship("Royal", foreign_keys=quoted_account_id, backref="diario_quoted") quoted_account = relationship("Royal", foreign_keys=quoted_account_id, backref="diario_quoted")
def __repr__(self): def __repr__(self):
return f"<Diario {self.diario_id}>" return f"&lt;Diario diario_id={self.diario_id} creator_id={self.creator_id} quoted_account_id={self.quoted_account_id} quoted={self.quoted} text={self.text} context={self.context} timestamp={self.timestamp} media_url={self.media_url} spoiler={self.spoiler}&gt;"
def __str__(self):
return f"diario:{self.diario_id}"