Commit 647ef3f29843f81c3b643aabacc692d4654de48b
1 parent
6da48b61
Exists in
devel
[TranslationServer] Salva traduções no BD
Showing
1 changed file
with
42 additions
and
3 deletions
Show diff stats
src/TranslationServer.py
... | ... | @@ -62,11 +62,17 @@ def check_database(): |
62 | 62 | sign_name VARCHAR(255) NOT NULL, |
63 | 63 | amount int NOT NULL, |
64 | 64 | has CHAR(3) )""" |
65 | + create_translations_table = """CREATE TABLE IF NOT EXISTS translations ( | |
66 | + id int NOT NULL PRIMARY KEY AUTO_INCREMENT, | |
67 | + text LONGTEXT NOT NULL, | |
68 | + gloss LONGTEXT NOT NULL, | |
69 | + amount int NOT NULL )""" | |
65 | 70 | create_platforms_table = """CREATE TABLE IF NOT EXISTS platforms ( |
66 | 71 | platform_name VARCHAR(255) NOT NULL PRIMARY KEY, |
67 | 72 | amount int NOT NULL )""" |
68 | 73 | insert_platforms_default = "INSERT IGNORE INTO platforms (platform_name, amount) VALUES (%s, %s)" |
69 | 74 | cursor.execute(create_signs_table) |
75 | + cursor.execute(create_translations_table) | |
70 | 76 | cursor.execute(create_platforms_table) |
71 | 77 | cursor.execute(insert_platforms_default, ("ANDROID", 0)) |
72 | 78 | cursor.execute(insert_platforms_default, ("IOS", 0)) |
... | ... | @@ -97,13 +103,35 @@ def select_sign_db(sign_name): |
97 | 103 | return None |
98 | 104 | return amount |
99 | 105 | |
106 | +def insert_translation_db(text, gloss): | |
107 | + cursor = conn.cursor() | |
108 | + query_string = "INSERT INTO translations (text, gloss, amount) VALUES (%s, %s, %s)" | |
109 | + cursor.execute(query_string, (text, gloss, 1)); | |
110 | + conn.commit() | |
111 | + | |
112 | +def update_translation_db(text, gloss): | |
113 | + cursor = conn.cursor() | |
114 | + query_string = "UPDATE translations SET amount=amount+1 WHERE text=%s AND gloss=%s" | |
115 | + cursor.execute(query_string, (text, gloss)); | |
116 | + conn.commit() | |
117 | + | |
118 | +def select_translation_db(text, gloss): | |
119 | + cursor = conn.cursor() | |
120 | + query_string = "SELECT amount FROM translations WHERE text=%s AND gloss=%s" | |
121 | + cursor.execute(query_string, (text, gloss)); | |
122 | + try: | |
123 | + amount = cursor.fetchone()[0] | |
124 | + except TypeError: | |
125 | + return None | |
126 | + return amount | |
127 | + | |
100 | 128 | def update_platform_db(platform_name): |
101 | 129 | cursor = conn.cursor() |
102 | 130 | query_string = "UPDATE platforms SET amount=amount+1 WHERE platform_name=%s" |
103 | 131 | cursor.execute(query_string, (platform_name)); |
104 | 132 | conn.commit() |
105 | 133 | |
106 | -def update_statistic(sign_name, platform_name, file_exists): | |
134 | +def update_database_statistic(sign_name, platform_name, file_exists): | |
107 | 135 | if RUN_MODE == "full": |
108 | 136 | with lock: |
109 | 137 | has = "YES" if file_exists else "NO" |
... | ... | @@ -114,10 +142,21 @@ def update_statistic(sign_name, platform_name, file_exists): |
114 | 142 | update_sign_db(sign_name, value_sign_name+1, has) |
115 | 143 | update_platform_db(platform_name) |
116 | 144 | |
145 | +def update_database_translation(text, gloss): | |
146 | + if RUN_MODE == "full": | |
147 | + with lock: | |
148 | + gloss_selected = select_translation_db(text, gloss) | |
149 | + if gloss_selected is None: | |
150 | + insert_translation_db(text, gloss) | |
151 | + else: | |
152 | + update_translation_db(text, gloss) | |
153 | + | |
117 | 154 | @app.route("/translate", methods=['GET']) |
118 | 155 | def translate(): |
119 | 156 | text = request.args.get('text').encode("UTF-8") |
120 | - return traduzir(text) | |
157 | + gloss = traduzir(text) | |
158 | + thread.start_new_thread(update_database_translation, (text, gloss)) | |
159 | + return gloss | |
121 | 160 | |
122 | 161 | @app.route("/statistics") |
123 | 162 | @check_run_mode |
... | ... | @@ -135,7 +174,7 @@ def get_sign(platform, sign): |
135 | 174 | path = BUNDLES_PATH[platform] |
136 | 175 | bundle_path = os.path.join(path, sign) |
137 | 176 | file_exists = os.path.exists(os.path.join(path, sign)) |
138 | - thread.start_new_thread(update_statistic, (sign, platform, file_exists)) | |
177 | + thread.start_new_thread(update_database_statistic, (sign, platform, file_exists)) | |
139 | 178 | if file_exists: |
140 | 179 | return send_from_directory(BUNDLES_PATH[platform], sign) |
141 | 180 | abort(404) | ... | ... |