Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files- modules/dropbox.py +25 -16
- tests/dropbox_token_generator.py +35 -0
modules/dropbox.py
CHANGED
|
@@ -10,6 +10,23 @@ from datetime import datetime, timedelta, timezone
|
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def list_dropbox_folder_hierarchy(dbx: dropbox.Dropbox, base_path: str = ""):
|
| 15 |
"""
|
|
@@ -62,22 +79,21 @@ def list_dropbox_folder_hierarchy(dbx: dropbox.Dropbox, base_path: str = ""):
|
|
| 62 |
return hierarchy
|
| 63 |
|
| 64 |
|
| 65 |
-
dbx = dropbox.Dropbox(os.getenv("DROPBOX_ACCESS_TOKEN"))
|
| 66 |
-
|
| 67 |
-
|
| 68 |
class AudioRequest(BaseModel):
|
| 69 |
scripture_name: str
|
| 70 |
global_index: int
|
| 71 |
|
|
|
|
| 72 |
# cache = {(scripture_name, global_index, type): {"url": ..., "expiry": ...}}
|
| 73 |
audio_cache: dict[tuple[str, int, str], dict] = {}
|
| 74 |
CACHE_TTL = timedelta(hours=3, minutes=30) # refresh before 4h expiry
|
| 75 |
|
|
|
|
| 76 |
async def get_audio_urls(req: AudioRequest):
|
| 77 |
base_path = f"/{req.scripture_name}/audio"
|
| 78 |
files_to_check = {
|
| 79 |
"recitation": f"{req.global_index}-recitation.mp3",
|
| 80 |
-
"santhai": f"{req.global_index}-santhai.mp3"
|
| 81 |
}
|
| 82 |
|
| 83 |
urls = {}
|
|
@@ -100,10 +116,7 @@ async def get_audio_urls(req: AudioRequest):
|
|
| 100 |
temp_link = dbx.files_get_temporary_link(file_path).link
|
| 101 |
urls[key] = temp_link
|
| 102 |
# store in cache with expiry
|
| 103 |
-
audio_cache[cache_key] = {
|
| 104 |
-
"url": temp_link,
|
| 105 |
-
"expiry": now + CACHE_TTL
|
| 106 |
-
}
|
| 107 |
except dropbox.exceptions.ApiError:
|
| 108 |
urls[key] = None
|
| 109 |
|
|
@@ -112,6 +125,7 @@ async def get_audio_urls(req: AudioRequest):
|
|
| 112 |
|
| 113 |
return urls
|
| 114 |
|
|
|
|
| 115 |
async def cleanup_audio_url_cache(interval_seconds: int = 600):
|
| 116 |
"""Periodically remove expired entries from audio_cache."""
|
| 117 |
while True:
|
|
@@ -127,13 +141,8 @@ async def cleanup_audio_url_cache(interval_seconds: int = 600):
|
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
# Create Dropbox client with your access token
|
| 130 |
-
ACCESS_TOKEN = os.getenv("DROPBOX_ACCESS_TOKEN")
|
| 131 |
-
if not ACCESS_TOKEN:
|
| 132 |
-
raise Exception("No access token available")
|
| 133 |
-
dbx = dropbox.Dropbox(ACCESS_TOKEN)
|
| 134 |
-
|
| 135 |
# data = list_dropbox_folder_hierarchy(dbx, "")
|
| 136 |
-
data = asyncio.run(
|
| 137 |
-
AudioRequest(scripture_name="divya_prabandham", global_index=0)
|
| 138 |
-
)
|
| 139 |
print(json.dumps(data, indent=2))
|
|
|
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
+
REFRESH_TOKEN = os.getenv("DROPBOX_REFRESH_TOKEN")
|
| 14 |
+
APP_KEY = os.getenv("DROPBOX_APP_KEY")
|
| 15 |
+
APP_SECRET = os.getenv("DROPBOX_APP_SECRET")
|
| 16 |
+
|
| 17 |
+
if not REFRESH_TOKEN:
|
| 18 |
+
raise Exception("DROPBOX_REFRESH_TOKEN missing")
|
| 19 |
+
|
| 20 |
+
if not APP_KEY:
|
| 21 |
+
raise Exception("APP_KEY missing")
|
| 22 |
+
|
| 23 |
+
if not APP_SECRET:
|
| 24 |
+
raise Exception("APP_SECRET missing")
|
| 25 |
+
|
| 26 |
+
dbx = dropbox.Dropbox(
|
| 27 |
+
app_key=APP_KEY, app_secret=APP_SECRET, oauth2_refresh_token=REFRESH_TOKEN
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
|
| 31 |
def list_dropbox_folder_hierarchy(dbx: dropbox.Dropbox, base_path: str = ""):
|
| 32 |
"""
|
|
|
|
| 79 |
return hierarchy
|
| 80 |
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
class AudioRequest(BaseModel):
|
| 83 |
scripture_name: str
|
| 84 |
global_index: int
|
| 85 |
|
| 86 |
+
|
| 87 |
# cache = {(scripture_name, global_index, type): {"url": ..., "expiry": ...}}
|
| 88 |
audio_cache: dict[tuple[str, int, str], dict] = {}
|
| 89 |
CACHE_TTL = timedelta(hours=3, minutes=30) # refresh before 4h expiry
|
| 90 |
|
| 91 |
+
|
| 92 |
async def get_audio_urls(req: AudioRequest):
|
| 93 |
base_path = f"/{req.scripture_name}/audio"
|
| 94 |
files_to_check = {
|
| 95 |
"recitation": f"{req.global_index}-recitation.mp3",
|
| 96 |
+
"santhai": f"{req.global_index}-santhai.mp3",
|
| 97 |
}
|
| 98 |
|
| 99 |
urls = {}
|
|
|
|
| 116 |
temp_link = dbx.files_get_temporary_link(file_path).link
|
| 117 |
urls[key] = temp_link
|
| 118 |
# store in cache with expiry
|
| 119 |
+
audio_cache[cache_key] = {"url": temp_link, "expiry": now + CACHE_TTL}
|
|
|
|
|
|
|
|
|
|
| 120 |
except dropbox.exceptions.ApiError:
|
| 121 |
urls[key] = None
|
| 122 |
|
|
|
|
| 125 |
|
| 126 |
return urls
|
| 127 |
|
| 128 |
+
|
| 129 |
async def cleanup_audio_url_cache(interval_seconds: int = 600):
|
| 130 |
"""Periodically remove expired entries from audio_cache."""
|
| 131 |
while True:
|
|
|
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
# Create Dropbox client with your access token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
# data = list_dropbox_folder_hierarchy(dbx, "")
|
| 145 |
+
data = asyncio.run(
|
| 146 |
+
get_audio_urls(AudioRequest(scripture_name="divya_prabandham", global_index=0))
|
| 147 |
+
)
|
| 148 |
print(json.dumps(data, indent=2))
|
tests/dropbox_token_generator.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
AUTH_CODE = os.getenv("DROPBOX_AUTH_CODE")
|
| 9 |
+
APP_KEY = os.getenv("DROPBOX_APP_KEY")
|
| 10 |
+
APP_SECRET = os.getenv("DROPBOX_APP_SECRET")
|
| 11 |
+
|
| 12 |
+
if not AUTH_CODE:
|
| 13 |
+
raise Exception("DROPBOX_AUTH_CODE missing")
|
| 14 |
+
|
| 15 |
+
if not APP_KEY:
|
| 16 |
+
raise Exception("APP_KEY missing")
|
| 17 |
+
|
| 18 |
+
if not APP_SECRET:
|
| 19 |
+
raise Exception("APP_SECRET missing")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
resp = requests.post(
|
| 23 |
+
"https://api.dropbox.com/oauth2/token",
|
| 24 |
+
data={
|
| 25 |
+
"code": os.getenv("DROPBOX_AUTH_CODE"),
|
| 26 |
+
"grant_type": "authorization_code",
|
| 27 |
+
# either include client_id/client_secret in the form:
|
| 28 |
+
"client_id": APP_KEY,
|
| 29 |
+
"client_secret": APP_SECRET
|
| 30 |
+
},
|
| 31 |
+
timeout=10
|
| 32 |
+
)
|
| 33 |
+
data = resp.json()
|
| 34 |
+
print(data)
|
| 35 |
+
# data contains 'access_token', 'refresh_token', 'expires_in'...
|