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,11 +62,17 @@ def check_database(): | ||
62 | sign_name VARCHAR(255) NOT NULL, | 62 | sign_name VARCHAR(255) NOT NULL, |
63 | amount int NOT NULL, | 63 | amount int NOT NULL, |
64 | has CHAR(3) )""" | 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 | create_platforms_table = """CREATE TABLE IF NOT EXISTS platforms ( | 70 | create_platforms_table = """CREATE TABLE IF NOT EXISTS platforms ( |
66 | platform_name VARCHAR(255) NOT NULL PRIMARY KEY, | 71 | platform_name VARCHAR(255) NOT NULL PRIMARY KEY, |
67 | amount int NOT NULL )""" | 72 | amount int NOT NULL )""" |
68 | insert_platforms_default = "INSERT IGNORE INTO platforms (platform_name, amount) VALUES (%s, %s)" | 73 | insert_platforms_default = "INSERT IGNORE INTO platforms (platform_name, amount) VALUES (%s, %s)" |
69 | cursor.execute(create_signs_table) | 74 | cursor.execute(create_signs_table) |
75 | + cursor.execute(create_translations_table) | ||
70 | cursor.execute(create_platforms_table) | 76 | cursor.execute(create_platforms_table) |
71 | cursor.execute(insert_platforms_default, ("ANDROID", 0)) | 77 | cursor.execute(insert_platforms_default, ("ANDROID", 0)) |
72 | cursor.execute(insert_platforms_default, ("IOS", 0)) | 78 | cursor.execute(insert_platforms_default, ("IOS", 0)) |
@@ -97,13 +103,35 @@ def select_sign_db(sign_name): | @@ -97,13 +103,35 @@ def select_sign_db(sign_name): | ||
97 | return None | 103 | return None |
98 | return amount | 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 | def update_platform_db(platform_name): | 128 | def update_platform_db(platform_name): |
101 | cursor = conn.cursor() | 129 | cursor = conn.cursor() |
102 | query_string = "UPDATE platforms SET amount=amount+1 WHERE platform_name=%s" | 130 | query_string = "UPDATE platforms SET amount=amount+1 WHERE platform_name=%s" |
103 | cursor.execute(query_string, (platform_name)); | 131 | cursor.execute(query_string, (platform_name)); |
104 | conn.commit() | 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 | if RUN_MODE == "full": | 135 | if RUN_MODE == "full": |
108 | with lock: | 136 | with lock: |
109 | has = "YES" if file_exists else "NO" | 137 | has = "YES" if file_exists else "NO" |
@@ -114,10 +142,21 @@ def update_statistic(sign_name, platform_name, file_exists): | @@ -114,10 +142,21 @@ def update_statistic(sign_name, platform_name, file_exists): | ||
114 | update_sign_db(sign_name, value_sign_name+1, has) | 142 | update_sign_db(sign_name, value_sign_name+1, has) |
115 | update_platform_db(platform_name) | 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 | @app.route("/translate", methods=['GET']) | 154 | @app.route("/translate", methods=['GET']) |
118 | def translate(): | 155 | def translate(): |
119 | text = request.args.get('text').encode("UTF-8") | 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 | @app.route("/statistics") | 161 | @app.route("/statistics") |
123 | @check_run_mode | 162 | @check_run_mode |
@@ -135,7 +174,7 @@ def get_sign(platform, sign): | @@ -135,7 +174,7 @@ def get_sign(platform, sign): | ||
135 | path = BUNDLES_PATH[platform] | 174 | path = BUNDLES_PATH[platform] |
136 | bundle_path = os.path.join(path, sign) | 175 | bundle_path = os.path.join(path, sign) |
137 | file_exists = os.path.exists(os.path.join(path, sign)) | 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 | if file_exists: | 178 | if file_exists: |
140 | return send_from_directory(BUNDLES_PATH[platform], sign) | 179 | return send_from_directory(BUNDLES_PATH[platform], sign) |
141 | abort(404) | 180 | abort(404) |