yxqo41main commited on
Commit
c4d0696
·
verified ·
1 Parent(s): 2d0838b

Initial API upload

Browse files
Files changed (1) hide show
  1. main.py +163 -12
main.py CHANGED
@@ -39,6 +39,9 @@ CDN_DIR = BASE_DIR / "cdn"
39
  API_DATA_DIR = CDN_DIR / "api_data"
40
  RECITERS_DIR = CDN_DIR / "reciters" / "audio"
41
  MUSHAFS_DIR = CDN_DIR / "mushafs"
 
 
 
42
 
43
  # Simple keep-alive function using threading.Timer
44
  def keep_alive():
@@ -52,62 +55,91 @@ def keep_alive():
52
  async def startup_event():
53
  keep_alive()
54
 
55
- # Reciter info (based on existing data)
56
  RECITERS = {
57
  "haitham-al-dukhin": {
58
  "name": "Haitham Al Dokhin",
59
  "arabic_name": "هيثم الدخين",
60
- "types": ["murattal"]
 
 
61
  },
62
  "abdul-rashid-sofi": {
63
  "name": "Abdul-Rasheed Sofi",
64
  "arabic_name": "عبد الرشيد الصوفي",
65
- "types": ["murattal"]
 
 
66
  },
67
  "yasser-al-dossari": {
68
  "name": "Yasser Al-Dossari",
69
  "arabic_name": "ياسر الدوسري",
70
- "types": ["murattal"]
 
 
71
  },
72
  "abdul-baset": {
73
  "name": "Abdul Baset",
74
  "arabic_name": "عبد الباسط عبد الصمد",
75
- "types": ["mujawwad", "murattal"]
 
 
76
  },
77
  "khalil-al-husary": {
78
  "name": "Khalil Al-Husary",
79
  "arabic_name": "محمود خليل الحصري",
80
- "types": ["murattal", "muallim"]
 
 
81
  },
82
  "siddiq-minshawi": {
83
  "name": "Siddiq Minshawi",
84
  "arabic_name": "محمد صديق المنشاوي",
85
- "types": ["murattal", "kids-repeat"]
 
 
86
  },
87
  "hani-ar-rifai": {
88
  "name": "Hani Ar-Rifai",
89
  "arabic_name": "هاني الرفاعي",
90
- "types": ["murattal"]
 
 
91
  },
92
  "abdurrahmaan-as-sudais": {
93
  "name": "Abdurrahmaan As-Sudais",
94
  "arabic_name": "عبد الرحمن السديس",
95
- "types": ["murattal"]
 
 
96
  },
97
  "maher-al-muaiqly": {
98
  "name": "Maher Al-Muaiqly",
99
  "arabic_name": "ماهر المعيقلي",
100
- "types": ["murattal"]
 
 
101
  },
102
  "ahmed-al-ajamy": {
103
  "name": "Ahmed Al-Ajamy",
104
  "arabic_name": "أحمد العجمي",
105
- "types": ["murattal"]
 
 
106
  },
107
  "al-huzaifi": {
108
  "name": "Al-Huzaifi",
109
  "arabic_name": "الحوذيفي",
110
- "types": ["ayah-by-ayah"]
 
 
 
 
 
 
 
 
 
111
  }
112
  }
113
 
@@ -345,6 +377,125 @@ async def get_reciter_audio(reciter_name: str, recitation_type: str, surah_numbe
345
 
346
  raise HTTPException(status_code=404, detail="Audio file not found")
347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
  if __name__ == "__main__":
349
  import uvicorn
350
  uvicorn.run(app, host="0.0.0.0", port=8000)
 
39
  API_DATA_DIR = CDN_DIR / "api_data"
40
  RECITERS_DIR = CDN_DIR / "reciters" / "audio"
41
  MUSHAFS_DIR = CDN_DIR / "mushafs"
42
+ PROFILES_DIR = CDN_DIR / "reciters" / "profiles-images"
43
+ ATHAN_DIR = CDN_DIR / "reciters" / "athan"
44
+ BACKGROUND_AUDIO_DIR = CDN_DIR / "background_audio"
45
 
46
  # Simple keep-alive function using threading.Timer
47
  def keep_alive():
 
55
  async def startup_event():
56
  keep_alive()
57
 
58
+ # Reciter info (based on existing data with more details)
59
  RECITERS = {
60
  "haitham-al-dukhin": {
61
  "name": "Haitham Al Dokhin",
62
  "arabic_name": "هيثم الدخين",
63
+ "types": ["murattal"],
64
+ "description": "Haitham Al Dokhin is a renowned Quran reciter known for his melodious voice and clear articulation.",
65
+ "profile_image": "haitham-dokin-profile.jpg"
66
  },
67
  "abdul-rashid-sofi": {
68
  "name": "Abdul-Rasheed Sofi",
69
  "arabic_name": "عبد الرشيد الصوفي",
70
+ "types": ["murattal"],
71
+ "description": "Abdul-Rasheed Sofi is a respected Quran reciter with a unique style of recitation.",
72
+ "profile_image": "abdulrasheed-sofi-profile.jpg"
73
  },
74
  "yasser-al-dossari": {
75
  "name": "Yasser Al-Dossari",
76
  "arabic_name": "ياسر الدوسري",
77
+ "types": ["murattal"],
78
+ "description": "Yasser Al-Dossari is a popular Saudi Quran reciter and imam.",
79
+ "profile_image": "yasser-aldosari-profile.png"
80
  },
81
  "abdul-baset": {
82
  "name": "Abdul Baset",
83
  "arabic_name": "عبد الباسط عبد الصمد",
84
+ "types": ["mujawwad", "murattal"],
85
+ "description": "Abdul Baset was one of the most famous Quran reciters of the 20th century.",
86
+ "profile_image": "abdelbasset-profile.png"
87
  },
88
  "khalil-al-husary": {
89
  "name": "Khalil Al-Husary",
90
  "arabic_name": "محمود خليل الحصري",
91
+ "types": ["murattal", "muallim"],
92
+ "description": "Khalil Al-Husary was an Egyptian Quran reciter known for his precise tajweed.",
93
+ "profile_image": "mahmoud-khalil-al-hussary-profile.png"
94
  },
95
  "siddiq-minshawi": {
96
  "name": "Siddiq Minshawi",
97
  "arabic_name": "محمد صديق المنشاوي",
98
+ "types": ["murattal", "kids-repeat"],
99
+ "description": "Siddiq Minshawi was an Egyptian reciter with a powerful and clear voice.",
100
+ "profile_image": "mohamed-siddiq-el-minshawi-profile.png"
101
  },
102
  "hani-ar-rifai": {
103
  "name": "Hani Ar-Rifai",
104
  "arabic_name": "هاني الرفاعي",
105
+ "types": ["murattal"],
106
+ "description": "Hani Ar-Rifai is a respected Quran reciter from Saudi Arabia.",
107
+ "profile_image": "hani-ar-rifai-profile.jpeg"
108
  },
109
  "abdurrahmaan-as-sudais": {
110
  "name": "Abdurrahmaan As-Sudais",
111
  "arabic_name": "عبد الرحمن السديس",
112
+ "types": ["murattal"],
113
+ "description": "Abdurrahmaan As-Sudais is the imam of the Grand Mosque in Mecca.",
114
+ "profile_image": "abdul-rahman-al-sudais-profile.png"
115
  },
116
  "maher-al-muaiqly": {
117
  "name": "Maher Al-Muaiqly",
118
  "arabic_name": "ماهر المعيقلي",
119
+ "types": ["murattal"],
120
+ "description": "Maher Al-Muaiqly is an imam of the Prophet's Mosque in Medina.",
121
+ "profile_image": "Maher-al-Muaiqly-profile.png"
122
  },
123
  "ahmed-al-ajamy": {
124
  "name": "Ahmed Al-Ajamy",
125
  "arabic_name": "أحمد العجمي",
126
+ "types": ["murattal"],
127
+ "description": "Ahmed Al-Ajamy is a popular Quran reciter from Saudi Arabia.",
128
+ "profile_image": "Ahmed-ibn-Ali-al-Ajmy-profile.png"
129
  },
130
  "al-huzaifi": {
131
  "name": "Al-Huzaifi",
132
  "arabic_name": "الحوذيفي",
133
+ "types": ["ayah-by-ayah"],
134
+ "description": "Al-Huzaifi is known for his ayah-by-ayah recitation style.",
135
+ "profile_image": None
136
+ },
137
+ "mishary-rashid-alafasy": {
138
+ "name": "Mishary Rashid Alafasy",
139
+ "arabic_name": "مشاري راشد العفاسي",
140
+ "types": [],
141
+ "description": "Mishary Rashid Alafasy is a world-renowned Kuwaiti Quran reciter and nasheed artist.",
142
+ "profile_image": "mishary-rashid-alafasy-profile.jpeg"
143
  }
144
  }
145
 
 
377
 
378
  raise HTTPException(status_code=404, detail="Audio file not found")
379
 
380
+ # Get reciter profile image
381
+ @app.api_route("/api/reciter-profile/{reciter_name}", methods=["GET", "HEAD"], operation_id="getReciterProfile")
382
+ async def get_reciter_profile(reciter_name: str):
383
+ if reciter_name not in RECITERS:
384
+ raise HTTPException(status_code=404, detail="Reciter not found")
385
+
386
+ reciter = RECITERS[reciter_name]
387
+ if not reciter["profile_image"]:
388
+ raise HTTPException(status_code=404, detail="Profile image not available for this reciter")
389
+
390
+ profile_filename = reciter["profile_image"]
391
+
392
+ if USE_HF_CDN:
393
+ try:
394
+ filename = f"reciters/profiles-images/{profile_filename}"
395
+ local_path = hf_hub_download(
396
+ repo_id=HF_DATASET_URL,
397
+ filename=filename,
398
+ repo_type="dataset",
399
+ token=HF_TOKEN
400
+ )
401
+ # Determine media type based on extension
402
+ ext = Path(profile_filename).suffix.lower()
403
+ media_type = "image/jpeg" if ext in [".jpg", ".jpeg"] else "image/png"
404
+ return FileResponse(local_path, media_type=media_type)
405
+ except Exception as e:
406
+ print(f"Error fetching profile image from HF CDN: {e}")
407
+ # Fall back to local
408
+ pass
409
+
410
+ # Try local file
411
+ profile_path = PROFILES_DIR / profile_filename
412
+ if profile_path.exists():
413
+ ext = profile_path.suffix.lower()
414
+ media_type = "image/jpeg" if ext in [".jpg", ".jpeg"] else "image/png"
415
+ return FileResponse(profile_path, media_type=media_type)
416
+
417
+ raise HTTPException(status_code=404, detail="Profile image not found")
418
+
419
+ # Get list of available athan files
420
+ @app.get("/api/athan")
421
+ async def get_athan_list():
422
+ athan_files = []
423
+ if ATHAN_DIR.exists():
424
+ for file in ATHAN_DIR.iterdir():
425
+ if file.is_file() and file.suffix.lower() == ".mp3":
426
+ athan_files.append(file.name)
427
+ return JSONResponse(content=athan_files)
428
+
429
+ # Get athan audio
430
+ @app.api_route("/api/athan/{filename}", methods=["GET", "HEAD"], operation_id="getAthan")
431
+ async def get_athan(filename: str):
432
+ # Sanitize filename to prevent path traversal
433
+ safe_filename = Path(filename).name
434
+ if not safe_filename.endswith(".mp3"):
435
+ raise HTTPException(status_code=400, detail="Invalid file type")
436
+
437
+ if USE_HF_CDN:
438
+ try:
439
+ hf_filename = f"reciters/athan/{safe_filename}"
440
+ local_path = hf_hub_download(
441
+ repo_id=HF_DATASET_URL,
442
+ filename=hf_filename,
443
+ repo_type="dataset",
444
+ token=HF_TOKEN
445
+ )
446
+ return FileResponse(local_path, media_type="audio/mpeg")
447
+ except Exception as e:
448
+ print(f"Error fetching athan from HF CDN: {e}")
449
+ # Fall back to local
450
+ pass
451
+
452
+ # Try local file
453
+ athan_path = ATHAN_DIR / safe_filename
454
+ if athan_path.exists():
455
+ return FileResponse(athan_path, media_type="audio/mpeg")
456
+
457
+ raise HTTPException(status_code=404, detail="Athan file not found")
458
+
459
+ # Get list of available background audio files
460
+ @app.get("/api/background-audio")
461
+ async def get_background_audio_list():
462
+ background_files = []
463
+ if BACKGROUND_AUDIO_DIR.exists():
464
+ for file in BACKGROUND_AUDIO_DIR.iterdir():
465
+ if file.is_file() and file.suffix.lower() == ".mp3":
466
+ background_files.append(file.name)
467
+ return JSONResponse(content=background_files)
468
+
469
+ # Get background audio
470
+ @app.api_route("/api/background-audio/{filename}", methods=["GET", "HEAD"], operation_id="getBackgroundAudio")
471
+ async def get_background_audio(filename: str):
472
+ # Sanitize filename to prevent path traversal
473
+ safe_filename = Path(filename).name
474
+ if not safe_filename.endswith(".mp3"):
475
+ raise HTTPException(status_code=400, detail="Invalid file type")
476
+
477
+ if USE_HF_CDN:
478
+ try:
479
+ hf_filename = f"background_audio/{safe_filename}"
480
+ local_path = hf_hub_download(
481
+ repo_id=HF_DATASET_URL,
482
+ filename=hf_filename,
483
+ repo_type="dataset",
484
+ token=HF_TOKEN
485
+ )
486
+ return FileResponse(local_path, media_type="audio/mpeg")
487
+ except Exception as e:
488
+ print(f"Error fetching background audio from HF CDN: {e}")
489
+ # Fall back to local
490
+ pass
491
+
492
+ # Try local file
493
+ background_path = BACKGROUND_AUDIO_DIR / safe_filename
494
+ if background_path.exists():
495
+ return FileResponse(background_path, media_type="audio/mpeg")
496
+
497
+ raise HTTPException(status_code=404, detail="Background audio file not found")
498
+
499
  if __name__ == "__main__":
500
  import uvicorn
501
  uvicorn.run(app, host="0.0.0.0", port=8000)