-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
158 lines (130 loc) · 5.19 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from datetime import datetime
from flask import jsonify, request, send_from_directory, send_file
import json
import validators
import langcodes
from api import Lyrics, Text, ContentUrl
from sendgridAPI import send_email
import nltk
nltk.download("punkt")
LYRICS_TRANSLATE = "lyricstranslate.com"
def configure_routes(app):
#####################################
# HTTP Handlers
#####################################
@app.route("/lyrics-to-anki", methods=["PUT"])
def song_req():
try:
obj = request.get_json()
lyrics = obj["lyrics"]
song_name = obj["song_name"]
song_lang = obj["song_lang"]
lyrics = Lyrics(lyrics, song_name, song_lang)
lyrics.build_anki_deck()
lyrics.build_anki_pkg()
return send_file(
lyrics.pkg,
mimetype="application/apkg",
as_attachment=True,
attachment_filename=lyrics.song_name + ".apkg",
)
except Exception as ex:
contentMsg = build_error_content_message(request, ex)
subjectMsg = "server side exception"
send_email(subjectMsg, contentMsg)
log_server_exception(ex)
return json_failure({"exception": str(ex)})
@app.route("/log-client-error", methods=["PUT"])
def log_client_error():
try:
obj = request.get_json()
sendAlertIndicator = obj["sendAlert"]
errorContext = obj["errorContext"]
log_client_exception(errorContext)
if sendAlertIndicator:
contentMsg = build_error_content_message(request, errorContext)
subjectMsg = "client side exception"
send_email(subjectMsg, contentMsg)
return json_success({"emailSent": sendAlertIndicator})
except Exception as ex:
contentMsg = build_error_content_message(request, ex)
subjectMsg = "server side exception while logging exception from client"
send_email(subjectMsg, contentMsg)
log_server_exception(ex)
return json_failure({"exception": str(ex)})
@app.route("/mobile/content-to-anki/", methods=["PUT"])
def anki_mobile_content_handler():
try:
obj = request.get_json()
# TODO: cleanse and validate input
text = obj["text"].strip()
lang = obj["lang"].strip().lower()
nonce = obj["nonce"].strip()
# validate lang
if not lang.isalpha():
raise Exception("Invalid input language arguments: {}".format(lang))
# TODO: right now language='en' hardcoded meaning client always sends language name written in english
# this much be changed when the mobile app supports more language
lang_code: str = langcodes.find(lang, language="en").language
# hack: youtube subtitles for chinese are searchable by zh-Hant, not zh
if lang_code == "zh" or lang_code == "cmn":
lang_code = "zh-Hant"
if validators.url(text):
url = text # we know text is actually a url
content_obj = ContentUrl(lang_code, url, nonce)
content_obj.hydrate_known_words()
if "youtube" in url.lower() or "youtu.be" in url.lower():
content_obj.process_youtube()
else:
content_obj.process_link()
a_list = content_obj.get_anki_notes()
else:
text_obj = Text(lang_code, text, nonce)
text_obj.hydrate_known_words()
text_obj.tokenize()
a_list = text_obj.get_anki_notes()
return json_success({"notes": a_list, "lang": lang, "nonce": nonce})
except Exception as ex:
print(ex)
return json_failure({"exception": str(ex)})
######################################
# Helper methods
######################################
def json_failure(fields=None):
if fields is None:
fields = {}
return jsonify({"success": False, **fields}), 400
def json_success(fields=None):
if fields is None:
fields = {}
return jsonify({"success": True, **fields}), 200
def log_server_exception(exc):
exc = str(exc)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
metadata_line = "*** Exception on " + dt_string + " \t***\n"
end_line = "\t***\t\n"
# with (open("logs.txt", "a+")) as file:
# file.write(metadata_line)
# file.write(exc)
# file.write(end_line)
print(metadata_line)
print(exc)
print(end_line)
def log_client_exception(exc):
exc = str(exc)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
metadata_line = "*** Exception on " + dt_string + " \t***\n"
end_line = "\t***\t\n"
# with (open("clientlogs.txt", "a+")) as file:
# file.write(metadata_line)
# file.write(exc)
# file.write(end_line)
print(metadata_line)
print(exc)
print(end_line)
def build_error_content_message(req, ex):
return "Input object is:\n {} \nand exception is\n {}".format(
str(req.get_json()), str(ex)
)