diff options
Diffstat (limited to 'lanre_py/lanre.py')
-rw-r--r-- | lanre_py/lanre.py | 60 |
1 files changed, 16 insertions, 44 deletions
diff --git a/lanre_py/lanre.py b/lanre_py/lanre.py index 4db21cd..376d4a6 100644 --- a/lanre_py/lanre.py +++ b/lanre_py/lanre.py @@ -40,51 +40,10 @@ def teardown_request(exception): @app.route('/') def ask_for_text(): - return render_template('ask_for_text.html') -@app.route('/options', methods=['POST']) -def add_entry(): - if not session.get('logged_in'): - abort(401) - g.db.execute('insert into entries (title, text) values (?, ?)', - [request.form['title'], request.form['text']]) - g.db.commit() - flash('New entry was successfully posted') - return redirect(url_for('ask_for_text.html')) - - -@app.route('/login', methods=['GET', 'POST']) -def login(): - error = None - if request.method == 'POST': - if request.form['username'] != app.config['USERNAME']: - error = 'Invalid username' - elif request.form['password'] != app.config['PASSWORD']: - error = 'Invalid password' - else: - session['logged_in'] = True - flash('You were logged in') - return redirect(url_for('ask_for_text')) - return render_template('login.html', error=error) -@app.route('/logout') -def logout(): - session.pop('logged_in', None) - flash('You were logged out') - return redirect(url_for('ask_for_text')) - - -def get_exact_translation(retrieved_json): - try: - response = retrieved_json["term0"]["PrincipalTranslations"]["0"]["FirstTranslation"]["term"] - if len(response) == 0: - response = "No translation" - except: - response = "No translation" - return response - @app.route('/_find_translations') def find_translations(): text_for_translation = request.args.get('text', 0) @@ -95,7 +54,6 @@ def find_translations(): for word in words: translation = get_exact_translation(requests.get(dictionary_link + strip_punctuation(word)).json) translations.append(translation) - return jsonify(result=translations, words=words, html_output=prepare_html_output(words)) @@ -118,8 +76,6 @@ def send_mail(): return jsonify(result="success") - - def prepare_html_output(words): words_with_html = ["<span class='word' id='" + str(i) +"'>" + word + "</span>" for i, word in enumerate(words)] @@ -137,6 +93,22 @@ def strip_punctuation(word): return word +def get_exact_translation(retrieved_json): + try: + response = fetch_translations(retrieved_json) + if len(response) == 0: + response = "No translation" + except: + response = "No translation" + return response + +def fetch_translations(retrieved_json): + response = "" + if(retrieved_json["term0"]["PrincipalTranslations"]["0"]["FirstTranslation"]["term"] != ""): + response += retrieved_json["term0"]["PrincipalTranslations"]["0"]["FirstTranslation"]["term"] + ", " + return response + + #this api is broken and inconsistent, I don't like it, mayby it'd be better to start with something different soon? #a95ae api key for word reference is: a95ae #http://www.wordreference.com/docs/api.aspx |