Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
| from fastapi import HTTPException | |
| from modules.audio.model import AudioRequest, AudioType | |
| from modules.dropbox.art import get_audible_art_url | |
| from modules.dropbox.audibles import get_audible_audio_url | |
| from modules.dropbox.audio import get_audio_urls, get_global_indices_with_audio | |
| from config import SanatanConfig | |
| from db import SanatanDatabase | |
| from typing import List | |
| async def svc_get_audio_urls(req: AudioRequest): | |
| config = SanatanConfig().get_scripture_by_name(req.scripture_name) | |
| audio_storage = config.get("audio_storage", "dropbox") | |
| if audio_storage == "dropbox": | |
| urls = await get_audio_urls(req) | |
| else: | |
| db = SanatanDatabase() | |
| data = db.fetch_document_by_index( | |
| collection_name=config["collection_name"], index=req.global_index | |
| ) | |
| url = data.get("audio", data.get("audio_url", "")) | |
| ## Temporary fix for bhagavat gita audio being moved in the source. | |
| if req.scripture_name == "bhagavat_gita": | |
| url = url.replace( | |
| "https://cdn.vivekavani.com/wp-content/", | |
| "https://vivekavani.com/wp-content/", | |
| ) | |
| urls = {"recitation": url} | |
| return urls | |
| async def svc_get_indices_with_audio( | |
| scripture_name: str, audio_type: AudioType | |
| ) -> List[int]: | |
| """ | |
| Service function to get all global indices for a scripture | |
| that have audio files of the specified type. | |
| Args: | |
| scripture_name: Name of the scripture. | |
| audio_type: AudioType enum value. | |
| Returns: | |
| List[int]: Sorted list of global indices. | |
| """ | |
| config = SanatanConfig().get_scripture_by_name(scripture_name) | |
| audio_storage = config.get("audio_storage", "dropbox") | |
| if audio_storage == "dropbox": | |
| indices = await get_global_indices_with_audio(scripture_name, audio_type) | |
| else: | |
| # Fallback for database storage: iterate all documents and filter by audio_type | |
| db = SanatanDatabase() | |
| collection_name = config["collection_name"] | |
| total = db.count(collection_name=collection_name) | |
| all_docs = db.fetch_all_matches(collection_name, page_size=total) | |
| indices = [] | |
| for doc in all_docs: | |
| audio_field = doc.get("audio", "") | |
| if audio_field.lower().startswith(audio_type.value): | |
| indices.append(doc["_global_index"]) | |
| indices.sort() | |
| return indices | |
| async def svc_get_audible_audio_url(path: str): | |
| if not path.startswith("_audibles/audio/"): | |
| raise HTTPException(status_code=400, detail="Invalid audible path") | |
| url = await get_audible_audio_url(path) | |
| return url | |
| async def svc_get_audible_art_url(path: str): | |
| if not path.startswith("_audibles/art/"): | |
| raise HTTPException(status_code=400, detail="Invalid audible path") | |
| url = await get_audible_art_url(path) | |
| return url | |