-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroboLangUpdate.py
More file actions
68 lines (49 loc) · 1.63 KB
/
Copy pathroboLangUpdate.py
File metadata and controls
68 lines (49 loc) · 1.63 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
import time
from googlelangdetect import detect_language_v2
# exc_info is used for getting exceptions info
from sys import exc_info
import bpgsql
def loadConfig():
config = {}
f = open("server.txt")
s = f.readlines()
f.close()
for c in s:
try:
config[c.split("=")[0]] = c.split("=")[1].replace("\r","").replace("\n","")
except:
print "."
return config
def main():
global config;
config = loadConfig();
#print config
print "Language Updater here!"
print "We are in %(env)s !" % {"env":config["environment"]}
print "Database server is at %(env)s !" % {"env":config["servername"]}
print "RoboBird db name is %(env)s !" % {"env":config["databasename"]}
db = bpgsql.Connection(host=config["servername"], username= config["pguser"], password=config["pgpass"], dbname=config["databasename"])
cur = db.cursor()
cur.execute('SELECT "Word" FROM "Words" WHERE "Lang" = \'\'');
c = 0;
dbg = bpgsql.Connection(host=config["servername"], username= config["pguser"], password=config["pgpass"], dbname=config["databasename"])
for w in cur:
c += 1
time.sleep(1);
try:
tlang = detect_language_v2(w, api_key=config["googletranslateapikey"])
except:
tlang = "x";
print c, w , tlang
if tlang <> "":
curg = dbg.cursor()
# THIS IS TO BE FIXED POSSIBLE SQL INJECTION PROBLEM HERE
#print 'UPDATE "Words" SET "Lang" = E\'%(lang)s\' WHERE "Word" = E\'%(w)s\';' % {"w":w[0], "lang":tlang[0]}
curg.execute('UPDATE "Words" SET "Lang" = E\'%(lang)s\' WHERE "Word" = E\'%(w)s\';' % {"w":w[0], "lang":tlang[0]});
dbg.close();
db.close();
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
quit()