Skip to content

Commit

Permalink
Update bot and site
Browse files Browse the repository at this point in the history
  • Loading branch information
torrua committed Nov 9, 2024
1 parent 88e4b21 commit 4385d61
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 70 deletions.
3 changes: 1 addition & 2 deletions app/bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# -*- coding:utf-8 -*-
"""
Providing routes for our application
"""

from flask import Blueprint, request as rq
from quart import Blueprint, request as rq
from telebot import types

from app.bot.telegram import bot, TOKEN
Expand Down
21 changes: 8 additions & 13 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from quart import Quart, render_template

from app.bot import bot_blueprint
from app.site.routes import site_blueprint

bootstrap = Bootstrap()


def create_app():
"""
Create app
"""

new_app = Flask(__name__)
bootstrap.init_app(new_app)
new_app = Quart(__name__)
new_app.register_blueprint(bot_blueprint, url_prefix="/bot")
new_app.register_blueprint(site_blueprint, url_prefix="/site")
new_app.debug = True
new_app.debug = False

@new_app.errorhandler(404)
def page_not_found(_):
return render_template("404.html"), 404
async def page_not_found(_):
return await render_template("404.html"), 404

@new_app.route("/", methods=["GET"])
@new_app.route("/index")
def index():
async def index():
"""
example endpoint
"""
return render_template("index.html")
return await render_template("index.html")

return new_app


app = create_app()

if __name__ == "__main__":
app = create_app()
app.run()
2 changes: 1 addition & 1 deletion app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask[async]>=3.0
Quart>=0.19.8
psycopg2-binary>=2.9
SQLAlchemy[asyncio]>=2.0
Loglan-Core>=0.5.0
Expand Down
1 change: 0 additions & 1 deletion app/site/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-r ../requirements.txt

Flask-Bootstrap~=3.3
Beautifulsoup4~=4.12
lxml~=5.3
56 changes: 29 additions & 27 deletions app/site/routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os

from flask import Blueprint, redirect, url_for, render_template, request, jsonify
from loglan_core import WordSelector, Event
from loglan_core.addons.definition_selector import DefinitionSelector
from quart import Blueprint, redirect, url_for, render_template, request, jsonify
from loglan_core import WordSelector, Event, BaseSelector, DefinitionSelector

from app.engine import async_session_maker
from app.site.compose.english_item import EnglishItem
Expand Down Expand Up @@ -37,50 +36,50 @@ def redirect_columns():

@site_blueprint.route("/")
@site_blueprint.route("/home")
def home():
async def home():
article = get_data(MAIN_SITE).body.find("div", {"id": "content"})
for bq in article.findAll("blockquote"):
bq["class"] = "blockquote"

for img in article.findAll("img"):
img["src"] = MAIN_SITE + img["src"]

return render_template(
return await render_template(
"home.html",
article="",
)


@site_blueprint.route("/articles")
def articles():
async def articles():
article_block = get_data(MAIN_SITE)
title = article_block.find("a", {"name": "articles"}).find_parent("h2")
content = title.find_next("ol")
return render_template(
return await render_template(
"articles.html",
articles=content,
title=title.get_text(),
)


@site_blueprint.route("/texts")
def texts():
async def texts():
article_block = get_data(MAIN_SITE)
title = article_block.find("a", {"name": "texts"}).find_parent("h2")
content = title.find_next("ol")
return render_template(
return await render_template(
"articles.html",
articles=content,
title=title.get_text(),
)


@site_blueprint.route("/columns")
def columns():
async def columns():
article_block = get_data(MAIN_SITE)
title = article_block.find("a", {"name": "columns"}).find_parent("h2")
content = title.find_next("ul")
return render_template(
return await render_template(
"articles.html",
articles=content,
title=title.get_text(),
Expand All @@ -90,25 +89,27 @@ def columns():
@site_blueprint.route("/dictionary")
@site_blueprint.route("/dictionary/")
async def dictionary():
with async_session_maker() as session:
events = session.query(Event).all()
events = {event.id: event.name for event in reversed(events)}
async with async_session_maker() as session:
events = await BaseSelector(model=Event).all_async(session)
events = {int(event.id): event.name for event in reversed(events)}
content = await generate_content(request.args)
return render_template(
return await render_template(
"dictionary.html",
content=content,
events=events,
)


@site_blueprint.route("/how_to_read")
def how_to_read():
return render_template("reading.html")
async def how_to_read():
return await render_template("reading.html")


@site_blueprint.route("/submit_search", methods=["POST"])
async def submit_search():
return await generate_content(request.form)

res = await request.form
return await generate_content(res)


def strtobool(val):
Expand All @@ -123,7 +124,7 @@ def strtobool(val):
async def generate_content(data):
word = data.get("word", str())
search_language = data.get("language_id", DEFAULT_SEARCH_LANGUAGE)
event_id = data.get("event_id", 1)
event_id = int(data.get("event_id", 1))
is_case_sensitive = data.get("case_sensitive", False)

if not word or not data:
Expand Down Expand Up @@ -162,7 +163,7 @@ async def search_eng(word, event_id, is_case_sensitive, nothing):
DefinitionSelector(case_sensitive=is_case_sensitive)
.with_relationships("source_word")
.by_key(key=word)
.by_event(event_id=event_id)
.by_event(event_id=int(event_id))
.all_async(session, unique=True)
)
result = EnglishItem(
Expand All @@ -178,14 +179,15 @@ async def search_eng(word, event_id, is_case_sensitive, nothing):
return result


async def search_log(word, event_id, is_case_sensitive, nothing):
async def search_log(word: str, event_id: int, is_case_sensitive: bool, nothing):

async with async_session_maker() as session:
word_result = await (
WordSelector(case_sensitive=is_case_sensitive)
.by_name(name=word)
.by_event(event_id=event_id)
.all_async(session)
WordSelector(case_sensitive=bool(is_case_sensitive))
.with_relationships()
.by_name(name=str(word))
.by_event(event_id=int(event_id))
.all_async(session, unique=True)
)
result = Composer(words=word_result, style=DEFAULT_HTML_STYLE).export_as_html()
if not result:
Expand All @@ -199,7 +201,7 @@ async def search_log(word, event_id, is_case_sensitive, nothing):

@site_blueprint.route("/<string:section>/", methods=["GET"])
@site_blueprint.route("/<string:section>/<string:article>", methods=["GET"])
def proxy(section: str = "", article: str = ""):
async def proxy(section: str = "", article: str = ""):
url = f"{MAIN_SITE}{section}/{article}"
content = get_data(url).body

Expand All @@ -210,7 +212,7 @@ def proxy(section: str = "", article: str = ""):
img["src"] = MAIN_SITE + section + "/" + img["src"]

name_of_article = content.h1.extract().get_text()
return render_template(
return await render_template(
"article.html",
name_of_article=name_of_article,
article=content,
Expand Down
70 changes: 57 additions & 13 deletions app/templates/404.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
{% extends "bootstrap/base.html" %}
{% block title %}fonifo{% endblock %}

{% block navbar %}
<div class="navbar navbar-fixed-top">
<!-- ... -->
</div>
{% endblock %}

{% block content %}
<div class="mx-3 px-3" style="margin-left: 25px"><h1>404</h1><br>
Levi pidri, jio tu na begco pei, no dzabi. Soi ceokra!<br></div>
{% endblock %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon"
type="image/png"
href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADJ0lEQVQ4T21TWUwTURQ9701bpmWAAq2tUlCQQhBRjPrhEomoARMVExdI1A+/MIS6JeKX30YSNaL4YeKHGrcS14AmKmpEMCYajUsEWdQWQaoVSwfaaWfmaYcl1vj+7rn3nHffPfcR/Ofs3XvDLMuRVYTQWeNpVWc08hfq6ysG/i0nfwPV1RedvKBvAUOO0Wob5a3JRjDg5/tuPQgDCOlUJLWisbHq4yRvSqCmxl2u59UWoWCUKmICTOlFsCyeB0WKovv8Lczc2YefzywIdgoKZFrW0LClNSaiCdTUuHN1Caxr2mofTSocQeSHAUPN+cipXA/GgJ5z1+HY0Q2doCDwKhX+tnQ1JLK8M2eqejWB3fuufEyaIzqnrRnSOvv1Mg1hTwEyy0u0eOBBOwyON0hZMKzFvns2BDuT3jUcqywidXXXHOGI7M3e9QmUV7SC2C2hvnw41paCECDQ9Rkjng7YN3zW8rLI4cvZbDCZ5ZLa2qtHTJmRAxmVnql5MBXov5AL29ISJDpskENheFsegimjSFs+iMRcEd6Ls1h40HCUuFzuGynzgxutq7/FOeRvs8KgW4G04vxxnDGI/T4MtD5F1o5e+GMDfZvcTFyuppspxYEK66rx92uuRygGmmYitWAJzAUTqzCR+/78FQI9PaC8hIiPbya1tVcOm7KiBzO2eogajRGzIQ1RJM22Y0bpMhCOgskKGGOgep0mMzb4A1/vd0RlMXyI7N9/OVNm1JNd/Qmhfh7B14XIKFsJquO0YskfgKf5EVRZhW1JMaThEQhZ0+G90y5RUPuEje4uwSnmRX8ZYXYuROpc5zh5eASe262wlHrBFA7B91bw0wMYfmEGU+nVkyc2V8UtktGWTrPWl4JQAiUkoa/pLiwrvRDyglPzCbw2w/8kXY2Gaf7p01t7pqzbs+fSPNCE+4mZNsFessg0+LgDBnsv0pb5NLIyxsHfZoHYJahKhFt36tSWuzE87jO5XG4r0XFHoLLtjCn65EIRRK9CGjIyyacjhEM3VZTy48e39U22FCcwCdbV3ZoxNhbZRQgrin1BxtgHk4lrrK/f1B+3LH+C3+k0TOAiD2BKAAAAAElFTkSuQmCC"/>

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<title>{% block title %}fonifo{% endblock %}</title>

<style>
body {
font-family: 'Georgia', serif;
display: flex;
flex-direction: column;
min-height: 100vh;
}
.header {
text-align: center;
}
.content {
flex: 1 0 auto;
}
.footer {
font-size: 14px;
margin-right: 10px;
padding-left: 15px;
}

{% block style %}{% endblock %}
</style>

<body>
<div class="content">
{% block content %}
<div class="mx-3 px-3" style="margin-left: 25px"><h1>404</h1><br>
Levi pidri, jio tu na begco pei, no dzabi. Soi ceokra!<br></div>
{% endblock %}
</div>
<footer>
<nav class="navbar navbar-expand-lg bg-light">
<ul class="navbar-nav">
<li class="nav-item">
<div class="container-fluent footer">Copyright © <script type="text/javascript">var year = new Date();document.write(year.getFullYear());</script> by <a href="http://www.loglan.org/">The Loglan Institute</a> & <a href="https://github.com/torrua">torrua</a>. All rights reserved.</div>
</li>
</ul>
</nav>
</footer>

<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
</body>
</html>
70 changes: 57 additions & 13 deletions app/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
{% extends "bootstrap/base.html" %}
{% block title %}Loglan Documentation{% endblock %}

{% block navbar %}
<div class="navbar navbar-fixed-top">
<!-- ... -->
</div>
{% endblock %}

{% block content %}
<div class="mx-3 px-3" style="margin-left: 25px"><h1>Loglan Documentation</h1><br>
Loi, logli! <br>Ti po lo fomyrirda je la Loglan.<br></div>
{% endblock %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon"
type="image/png"
href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADJ0lEQVQ4T21TWUwTURQ9701bpmWAAq2tUlCQQhBRjPrhEomoARMVExdI1A+/MIS6JeKX30YSNaL4YeKHGrcS14AmKmpEMCYajUsEWdQWQaoVSwfaaWfmaYcl1vj+7rn3nHffPfcR/Ofs3XvDLMuRVYTQWeNpVWc08hfq6ysG/i0nfwPV1RedvKBvAUOO0Wob5a3JRjDg5/tuPQgDCOlUJLWisbHq4yRvSqCmxl2u59UWoWCUKmICTOlFsCyeB0WKovv8Lczc2YefzywIdgoKZFrW0LClNSaiCdTUuHN1Caxr2mofTSocQeSHAUPN+cipXA/GgJ5z1+HY0Q2doCDwKhX+tnQ1JLK8M2eqejWB3fuufEyaIzqnrRnSOvv1Mg1hTwEyy0u0eOBBOwyON0hZMKzFvns2BDuT3jUcqywidXXXHOGI7M3e9QmUV7SC2C2hvnw41paCECDQ9Rkjng7YN3zW8rLI4cvZbDCZ5ZLa2qtHTJmRAxmVnql5MBXov5AL29ISJDpskENheFsegimjSFs+iMRcEd6Ls1h40HCUuFzuGynzgxutq7/FOeRvs8KgW4G04vxxnDGI/T4MtD5F1o5e+GMDfZvcTFyuppspxYEK66rx92uuRygGmmYitWAJzAUTqzCR+/78FQI9PaC8hIiPbya1tVcOm7KiBzO2eogajRGzIQ1RJM22Y0bpMhCOgskKGGOgep0mMzb4A1/vd0RlMXyI7N9/OVNm1JNd/Qmhfh7B14XIKFsJquO0YskfgKf5EVRZhW1JMaThEQhZ0+G90y5RUPuEje4uwSnmRX8ZYXYuROpc5zh5eASe262wlHrBFA7B91bw0wMYfmEGU+nVkyc2V8UtktGWTrPWl4JQAiUkoa/pLiwrvRDyglPzCbw2w/8kXY2Gaf7p01t7pqzbs+fSPNCE+4mZNsFessg0+LgDBnsv0pb5NLIyxsHfZoHYJahKhFt36tSWuzE87jO5XG4r0XFHoLLtjCn65EIRRK9CGjIyyacjhEM3VZTy48e39U22FCcwCdbV3ZoxNhbZRQgrin1BxtgHk4lrrK/f1B+3LH+C3+k0TOAiD2BKAAAAAElFTkSuQmCC"/>

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<title>{% block title %}Loglan Documentation{% endblock %}</title>
</head>

<style>
body {
font-family: 'Georgia', serif;
display: flex;
flex-direction: column;
min-height: 100vh;
}
.header {
text-align: center;
}
.content {
flex: 1 0 auto;
}
.footer {
font-size: 14px;
margin-right: 10px;
padding-left: 15px;
}

{% block style %}{% endblock %}
</style>

<body>
<div class="content">
{% block content %}
<div class="mx-3 px-3" style="margin-left: 25px"><h1>Loglan Documentation</h1><br>
Loi, logli! <br>Ti po lo fomyrirda je la Loglan.<br></div>
{% endblock %}
</div>
<footer>
<nav class="navbar navbar-expand-lg bg-light">
<ul class="navbar-nav">
<li class="nav-item">
<div class="container-fluent footer">Copyright © <script type="text/javascript">var year = new Date();document.write(year.getFullYear());</script> by <a href="http://www.loglan.org/">The Loglan Institute</a> & <a href="https://github.com/torrua">torrua</a>. All rights reserved.</div>
</li>
</ul>
</nav>
</footer>

<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
</body>
</html>

0 comments on commit 4385d61

Please sign in to comment.