randydev commited on
Commit
b449f33
1 Parent(s): ea2741e

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +22 -10
main.py CHANGED
@@ -30,6 +30,7 @@ import io
30
  import string
31
  import openai
32
  import uuid
 
33
  from io import BytesIO
34
  from datetime import datetime as dt
35
  from dotenv import load_dotenv
@@ -154,9 +155,13 @@ UPLOAD_DIRECTORY = "./uploads"
154
  class YouTubeBase(BaseModel):
155
  link: str
156
 
157
- @app.post("/akeno/youtube-video")
158
- async def youtube_video(link: str):
159
- status, url = YoutubeDriver.check_url(link)
 
 
 
 
160
  if not status:
161
  return SuccessResponse(
162
  status="False",
@@ -165,14 +170,21 @@ async def youtube_video(link: str):
165
  try:
166
  with YoutubeDL(YoutubeDriver.video_options()) as ytdl:
167
  yt_data = ytdl.extract_info(url, download=True)
168
- yt_file = ytdl.prepare_filename(yt_data)
169
- if not os.path.exists(yt_file):
170
- return SuccessResponse(
171
- status="False",
172
- randydev={"error": "Video download failed"}
 
 
 
 
 
 
 
 
 
173
  )
174
- file_stream = open(yt_file, "rb")
175
- return StreamingResponse(file_stream, media_type="video/mp4")
176
  except Exception as e:
177
  return SuccessResponse(
178
  status="False",
 
30
  import string
31
  import openai
32
  import uuid
33
+ import time
34
  from io import BytesIO
35
  from datetime import datetime as dt
36
  from dotenv import load_dotenv
 
155
  class YouTubeBase(BaseModel):
156
  link: str
157
 
158
+ def secs_to_mins(secs: int) -> str:
159
+ mins, secs = divmod(secs, 60)
160
+ return f"{mins}:{secs}"
161
+
162
+ @app.post("/akeno/youtube-video", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
163
+ async def youtube_video(payload: YouTubeBase):
164
+ status, url = YoutubeDriver.check_url(payload.link)
165
  if not status:
166
  return SuccessResponse(
167
  status="False",
 
170
  try:
171
  with YoutubeDL(YoutubeDriver.video_options()) as ytdl:
172
  yt_data = ytdl.extract_info(url, download=True)
173
+ yt_file = yt_data["id"]
174
+ time_second = time.time()
175
+ with open(f"{yt_file}.mp4", "rb") as video_file:
176
+ encoded_string = base64.b64encode(video_file.read())
177
+ return SuccessResponse(
178
+ status="True",
179
+ randydev={
180
+ "video_data": encoded_string,
181
+ "title": yt_data['title'],
182
+ "views": yt_data['view_count'],
183
+ "duration": secs_to_mins(int(yt_data['duration'])),
184
+ "thumbnail": f"https://i.ytimg.com/vi/{yt_data['id']}/hqdefault.jpg",
185
+ "time_second": time_second
186
+ }
187
  )
 
 
188
  except Exception as e:
189
  return SuccessResponse(
190
  status="False",