/
m_server.py
107 lines (96 loc) · 3.35 KB
/
m_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
#coding:utf-8
import os
import json
import time
import StringIO
import tornado.web
import tornado.ioloop
from hashlib import md5
from dejavu import Dejavu
from dejavu.recognize import FileRecognizer
# load config from a JSON file (or anything outputting a python dictionary)
with open("dejavu.cnf") as f:
config = json.load(f)
PORT = 13982
SETTINGS = {
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
"static_path": os.path.join(os.path.dirname(__file__), "static"),
# "login_url":'/login',
# "cookie_secret": '6d791ffb386be8ca0c75cbce9b13cc6d',#md5 for zephor
"debug":True
}
class MainHandler(tornado.web.RequestHandler):
def get(self):
try:
self.get_argument('pc')
except:
self.render("index.html")
else:
self.render("pc.html")
def post(self):
self.set_header('Content-Type', 'application/json;charset=utf-8')
op=self.get_argument('op')
res = {
'status': True,
'data': None
}
if op == 'w':
kw = self.get_argument('kword')
res['data'] = kw
elif op == 'a':
res['data'] = self.msearch()
# for aud in self.request.files:
# aud=self.request.files[aud]
# if len(aud)<1:
# self.write(u'上传失败')
# return
# _t=aud[0]['content_type']
# if _t not in types:
# self.write(u'上传文件有误,请回去重新上传')
# return
# fname=md5(aud[0]['filename']+str(time())).hexdigest()
# fname+='.png'
# if not _op.exists(_uploadFolder+fid):
# os.mkdir(_uploadFolder+fid)
# #f=open(_uploadFolder+fid+_op.sep+fname, 'wb')
# try:
# img = Image.open(StringIO.StringIO(aud[0]['body']))
# song = djv.recognize(FileRecognizer, "mp3/Sean-Fournier--Falling-For-You.mp3")
self.write(json.dumps(res))
def msearch(self):
types=('audio/wav','audio/mp3')
for aud in self.request.files:
print aud
aud=self.request.files[aud]
if len(aud)<1:
return u'上传失败'
_t=aud[0]['content_type']
if _t not in types:
return u'上传文件有误,请回去重新上传'
print
fname=md5(aud[0]['filename']+str(time.time())).hexdigest()
fname+='.wav'
fname = os.path.join('temp', fname)
# print type()
with open(fname, 'wb') as f:
f.write(aud[0]['body'])
try:
djv = Dejavu(config)
print 'begint recognize...'
song = djv.recognize(FileRecognizer, fname)
except Exception as e:
song = str(e)
finally:
# os.remove(fname)
pass
print 'recognize: ', song
return song
application = tornado.web.Application([
(r"/index.html", MainHandler),
(r"/sm", MainHandler),
], **SETTINGS)
if __name__ == "__main__":
application.listen(PORT)
print 'Development server is running at http://127.0.0.1:%s/' % PORT
print 'Quit the server with CONTROL-C'
tornado.ioloop.IOLoop.instance().start()