diff options
author | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2012-12-25 20:32:39 +0100 |
---|---|---|
committer | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2012-12-25 20:32:39 +0100 |
commit | c0f8db6c713f9e8babe001e85e2af3dbf6aac202 (patch) | |
tree | c103606ff3b5542d7ed6290ef8b48d56dd389447 /lanre_py/lanre.py | |
parent | b30a5f614e5178fca2adb25faaa5d43c8bd70463 (diff) | |
download | lanre-master.tar.gz lanre-master.tar.bz2 lanre-master.zip |
Diffstat (limited to 'lanre_py/lanre.py')
-rw-r--r-- | lanre_py/lanre.py | 71 |
1 files changed, 43 insertions, 28 deletions
diff --git a/lanre_py/lanre.py b/lanre_py/lanre.py index 2effe07..90d3101 100644 --- a/lanre_py/lanre.py +++ b/lanre_py/lanre.py @@ -47,33 +47,50 @@ def ask_for_text(): @app.route('/_find_translations') def find_translations(): text_for_translation = request.args.get('text', 0) - language = request.args.get('language', 0) - dictionary_link = "http://api.wordreference.com/a95ae/json/" + language + "/" + from_language = request.args.get('from_language', 0) + dest_language = request.args.get('dest_language', 0) words = text_for_translation.split(" ") translations = [] for word in words: - translation = get_exact_translation(requests.get(dictionary_link + strip_punctuation(word)).json) + translation = find_translation_in_glosbe(prepare_word(word), from_language, dest_language) translations.append(translation) return jsonify(result=translations, words=words, html_output=prepare_html_output(words)) +def find_translation_in_glosbe(phrase, from_language, dest): + url = "http://glosbe.com/gapi/translate?from=" + from_language + url += "&dest=" + dest + "&format=json&phrase=" + url += phrase + "&pretty=true" + response = requests.get(url).json + try: + return response["tuc"][0]["phrase"]["text"] + except: + return "no translation" + + + + + @app.route('/_send_mail') def send_mail(): - from_address = "attero@hackerspace.pl" - to_address = request.args.get('mail_address', 0) - message_text = request.args.get('message', 0) - msg = ("From: %s\r\nTo: %s\r\nSubject: Lanre\r\n" % (from_address, to_address)) - server = smtplib.SMTP('hackerspace.pl', 587) - server.starttls() - server.login("attero", "dupa.8") - server.set_debuglevel(1) - print(from_address) - print(to_address) - print(msg + message_text) - print("somethin wrong in loging to server") - server.sendmail(from_address, to_address, msg + message_text) - server.quit() - return jsonify(result="success") + try: + from_address = "attero@hackerspace.pl" + to_address = request.args.get('mail_address', 0) + message_text = request.args.get('message', 0) + msg = ("From: %s\r\nTo: %s\r\nSubject: Lanre\r\n" % (from_address, to_address)) + server = smtplib.SMTP('hackerspace.pl', 587) + server.starttls() + server.login("attero", "dupa.8") + server.set_debuglevel(1) + print(from_address) + print(to_address) + print(msg + message_text) + print("somethin wrong in loging to server") + server.sendmail(from_address, to_address, msg + message_text) + server.quit() + return jsonify(result="success") + except: + return jsonify(result="failure") @@ -82,6 +99,12 @@ def prepare_html_output(words): output = " ".join(words_with_html) return output +def prepare_word(word): + try: + word = strip_punctuation(word) + return word[0].lower() + word[1:] + except: + return "" def strip_punctuation(word): try: @@ -93,15 +116,6 @@ 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 = "" @@ -109,7 +123,8 @@ def fetch_translations(retrieved_json): 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? + +#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 |