Saqib commited on
Commit
ec7de72
1 Parent(s): 1d99973

Update modules/app.py

Browse files
Files changed (1) hide show
  1. modules/app.py +30 -1
modules/app.py CHANGED
@@ -1,4 +1,6 @@
1
  from fastapi import FastAPI, HTTPException, Request, Depends
 
 
2
  import aiohttp
3
  import base64
4
  import io
@@ -28,7 +30,34 @@ def generate_hash(length=12):
28
  @app.get("/")
29
  async def read_root():
30
  return {"message": "Saqib's API"}
31
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  @app.post("/whisper")
33
  async def whisper(request: Request):
34
  data = await request.json() # Extracting JSON data from request
 
1
  from fastapi import FastAPI, HTTPException, Request, Depends
2
+ from pytube import YouTube
3
+ import logging
4
  import aiohttp
5
  import base64
6
  import io
 
30
  @app.get("/")
31
  async def read_root():
32
  return {"message": "Saqib's API"}
33
+
34
+ @app.get("/get_audio")
35
+ async def get_audio(url: str):
36
+ if not url:
37
+ raise HTTPException(status_code=400, detail="URL is required")
38
+
39
+ try:
40
+ yt = YouTube(url)
41
+ video = yt.streams.filter(only_audio=True).first()
42
+ # out_file = video.download(output_path=".")
43
+ # current_dir
44
+ out_file = video.download()
45
+
46
+ file_stats = os.stat(out_file)
47
+ logging.info(f'Size of audio file in Bytes: {file_stats.st_size}')
48
+
49
+ if file_stats.st_size <= 30000000:
50
+ base, ext = os.path.splitext(out_file)
51
+ new_file = base + '.mp3'
52
+ os.rename(out_file, new_file)
53
+
54
+ return FileResponse(new_file, media_type="audio/mpeg", filename="audio.mp3")
55
+ else:
56
+ os.remove(out_file)
57
+ raise HTTPException(status_code=413, detail="Audio file is too large. Limited to about 1.5 hours.")
58
+ except Exception as e:
59
+ raise HTTPException(status_code=500, detail=str(e))
60
+
61
  @app.post("/whisper")
62
  async def whisper(request: Request):
63
  data = await request.json() # Extracting JSON data from request