-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·148 lines (113 loc) · 3.67 KB
/
server.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
#!/usr/bin/env python2
from flask import *
import json
import threading as th
import requests
app = Flask(__name__, static_url_path='')
with open('Uni2Pinyin.json') as the_file:
pinyin = json.load(the_file)
with open('savedWords.json') as savedWordsJSON:
savedWords = json.load(savedWordsJSON)
fileLock = th.Lock()
TTSLock = th.Lock()
currentWords = {}
from pymongo import MongoClient
client = MongoClient("localhost", 27017)
db = client['hacktj']
@app.route('/')
def index():
return render_template('index.html')
@app.route('/uni2pinyin')
def getPinYin():
spoken = request.args.get('spoken')
actual = request.args.get('actual')
if len(actual) != len(spoken):
return 'False'
for c in range(len(spoken)):
spoken_unicode = hex(ord(spoken[c]))[2:].upper()
actual_unicode = hex(ord(actual[c]))[2:].upper()
if spoken_unicode not in pinyin or actual_unicode not in pinyin or not (set(pinyin[spoken_unicode]) & set(pinyin[actual_unicode])):
return 'False'
return 'True'
@app.route('/addWord')
def addWord():
word = request.args.get('word')
th.Thread(target=getTTSAsync, args=(word,)).run()
return 'True'
@app.route('/delWord')
def delWord():
word = request.args.get('word')
th.Thread(target=delTTSAsync, args=(word,)).run()
return 'True'
def delTTSAsync(word):
TTSLock.acquire()
try:
del(currentWords[word])
except ValueError:
pass
TTSLock.release()
def getTTSAsync(word):
out = requests.get('https://api.voicerss.org/', params={'key': '970f71e61a4b4c8abd6af0d1f6a5326e', 'src': word, 'hl': 'zh-cn', 'r': -5})
TTSLock.acquire()
currentWords[word] = out
TTSLock.release()
@app.route('/saveSet')
def saveWordList():
name = request.args.get('label')
words = request.args.get('words')
db.lists.insert({'label':name, 'words':words})
return 'True'
@app.route('/loadSet')
def loadWordList():
name = request.args.get('label')
try:
data = db.lists.find({'label':name})
return jsonify(**{'data': json.loads(data[0]['words'])})
except IndexError:
return jsonify(**{'data': []})
@app.route('/getListOfSets')
def getListList():
return jsonify(**{'data': [x['label'] for x in db.lists.find()]})
@app.route('/deleteSet')
def deleteWordList():
name = request.args.get('label')
try:
db.lists.delete_many({'label':name})
except TypeError:
return 'False'
return 'True'
@app.route('/loadMeaningFromFile')
def loadMeaningList():
words = open('file.txt').read().split('\t') # get file from js
char_meaning = {}
k = 0
while k < len(words):
char_meaning[words[k]] = words[k + 2]
return char_meaning
@app.route('/loadPinyinFromFile')
def loadPinyinList():
words = open('file.txt').read().split('\t') # get file from js
char_pinyin = {}
k = 0
while k < len(words):
char_pinyin[words[k]] = words[k + 1]
return char_pinyin
@app.route('/getTTS')
def getTTS():
phrase = request.args.get('chars')
try:
TTSLock.acquire()
out = Response(currentWords[phrase], mimetype='audio/mpeg')
TTSLock.release()
return out
except KeyError:
audio = requests.get('https://api.voicerss.org/', params={'key': '970f71e61a4b4c8abd6af0d1f6a5326e', 'src': phrase, 'hl': 'zh-cn', 'r': -5})
return Response(audio.content, mimetype='audio/mpeg')
def saveWords():
fileLock.acquire()
with open('savedWords.json', 'w') as jsonFile:
json.dump(savedWords, jsonFile)
fileLock.release()
import sys
if __name__ == '__main__':
app.run(host='0.0.0.0', port = int(sys.argv[1]) if len(sys.argv) > 1 else 80, threaded=True)