-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.py
More file actions
66 lines (45 loc) · 1.99 KB
/
Copy pathcron.py
File metadata and controls
66 lines (45 loc) · 1.99 KB
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
import os
from configparser import ConfigParser
import pymysql
from googletrans import Translator
def translate(lang, srclang, sources, cursor, translator):
if lang["issource"] == 1:
for src in sources:
cursor.execute("INSERT INTO translations (source, language, txt) "
"VALUES (%s, %s, %s)", (src["id"], lang["id"],
src["txt"]))
else:
srctxts = list(map(lambda x: x["txt"], sources))
trans = translator.translate(srctxts, src=srclang["locale"],
dest=lang["locale"])
for i, t in enumerate(trans):
cursor.execute("INSERT INTO translations (source, language, txt) "
"VALUES (%s, %s, %s)", (sources[i]["id"],
lang["id"], t.text))
cursor.execute("UPDATE languages SET isready=1 WHERE id=%s", (lang["id"]))
def main():
config = ConfigParser()
config.read(os.path.join(os.path.dirname(os.path.abspath(__file__)),
'doodledoo.ini'))
cfg = config["doodledoo"]
con = pymysql.connect(host=cfg['dbhost'], user=cfg['dbuser'],
password=cfg['dbpassword'], db=cfg['dbname'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cursor = con.cursor()
cursor.execute("SELECT id, locale FROM languages WHERE issource=1")
sourcelang = cursor.fetchall()
if len(sourcelang) != 1:
raise Exception("Multiple source languages found")
sourcelang = sourcelang[0]
cursor.execute("SELECT id, txt FROM sources")
sources = cursor.fetchall()
cursor.execute("SELECT id, locale, issource FROM languages WHERE "
"isready=0")
langs = cursor.fetchall()
trans = Translator()
for l in langs:
translate(l, sourcelang, sources, cursor, trans)
con.commit()
if __name__ == "__main__":
main()