-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieveAudio.py
More file actions
46 lines (36 loc) · 1.47 KB
/
Copy pathretrieveAudio.py
File metadata and controls
46 lines (36 loc) · 1.47 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
# --------------------------------------------------------------------------------------------------------
# IMPORTING LIBRARIES
import mysql.connector as connection
import connectMysql as conn
# --------------------------------------------------------------------------------------------------------
def retrieve_audio(mydb, start_file_id, end_file_id):
try:
query = "SELECT ADID, AUDIO_DATA FROM AUDIO WHERE ADID BETWEEN %s AND %s"
cursor = mydb.cursor()
cursor.execute(query, (start_file_id, end_file_id))
rows = cursor.fetchall()
for row in rows:
audio_id, audio_data = row
output_filename = f"retrieved_audio_{audio_id}.wav"
# Write the binary data to a file
with open(output_filename, "wb") as file:
file.write(audio_data)
print(f"Successfully retrieved and saved to {output_filename}")
except connection.Error as error:
print(error)
with open("log.txt", "a") as f:
f.write(f"\n\nCould not retrieve audio file from MySQL server. \n\n")
finally:
if cursor:
cursor.close()
def main():
mydb = conn.connect_to_mysql()
if mydb is None:
print("Failed to connect to the database.")
return
retrieve_audio(mydb, 1,6)
if mydb.is_connected():
mydb.close()
print("Database connection closed.")
if __name__ == "__main__":
main()