def get_next_session(message): email = message['email'] loudness = message['loudness'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) cursor.execute( """insert into sessions(calibrated_rms_value) values(%s);""", [loudness]) cursor.execute("""select LASTVAL();""") # xxx convert to fetchone session_id = cursor.fetchall()[0][0] cursor.execute( """select count(*) from users u, sentences s, user_sentence_session us where s.id = us.sentence_id and u.id = us.user_id and u.email = %s;""", [email]) total_completed = cursor.fetchall()[0][0] cursor.execute( """select u.session_count from users u where u.email = %s;""", [email]) sentences_per_session = cursor.fetchall()[0][0] hvb_close_db(conn, cursor) response = { "code": "session", "session_id": session_id, "completed": total_completed, "sentences_per_session": sentences_per_session } print("response = ", response) return response
def login_post(): conn, cursor = hvb_connect_db(app.hvb_conf["db"]) email = request.form["email"] password = request.form["password"] cursor.execute("""select email, password, compendium from users where email = %s; """, [email]) user = cursor.fetchall() if len(user) == 0: print("hvb.login: ERROR: email not known") return render_template("login.html") elif len(user) > 1: print("hvb.login: ERROR: email associated with multiple accounts") password = user[0][1] compendium = user[0][2] salted_password = hashlib.sha512(request.form["password"] + compendium).hexdigest() if password != salted_password: print("hvb.login: ERROR: password equality test failed") session["email"] = None error = "Invalid credentials" return render_template("login.html") session["email"] = email hvb_close_db(conn, cursor) # flash("You were successfully logged in") return redirect(url_for("record"))
def update_opacity(message): email = message['email'] css_id = message['css_id'] opacity = message['opacity'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) cursor.execute("""update user_grid_opacity ugo SET increments = increments + 1, opacity = %s from users u, grid g where u.email = %s and ugo.user_id = u.id and ugo.grid_id = g.id and g.css_id = %s;""", [opacity, email, css_id]) hvb_close_db(conn, cursor)
def update_opacity(message): email = message['email'] css_id = message['css_id'] opacity = message['opacity'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) cursor.execute( """update user_grid_opacity ugo SET increments = increments + 1, opacity = %s from users u, grid g where u.email = %s and ugo.user_id = u.id and ugo.grid_id = g.id and g.css_id = %s;""", [opacity, email, css_id]) hvb_close_db(conn, cursor)
def update_session_count(message): email = message['email'] count = message['count'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) # '''update users set password=%(password)s,compendium=%(compendium)s where email=%(email)s''', form_data) print("setting the session count to ", count, " of user email = ", email) cursor.execute("""update users set session_count=%s where email = %s;""", [count, email]) hvb_close_db(conn, cursor) return None
def update_sentences_completed(message): email = message['email'] sentence_id = message['id'] session_id = message['session_id'] loudness = message['loudness'] rms_value = message['rms_value'] uri = 'https://s3.amazonaws.com/human-voice-bank'+'/'+email+'/'+message['uri'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) make_sentence_update(cursor, email, sentence_id, session_id, loudness, rms_value, uri) hvb_close_db(conn, cursor)
def get_opacity(message): email = message['email'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) cursor.execute("""select g.css_id, ugo.opacity, ugo.increments, ugo.instances from users u, grid g, user_grid_opacity ugo where u.email = %s and u.id = ugo.user_id and g.id = ugo.grid_id;""", [email]); raw_data = cursor.fetchall() opacity_array = [] for datum in raw_data: opacity_array.append({'id': datum[0], 'opacity': datum[1], 'increments': datum[2], 'instances': datum[3]}) opacity = {"code": "set-opacity", 'opacity-array': opacity_array} hvb_close_db(conn, cursor) return opacity
def get_next_sentence(message): email = message['email'] if 'count' in message: count = int(message['count']) if count > 100: count = 100 else: count = 25 conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) next_block = get_next_block(cursor, email, count) hvb_close_db(conn, cursor) return next_block
def update_sentences_completed(message): email = message['email'] sentence_id = message['id'] session_id = message['session_id'] loudness = message['loudness'] rms_value = message['rms_value'] uri = 'https://s3.amazonaws.com/human-voice-bank' + '/' + email + '/' + message[ 'uri'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) make_sentence_update(cursor, email, sentence_id, session_id, loudness, rms_value, uri) hvb_close_db(conn, cursor)
def password_reset(): form_data = request.form.to_dict() print("form data = ", form_data) if not ("email" in session) or session["email"] != form_data["email"]: abort(400) conn, cursor = hvb_connect_db(app.hvb_conf["db"]) salt = uuid.uuid4().hex form_data["password"] = hashlib.sha512(str(form_data["password"]) + str(salt)).hexdigest() form_data["compendium"] = salt cursor.execute( """update users set password=%(password)s,compendium=%(compendium)s where email=%(email)s""", form_data ) hvb_close_db(conn, cursor) return render_template("record.html")
def get_next_session(message): email = message['email'] loudness = message['loudness'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) cursor.execute("""insert into sessions(calibrated_rms_value) values(%s);""", [loudness]) cursor.execute("""select LASTVAL();""") # xxx convert to fetchone session_id = cursor.fetchall()[0][0] cursor.execute("""select count(*) from users u, sentences s, user_sentence_session us where s.id = us.sentence_id and u.id = us.user_id and u.email = %s;""", [email]); total_completed = cursor.fetchall()[0][0]; cursor.execute("""select u.session_count from users u where u.email = %s;""", [email]) sentences_per_session = cursor.fetchall()[0][0] hvb_close_db(conn, cursor) response = {"code": "session", "session_id": session_id, "completed": total_completed, "sentences_per_session": sentences_per_session} print("response = ", response) return response
def initiate_reset(email): conn, cursor = hvb_connect_db(app.hvb_conf["db"]) cursor.execute("""select id, email, compendium from users where email = %s; """, [email]) user = cursor.fetchall() if len(user) == 0: print("hvb.initiate_reset: email not known") return abort(400) elif len(user) > 1: print("hvb.login: ERROR: email associated with multiple accounts") hvb_close_db(conn, cursor) id = user[0][0] email = user[0][1] geo = user[0][2] reset_password(str(id) + geo, email) return ""
def get_opacity(message): email = message['email'] conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) cursor.execute( """select g.css_id, ugo.opacity, ugo.increments, ugo.instances from users u, grid g, user_grid_opacity ugo where u.email = %s and u.id = ugo.user_id and g.id = ugo.grid_id;""", [email]) raw_data = cursor.fetchall() opacity_array = [] for datum in raw_data: opacity_array.append({ 'id': datum[0], 'opacity': datum[1], 'increments': datum[2], 'instances': datum[3] }) opacity = {"code": "set-opacity", 'opacity-array': opacity_array} hvb_close_db(conn, cursor) return opacity
def registration1_post(): if not ("email" in session): return redirect("/", code=302) conn, cursor = hvb_connect_db(app.hvb_conf["db"]) try: form_data = request.form.to_dict() form_data["email"] = session["email"] print("HERE... formdata = ", form_data) cursor.execute( """update users set height_inches=%(height_inches)s, height_feet=%(height_feet)s, accent=%(accent)s, stateprovince=%(stateprovince)s, stateprovince1=%(stateprovince1)s, country=%(country)s, country1=%(country1)s, voice_sound=%(voice_sound)s, first_language=%(first_language)s, second_language=%(second_language)s where email=%(email)s""", form_data, ) except KeyError as key_error: hvb_close_db(conn, cursor) print("Key error = ", key_error) error = "Invalid credentials" print("Error = ", error) print("Key error: ") return "KeyError", 404 except IntegrityError as integrityError: hvb_close_db(conn, cursor) print(integrityError) return "IntegrityError", 404 except Exception as e: print("exection: ", e) return "Exception", 404 hvb_close_db(conn, cursor) print("OKAY") return "OK", 200 # redirect(url_for('record'))
def authorize_password_reset(userid, email): s = TimestampSigner(app.secret_key) user_email = s.unsign(email) conn, cursor = hvb_connect_db(app.hvb_conf["db"]) cursor.execute("""select id, compendium from users where email = %s; """, [user_email]) user = cursor.fetchall() if len(user) == 0: print("hvb.password_reset: email not known") return abort(400) elif len(user) > 1: print("hvb.login: ERROR: email associated with multiple accounts") hvb_close_db(conn, cursor) id = user[0][0] compendium = user[0][1] session["email"] = user_email if str(id) + str(compendium) == str(s.unsign(userid)): return render_template("password-reset.html", email=user_email) else: abort(400)
def load(): conf = parse_config() conn, cursor = hvb_connect_db(conf["db"]) cursor.execute("""select id, sentence, phonetic from sentences;""") sentence_tuples = cursor.fetchall() hvb_close_db(conn, cursor) sentence_meta_list = [] phoneme_set = set([]) for id, sentence, phonetic in sentence_tuples: sentence_meta = SentenceMeta(id, sentence, phonetic) parse_phonemes(sentence_meta, phoneme_set) sentence_meta_list.append(sentence_meta) phoneme_map = {} id = 0 for phoneme in list(phoneme_set): phoneme_map[phoneme] = id id += 1 f = open("phonemes-dml.sql", "w+") insert_phonemes(phoneme_map, f) insert_sentence_phoneme_join_table(phoneme_map, sentence_meta_list, f)
def registration_post(): # XXX the insert below should fail if the email already exists, but we should note the failure and provide a message # to the user and redirect them to the login/password reset page. conn, cursor = hvb_connect_db(app.hvb_conf["db"]) try: form_data = request.form.to_dict() print("formdata = ", form_data) salt = uuid.uuid4().hex form_data["password"] = hashlib.sha512(str(form_data["password"]) + str(salt)).hexdigest() form_data["compendium"] = salt cursor.execute( """insert into users (email, firstname, dob, gender, password, compendium) values (%(email)s, %(firstname)s, %(dob)s, %(gender)s, %(password)s, %(compendium)s);""", form_data, ) session["email"] = form_data["email"] cursor.execute( """insert into user_grid_opacity(user_id, grid_id, opacity, increments, instances) select u.id, g.id, 0, 0, (select count(*) from phoneme_grid pg, sentence_phoneme sp where pg.grid_id = g.id and pg.phoneme_id = sp.phoneme_id) from users u, grid g where u.email = %s;""", [session["email"]], ) except KeyError as key_error: hvb_close_db(conn, cursor) print("Key error = ", key_error) error = "Invalid credentials" print("Error = ", error) print("Key error: ") return "KeyError", 404 except IntegrityError as integrityError: hvb_close_db(conn, cursor) print(integrityError) return "IntegrityError", 404 except Exception as e: print("exection: ", e) return "Exception", 404 hvb_close_db(conn, cursor) print("OKAY") return "OK", 200 # redirect(url_for('record'))
conn.commit() if __name__ == '__main__': conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) filename = conf['csv'] print("Database connection established") execute_sql(conf['drop'], cursor, conn) print("Database dropped") execute_sql(conf['create'], cursor, conn) print("New schema created") #execute_sql(conf['vocalid'], cursor, conn) #print("") print("Creating sentences table.. ") create_sentences_table(filename, cursor, conn) print("Generating sentences table.. ") generate_sentence_metadata(conn, cursor, conf['phoneme_dml']) print("Executing phoneme dml") execute_sql(conf['phoneme_dml'], cursor, conn) print("Executing grid dml") execute_sql(conf['grid_dml'], cursor, conn) print("Generating phoneme grid table..") # XXX filename1 = 'sql/Phoneme_Unicode_Codes_and_HVB_locations_revised.csv' generate_phoneme_grid_mapping(conn, cursor, filename1, conf['grid_dml']) hvb_close_db(conn, cursor)
def upload_audio(message): try: email = message['email'] filename = message['filename'] rms = message['rms'] data = message['data'] length = int(message['length']) pcm_data = [] for i in range(0, length): pcm_data.append(float(data[str(i)])) sample_rate = message['sample_rate'] data = float32_wav_file(pcm_data, sample_rate) # f = open("ws-"+filename, 'wb+') # f.write(data) # f.flush() # f.close() conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) userid = '' dob = '' gender = '' platform = 'python-uploader' filename_column_field = '' full_path = str(email) + '/' + str(filename) + '-' + platform try: cursor.execute("""select u.id, u.dob, u.gender from users u where u.email = %s;""", [email]) userdata = cursor.fetchone() print("u.id u.dob, u.gender = ", userdata) userid = userdata[0] dob = userdata[1] gender = userdata[2] sentence = filename.split('-')[0] cursor.execute("""select filename from sentences where sentence = %s;""", [sentence]) filename_column_field = cursor.fetchone()[0] filename = str(email) + '/donor' + str(userid) + '-' + str(dob) + '-' + str( gender) + '-' + platform + '-' + filename_column_field + '-' + str(filename) except Exception as e: import sys import traceback print('TornadoWebsocketHandler: WARNING upload_audio exception generating wave file name with embedded data') exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr) full_path = filename hvb_close_db(conn, cursor) print("fullpath = ", full_path) record.upload_wav_to_s3(parse_config(), data, full_path) except Exception as e: import sys import traceback print('TornadoWebsocketHandler: WARNING upload_audio exception generating wave file name with embedded data') exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr)
def upload_audio(message): try: email = message['email'] filename = message['filename'] rms = message['rms'] data = message['data'] length = int(message['length']) pcm_data = [] for i in range(0, length): pcm_data.append(float(data[str(i)])) sample_rate = message['sample_rate'] data = float32_wav_file(pcm_data, sample_rate) # f = open("ws-"+filename, 'wb+') # f.write(data) # f.flush() # f.close() conf = parse_config() conn, cursor = hvb_connect_db(conf['db']) userid = '' dob = '' gender = '' platform = 'python-uploader' filename_column_field = '' full_path = str(email) + '/' + str(filename) + '-' + platform try: cursor.execute( """select u.id, u.dob, u.gender from users u where u.email = %s;""", [email]) userdata = cursor.fetchone() print("u.id u.dob, u.gender = ", userdata) userid = userdata[0] dob = userdata[1] gender = userdata[2] sentence = filename.split('-')[0] cursor.execute( """select filename from sentences where sentence = %s;""", [sentence]) filename_column_field = cursor.fetchone()[0] filename = str(email) + '/donor' + str(userid) + '-' + str( dob) + '-' + str( gender ) + '-' + platform + '-' + filename_column_field + '-' + str( filename) except Exception as e: import sys import traceback print( 'TornadoWebsocketHandler: WARNING upload_audio exception generating wave file name with embedded data' ) exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr) full_path = filename hvb_close_db(conn, cursor) print("fullpath = ", full_path) record.upload_wav_to_s3(parse_config(), data, full_path) except Exception as e: import sys import traceback print( 'TornadoWebsocketHandler: WARNING upload_audio exception generating wave file name with embedded data' ) exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr)