vikramvasudevan commited on
Commit
033e9d9
·
verified ·
1 Parent(s): 29087af

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. modules/dropbox/audio.py +32 -20
modules/dropbox/audio.py CHANGED
@@ -69,41 +69,53 @@ CACHE_TTL = timedelta(hours=3, minutes=30) # refresh before 4h expiry
69
 
70
  async def get_audio_urls(req: AudioRequest):
71
  base_path = f"/{req.scripture_name}/audio"
72
- files_to_check = {
73
- "recitation": f"{req.global_index}-recitation.mp3",
74
- "santhai": f"{req.global_index}-santhai.mp3",
75
- }
76
 
77
  urls = {}
78
- now = datetime.now(timezone.utc) # timezone-aware UTC datetime
79
 
80
- for key, filename in files_to_check.items():
81
- cache_key = (req.scripture_name, req.global_index, key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
- # Check cache first
84
  cached = audio_cache.get(cache_key)
85
  if cached and cached["expiry"] > now:
86
- urls[key] = cached["url"]
87
  continue
88
 
89
- # Generate new temporary link
90
  file_path = f"{base_path}/{filename}"
91
  try:
92
- metadata = dbx.files_get_metadata(file_path)
93
- if isinstance(metadata, FileMetadata):
94
- temp_link = dbx.files_get_temporary_link(file_path).link
95
- urls[key] = temp_link
96
- # store in cache with expiry
97
- audio_cache[cache_key] = {"url": temp_link, "expiry": now + CACHE_TTL}
98
  except dropbox.exceptions.ApiError:
99
- urls[key] = None
100
-
101
- if not any(urls.values()):
102
- raise HTTPException(status_code=404, detail="No audio files found")
103
 
104
  return urls
105
 
106
 
 
107
  async def cleanup_audio_url_cache(interval_seconds: int = 600):
108
  """Periodically remove expired entries from audio_cache."""
109
  while True:
 
69
 
70
  async def get_audio_urls(req: AudioRequest):
71
  base_path = f"/{req.scripture_name}/audio"
72
+ prefix = f"{req.global_index}-"
 
 
 
73
 
74
  urls = {}
75
+ now = datetime.now(timezone.utc)
76
 
77
+ try:
78
+ # List all files under the scripture's audio directory
79
+ entries = dbx.files_list_folder(base_path).entries
80
+ except dropbox.exceptions.ApiError:
81
+ raise HTTPException(status_code=404, detail="Audio directory not found")
82
+
83
+ # Filter files that match the prefix
84
+ matching_files = [
85
+ entry for entry in entries
86
+ if isinstance(entry, FileMetadata) and entry.name.startswith(prefix)
87
+ ]
88
+
89
+ if not matching_files:
90
+ raise HTTPException(status_code=404, detail="No audio files found")
91
+
92
+ for entry in matching_files:
93
+ # Extract file type from "<global_index>-<type>.mp3"
94
+ # Example: "123-recitation.mp3" -> "recitation"
95
+ filename = entry.name
96
+ file_type = filename[len(prefix):].rsplit(".", 1)[0]
97
+
98
+ cache_key = (req.scripture_name, req.global_index, file_type)
99
 
100
+ # Check cache
101
  cached = audio_cache.get(cache_key)
102
  if cached and cached["expiry"] > now:
103
+ urls[file_type] = cached["url"]
104
  continue
105
 
106
+ # Generate temporary link
107
  file_path = f"{base_path}/{filename}"
108
  try:
109
+ temp_link = dbx.files_get_temporary_link(file_path).link
110
+ urls[file_type] = temp_link
111
+ audio_cache[cache_key] = {"url": temp_link, "expiry": now + CACHE_TTL}
 
 
 
112
  except dropbox.exceptions.ApiError:
113
+ urls[file_type] = None
 
 
 
114
 
115
  return urls
116
 
117
 
118
+
119
  async def cleanup_audio_url_cache(interval_seconds: int = 600):
120
  """Periodically remove expired entries from audio_cache."""
121
  while True: