From fde88b9648d2c5601badb47870732f3dace6f550 Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 3 Jan 2021 02:26:21 +0100 Subject: [PATCH] updated interface --- app.py | 25 +++++++++++++++++++++---- templates/index.html | 5 +++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 4f4bed22..1861bd01 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,31 @@ from flask import Flask, render_template, request +from bs4 import BeautifulSoup +import urllib, requests app = Flask(__name__) @app.route('/') def main(): - return render_template('index.html', svg="") + return render_template('index.html', svg="", name="") @app.route('/submit_kanji', methods=['GET']) def submit_kanji(): try: - with open(f"assets/0{format(ord(request.args.get('kanji')), 'x')}.svg", encoding='utf-8') as file: - return render_template('index.html', svg=file.read()) + character = request.args.get('kanji') + + url = "https://jisho.org/search/{0}".format(urllib.parse.quote_plus(character + "#kanji")) + r = requests.get(url, headers={ + "User-Agent": "little-web-project", + "From": "http://lauchism.com" + }) + + if r.status_code != 200: + print(f"ERROR: Failed to access Jisho API... {r.status_code}") + return None + + body = BeautifulSoup(r.text, features="html.parser") + names = body.find("div", {"class": "kanji-details__main-meanings"}) + + with open(f"assets/0{format(ord(character), 'x')}.svg", encoding='utf-8') as file: + return render_template('index.html', svg=file.read(), name=names.string) except: - return render_template('index.html', svg="no") \ No newline at end of file + return render_template('index.html', svg="no", name="no") \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 4959d0ec..1fcf6669 100644 --- a/templates/index.html +++ b/templates/index.html @@ -31,6 +31,7 @@ +

search for kanji

@@ -39,6 +40,10 @@ {{svg|safe}} +

+ {{name}} +

+