From 483591f63325dcfbae17c534b897441dd562c416 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 8 Aug 2018 21:59:47 +0200 Subject: [PATCH] many things --- .gitignore | 1 + db.py | 3 ++- requirements.txt | 3 ++- telegrambot.py | 45 +++++++++++++++++++++++++++++++++++++----- templates/profile.html | 2 +- webserver.py | 6 ++++-- 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 8fbae830..491bf533 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ libopus-0.dll music.opus opusfiles/ ignored/* +markovmodel.json \ No newline at end of file diff --git a/db.py b/db.py index 15b226bc..bf09215a 100644 --- a/db.py +++ b/db.py @@ -891,7 +891,8 @@ class GameOrigins(enum.Enum): class LibraryGame(Base): __tablename__ = "librarygames" - owner_id = Column(Integer, ForeignKey("royals_id"), nullable=False) + id = Column(BigInteger, primary_key=True) + owner_id = Column(Integer, ForeignKey("royals.id"), nullable=False) owner = relationship("Royal", lazy="joined") name = Column(String) platform = Column(String) diff --git a/requirements.txt b/requirements.txt index 5165a6db..7cfb0df8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,5 @@ async_timeout raven bcrypt markdown2 -pygments \ No newline at end of file +pygments +markovify \ No newline at end of file diff --git a/telegrambot.py b/telegrambot.py index 575dca9d..da559992 100644 --- a/telegrambot.py +++ b/telegrambot.py @@ -1,6 +1,5 @@ import datetime import random -import math import typing import db import errors @@ -14,9 +13,20 @@ import os import time import cast import re +import logging +import configparser +import markovify + +# Markov model +try: + with open("markovmodel.json") as file: + model = markovify.Text.from_json(file.read()) +except Exception: + model = None + +logging.getLogger().setLevel(level=20) # Init the config reader -import configparser config = configparser.ConfigParser() config.read("config.ini") @@ -561,8 +571,30 @@ def cmd_calendar(bot: Bot, update: Update): bot.send_message(update.message.chat.id, msg, parse_mode="HTML", disable_web_page_preview=True) +def cmd_markov(bot: Bot, update: Update): + if model is None: + bot.send_message(update.message.chat.id, "⚠️ Il modello Markov non è disponibile.") + return + try: + _, first_word = update.message.text.split(" ", 1) + except IndexError: + sentence = model.make_sentence(tries=1000) + if sentence is None: + bot.send_message(update.message.chat.id, "⚠ Complimenti! Hai vinto la lotteria di Markov!\n" + "O forse l'hai persa.\n" + "Non sono riuscito a generare una frase, riprova.") + return + bot.send_message(update.message.chat.id, sentence) + else: + sentence = model.make_sentence_with_start(first_word, tries=1000) + if sentence is None: + bot.send_message(update.message.chat.id, "⚠ Non è stato possibile generare frasi partendo da questa" + " parola.") + return + bot.send_message(update.message.chat.id, sentence) + + def process(arg_discord_connection): - print("Telegrambot starting...") if arg_discord_connection is not None: global discord_connection discord_connection = arg_discord_connection @@ -586,7 +618,9 @@ def process(arg_discord_connection): u.dispatcher.add_handler(CommandHandler("wheel", cmd_wheel)) u.dispatcher.add_handler(CommandHandler("newevent", cmd_newevent)) u.dispatcher.add_handler(CommandHandler("calendar", cmd_calendar)) + u.dispatcher.add_handler(CommandHandler("markov", cmd_markov)) u.dispatcher.add_handler(CallbackQueryHandler(on_callback_query)) + logging.info("Handlers registered.") u.bot.send_message(config["Telegram"]["main_group"], f"ℹ Royal Bot avviato e pronto a ricevere comandi!\n" f"Ultimo aggiornamento: `{version}: {commit_msg}`", @@ -594,11 +628,12 @@ def process(arg_discord_connection): while True: try: u.start_polling() + logging.info("Polling started.") u.idle() except telegram.error.TimedOut: - print("Telegrambot timed out.") + logging.warning("Timed out, restarting in 1 minute.") time.sleep(60) - print("Telegrambot restarting...") + logging.info("Now restarting...") if __name__ == "__main__": diff --git a/templates/profile.html b/templates/profile.html index 3c935d83..5ad5f64f 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -28,7 +28,7 @@ Bio
- {{ css.bio }} + {{ bio }}
{% endif %} diff --git a/webserver.py b/webserver.py index 0a6f8314..d7bd5a30 100644 --- a/webserver.py +++ b/webserver.py @@ -84,8 +84,10 @@ def page_profile(name: str): tg = db_session.query(db.Telegram).filter_by(royal=user).one_or_none() discord = db_session.execute(query_discord_music.one_query, {"royal": user.id}).fetchone() db_session.close() + converted_bio = Markup(markdown2.markdown(css.bio.replace("<", "<"), + extras=["spoiler", "tables", "smarty-pants", "fenced-code-blocks"])) return render_template("profile.html", ryg=user, css=css, osu=osu, rl=rl, dota=dota, lol=lol, steam=steam, ow=ow, - tg=tg, discord=discord, config=config) + tg=tg, discord=discord, config=config, bio=converted_bio) @app.route("/login") @@ -248,7 +250,7 @@ def page_wiki(key: str): f' modificata da' f' {user.username}:' f' {"Nessun motivo specificato." if not edit_reason else edit_reason}\n', - parse_mode="HTML") + parse_mode="HTML", disable_web_page_preview=True, disable_notification=True) except Exception: pass return redirect(url_for("page_wiki", key=key))