-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioCache.py
More file actions
94 lines (78 loc) · 2.77 KB
/
AudioCache.py
File metadata and controls
94 lines (78 loc) · 2.77 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
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
import hashlib
import sqlite3
import random
import os
import config
import logging
logger = logging.getLogger(__name__)
def text_to_hash(text):
"""Generate a 29 character hash from a string.
- Used for audio cache file names.
- Changing the hash will break the cache."""
try:
hash = hashlib.sha256(text.encode()).hexdigest()
return hash[:29]
except:
return None
def select_random_text():
"""Select a random text line from the database file."""
# Read the data from the database file, choose a random text line
try:
db = sqlite3.connect(config.DATABASE_FILE)
cursor = db.cursor()
cursor.execute("SELECT * FROM texts ORDER BY RANDOM() LIMIT 1")
data = cursor.fetchone()
db.close()
except:
data = []
return None
# Select a random text line
random_index = random.randint(0, len(data) - 1)
random_text = data[random_index]["text"]
return random_text
def create_entry(text, state=None):
"""Create a new entry in the database file."""
if text is None:
return None
hash = text_to_hash(text.strip())
# Create a new text line entry in the database with autoincrement id, hash, text, state and audio file path
try:
db = sqlite3.connect(config.DATABASE_FILE)
cursor = db.cursor()
cursor.execute(
"INSERT INTO texts (hash, text, state, audio_file_path) VALUES (?, ?, ?, ?)",
(
hash,
text,
str(state),
f"{config.AUDIO_CACHE_FOLDER}{os.path.sep}{hash}.wav",
),
)
db.commit()
db.close()
logger.info(f"DB - Entry created: {text}")
except Exception as e:
logger.error(f"DB - Error creating entry: {e}")
return None
entry = {"text": text, "hash": hash, "state": state}
return entry
def get_or_create_entry(text, state=None):
"""Get an entry from the database file or create a new one if it doesn't exist."""
try:
with sqlite3.connect(config.DATABASE_FILE) as db:
db.row_factory = sqlite3.Row
cursor = db.cursor()
cursor.execute("SELECT id, hash, text, state, audio_file_path FROM texts WHERE text = ?", (text,))
result = cursor.fetchone()
if result:
return {
"id": result['id'],
"text": result['text'],
"hash": result['hash'],
"state": result['state'],
"audio_file_path": result['audio_file_path']
}
except Exception as e:
logger.error(f"DB - Error getting entry: {e}")
# If text is not in database or there was an error, create new entry
return create_entry(text, state)