-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
85 lines (73 loc) · 2.11 KB
/
project.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
from flask import Flask, render_template, request, redirect, url_for, jsonify
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Msgkey
from DTMFdetector import DTMFdetector
from nocache import nocache
import os
import hashing_algo
import datetime
import wavegen
import base64
app = Flask(__name__)
engine = create_engine('sqlite:///soufi.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
@app.route('/')
@app.route('/home')
def index():
return render_template('home.html')
@app.route('/sender/msg')
@nocache
def send_msg():
return render_template('send_msg.html')
@app.route('/receive/msg')
@nocache
def rec_msg():
return render_template('rec_msg.html')
@app.route('/sender/play', methods=['GET', 'POST'])
@nocache
def send_play():
if request.method == 'POST':
ts = datetime.datetime.now()
nowt = str(ts)
i = 0
ts = ""
while len(ts)<14:
if(nowt[i]>="0" and nowt[i]<="9"):
ts=ts+nowt[i];
i=i+1
n = int(ts)
hashkey = hashing_algo.idToHash(n)
wavegen.make_audio_file(hashkey)
newmsg = Msgkey(hash_key=hashkey,name=str(request.form['msg']))
session.add(newmsg)
session.commit()
return render_template('send_play.html',msg = request.form['msg'],hashkey=hashkey)
@app.route('/receive/hash', methods=['GET', 'POST'])
@nocache
def rec_hash():
if request.method == 'POST':
base= request.form['sound']
base=base.replace("data:audio/wav;base64,","")
wavefile = base64.b64decode(base)
filename = 's.wav' # I assume you have a way of picking unique filenames
with open(filename, 'wb') as f:
f.write(wavefile)
cmd = "sox s.wav -C 256 -r 16000 ns2.wav"
print os.system(cmd)
dtmf = DTMFdetector()
data = dtmf.getDTMFfromWAV("ns2.wav")
new_str="%"
for i in range(len(data)):
new_str=new_str+data[i]+"%"
try:
msg_val = session.query(Msgkey).filter(Msgkey.hash_key.like(new_str)).one()
# return render_template('rec_hash.html',msg = base)
return msg_val.name
except:
return data
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)