vikramvasudevan commited on
Commit
e74e470
·
verified ·
1 Parent(s): 7be9035

Upload folder using huggingface_hub

Browse files
data/dropbox/discourses/narasimha_avatar.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "id": 1,
3
+ "topic_name": "Narasimha Avataram",
4
+ "description": "Discourses and references about Lord Narasimha's incarnation.",
5
+ "thumbnail_url": "https://img.youtube.com/vi/-AZOlc6_aMQ/hqdefault.jpg",
6
+ "videos": [
7
+ {
8
+ "youtube_id": "-AZOlc6_aMQ",
9
+ "title": "நரசிம்ம அவதாரம் | ஸ்ரீ சங்கர மடம் | Narayaniyam | Dr Venkatesh Upanyasam | Narasimha Avatar.",
10
+ "playlist_id": null,
11
+ "timestamp_tags": [
12
+ {
13
+ "start_sec": 120,
14
+ "label": "Thirumangai Azhwar - Periya Thirumozhi 2.3.8",
15
+ "scripture": "divya_prabandham",
16
+ "global_index": 1075
17
+ },
18
+ {
19
+ "start_sec": 120,
20
+ "label": "Thirumangai Azhwar - Thirunedunthandagam 4.26",
21
+ "scripture": "divya_prabandham",
22
+ "global_index": 2077
23
+ }
24
+ ]
25
+ }
26
+ ]
27
+ }
data/dropbox/discourses/sahasranamam.json ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "id": 2,
3
+ "topic_name": "Sahasranamam",
4
+ "description": "Discourses and references about Sahasranamam.",
5
+ "thumbnail_url": "https://img.youtube.com/vi/XCHbU6KU82A/hqdefault.jpg",
6
+ "videos": [
7
+ {
8
+ "youtube_id": "XCHbU6KU82A",
9
+ "title": "VISHNU SAHASRANAMA MAHIMAI part 1 | விஷ்ணு ஸஹஸ்ரநாம மஹிமை 1 | Dr Venkatesh Upanyasam",
10
+ "playlist_id": null,
11
+ "timestamp_tags": [
12
+ {
13
+ "start_sec": 1,
14
+ "label": "Shuklambaradharam",
15
+ "scripture": "vishnu_sahasranamam",
16
+ "global_index": 1
17
+ },
18
+ {
19
+ "start_sec": 3622,
20
+ "label": "Sthuvantha:",
21
+ "scripture": "vishnu_sahasranamam",
22
+ "global_index": 8
23
+ },
24
+ {
25
+ "start_sec": 5400,
26
+ "end_sec": 5500,
27
+ "label": "Nara: Sada:",
28
+ "scripture": "vishnu_sahasranamam",
29
+ "global_index": 14
30
+ }
31
+ ]
32
+ },
33
+ {
34
+ "youtube_id": "YQseyjQ6kZc",
35
+ "title": "VISHNU SAHASRANAMA MAHIMAI part 2 | விஷ்ணு ஸஹஸ்ரநாம மகிமை 2 | Dr Venkatesh Upanyasam",
36
+ "playlist_id": null,
37
+ "timestamp_tags": [
38
+ {
39
+ "start_sec": 30,
40
+ "label": "Shuklambaradharam",
41
+ "scripture": "vishnu_sahasranamam",
42
+ "global_index": 30
43
+ },
44
+ {
45
+ "start_sec": 900,
46
+ "end_sec": 960,
47
+ "label": "Shuklambaradharam",
48
+ "scripture": "vishnu_sahasranamam",
49
+ "global_index": 40
50
+ },
51
+ {
52
+ "start_sec": 1800,
53
+ "end_sec": 1830,
54
+ "label": "Shuklambaradharam",
55
+ "scripture": "vishnu_sahasranamam",
56
+ "global_index": 80
57
+ }
58
+ ]
59
+ }
60
+ ]
61
+ }
modules/dropbox/discources.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ def fetch_all_discourses(folder_path="data/dropbox/discourses"):
5
+ """
6
+ Loads all JSON discourse files from the specified folder and returns
7
+ them as a list of Python dictionaries.
8
+
9
+ Each file should contain a valid JSON object representing one topic.
10
+ """
11
+ discourses = []
12
+
13
+ if not os.path.exists(folder_path):
14
+ print(f"⚠️ Folder not found: {folder_path}")
15
+ return discourses
16
+
17
+ for filename in os.listdir(folder_path):
18
+ if filename.lower().endswith(".json"):
19
+ file_path = os.path.join(folder_path, filename)
20
+ try:
21
+ with open(file_path, "r", encoding="utf-8") as f:
22
+ data = json.load(f)
23
+ discourses.append(data)
24
+ except Exception as e:
25
+ print(f"❌ Error loading {filename}: {e}")
26
+
27
+ return discourses
28
+
29
+ def get_discourse_summaries(folder_path="data/dropbox/discourses", page=1, per_page=10):
30
+ """
31
+ Returns a paginated list of discourse topic summaries.
32
+
33
+ Each summary contains:
34
+ - id
35
+ - topic_name
36
+ - thumbnail_url
37
+
38
+ The results are sorted alphabetically by topic_name.
39
+
40
+ Args:
41
+ folder_path (str): Path to the folder containing JSON files.
42
+ page (int): Page number (1-indexed).
43
+ per_page (int): Number of items per page.
44
+ """
45
+ summaries = []
46
+
47
+ if not os.path.exists(folder_path):
48
+ print(f"⚠️ Folder not found: {folder_path}")
49
+ return []
50
+
51
+ # Load and extract only minimal info
52
+ for filename in os.listdir(folder_path):
53
+ if filename.lower().endswith(".json"):
54
+ file_path = os.path.join(folder_path, filename)
55
+ try:
56
+ with open(file_path, "r", encoding="utf-8") as f:
57
+ data = json.load(f)
58
+ summaries.append({
59
+ "id": data.get("id"),
60
+ "topic_name": data.get("topic_name"),
61
+ "thumbnail_url": data.get("thumbnail_url")
62
+ })
63
+ except Exception as e:
64
+ print(f"❌ Error loading {filename}: {e}")
65
+
66
+ # Sort by topic name
67
+ summaries.sort(key=lambda x: (x.get("topic_name") or "").lower())
68
+
69
+ # Pagination logic
70
+ total_items = len(summaries)
71
+ total_pages = (total_items + per_page - 1) // per_page # ceil division
72
+
73
+ if page < 1 or page > total_pages:
74
+ print(f"⚠️ Invalid page number {page}. Must be between 1 and {total_pages}.")
75
+ return []
76
+
77
+ start = (page - 1) * per_page
78
+ end = start + per_page
79
+
80
+ paginated = summaries[start:end]
81
+
82
+ return {
83
+ "page": page,
84
+ "per_page": per_page,
85
+ "total_pages": total_pages,
86
+ "total_items": total_items,
87
+ "data": paginated
88
+ }
89
+
90
+
91
+ import os
92
+ import json
93
+
94
+ def get_discourse_by_id(topic_id: int, folder_path="data/dropbox/discourses"):
95
+ """
96
+ Returns the full discourse JSON for a given topic_id.
97
+ Returns None if not found.
98
+ """
99
+ if not os.path.exists(folder_path):
100
+ print(f"⚠️ Folder not found: {folder_path}")
101
+ return None
102
+
103
+ for filename in os.listdir(folder_path):
104
+ if filename.lower().endswith(".json"):
105
+ file_path = os.path.join(folder_path, filename)
106
+ try:
107
+ with open(file_path, "r", encoding="utf-8") as f:
108
+ data = json.load(f)
109
+ if data.get("id") == topic_id:
110
+ return data
111
+ except Exception as e:
112
+ print(f"❌ Error loading {filename}: {e}")
113
+
114
+ return None
server.py CHANGED
@@ -4,7 +4,7 @@ import random
4
  import traceback
5
  from typing import List, Optional
6
  import uuid
7
- from fastapi import APIRouter, Request, Query
8
  from fastapi.responses import JSONResponse
9
  import pycountry
10
  from pydantic import BaseModel
@@ -15,6 +15,7 @@ from metadata import MetadataWhereClause
15
  from modules.audio.model import AudioRequest, AudioType
16
  from modules.audio.service import svc_get_audio_urls, svc_get_indices_with_audio
17
  from modules.config.categories import get_scripture_categories
 
18
  from modules.languages.get_v2 import handle_fetch_languages_v2
19
  from modules.quiz.answer_validator import validate_answer
20
  from modules.quiz.models import Question
@@ -563,4 +564,29 @@ def route_get_donation_product_ids(include_tests: bool = False):
563
  {"id": "android.test.refunded"},
564
  {"id": "android.test.item_unavailable"},
565
  ]
566
- return products
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import traceback
5
  from typing import List, Optional
6
  import uuid
7
+ from fastapi import APIRouter, HTTPException, Request, Query
8
  from fastapi.responses import JSONResponse
9
  import pycountry
10
  from pydantic import BaseModel
 
15
  from modules.audio.model import AudioRequest, AudioType
16
  from modules.audio.service import svc_get_audio_urls, svc_get_indices_with_audio
17
  from modules.config.categories import get_scripture_categories
18
+ from modules.dropbox.discources import fetch_all_discourses, get_discourse_by_id, get_discourse_summaries
19
  from modules.languages.get_v2 import handle_fetch_languages_v2
20
  from modules.quiz.answer_validator import validate_answer
21
  from modules.quiz.models import Question
 
564
  {"id": "android.test.refunded"},
565
  {"id": "android.test.item_unavailable"},
566
  ]
567
+ return products
568
+
569
+ @router.get("/discourse/list")
570
+ def get_all_discourses(
571
+ page: int = Query(1, ge=1, description="Page number (1-indexed)"),
572
+ per_page: int = Query(10, ge=1, le=100, description="Number of items per page")
573
+ ):
574
+ """
575
+ Returns a paginated list of discourse topics.
576
+ Each topic includes:
577
+ - id
578
+ - topic_name
579
+ - thumbnail_url
580
+ """
581
+ result = get_discourse_summaries(page=page, per_page=per_page)
582
+ return result
583
+
584
+ @router.get("/discourse/find/{topic_id}")
585
+ def get_discourse_detail(topic_id: int):
586
+ """
587
+ Returns the full details of a discourse topic by its unique ID.
588
+ """
589
+ topic = get_discourse_by_id(topic_id)
590
+ if not topic:
591
+ raise HTTPException(status_code=404, detail="Discourse topic not found")
592
+ return topic