vikramvasudevan commited on
Commit
0bc84f3
·
verified ·
1 Parent(s): b920ad4

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. modules/dropbox/audio.py +14 -8
modules/dropbox/audio.py CHANGED
@@ -67,6 +67,10 @@ audio_cache: dict[tuple[str, int, str], dict] = {}
67
  CACHE_TTL = timedelta(hours=3, minutes=30) # refresh before 4h expiry
68
 
69
 
 
 
 
 
70
  async def get_audio_urls(req: AudioRequest):
71
  base_path = f"/{req.scripture_name}/audio"
72
  prefix = f"{req.global_index}-"
@@ -75,12 +79,19 @@ async def get_audio_urls(req: AudioRequest):
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)
@@ -90,20 +101,16 @@ async def get_audio_urls(req: AudioRequest):
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
@@ -115,7 +122,6 @@ async def get_audio_urls(req: AudioRequest):
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:
 
67
  CACHE_TTL = timedelta(hours=3, minutes=30) # refresh before 4h expiry
68
 
69
 
70
+ from dropbox.files import FileMetadata
71
+ from datetime import datetime, timezone
72
+ from fastapi import HTTPException
73
+
74
  async def get_audio_urls(req: AudioRequest):
75
  base_path = f"/{req.scripture_name}/audio"
76
  prefix = f"{req.global_index}-"
 
79
  now = datetime.now(timezone.utc)
80
 
81
  try:
82
+ # Initial listing (first batch of up to 500)
83
+ result = dbx.files_list_folder(base_path)
84
+ entries = result.entries
85
+
86
+ # Continue listing if there are more entries
87
+ while result.has_more:
88
+ result = dbx.files_list_folder_continue(result.cursor)
89
+ entries.extend(result.entries)
90
+
91
  except dropbox.exceptions.ApiError:
92
  raise HTTPException(status_code=404, detail="Audio directory not found")
93
 
94
+ # Filter files matching the prefix
95
  matching_files = [
96
  entry for entry in entries
97
  if isinstance(entry, FileMetadata) and entry.name.startswith(prefix)
 
101
  raise HTTPException(status_code=404, detail="No audio files found")
102
 
103
  for entry in matching_files:
 
 
104
  filename = entry.name
105
  file_type = filename[len(prefix):].rsplit(".", 1)[0]
106
 
107
  cache_key = (req.scripture_name, req.global_index, file_type)
108
 
 
109
  cached = audio_cache.get(cache_key)
110
  if cached and cached["expiry"] > now:
111
  urls[file_type] = cached["url"]
112
  continue
113
 
 
114
  file_path = f"{base_path}/{filename}"
115
  try:
116
  temp_link = dbx.files_get_temporary_link(file_path).link
 
122
  return urls
123
 
124
 
 
125
  async def cleanup_audio_url_cache(interval_seconds: int = 600):
126
  """Periodically remove expired entries from audio_cache."""
127
  while True: