randydev commited on
Commit
efa8d1f
β€’
1 Parent(s): 29bec71

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +68 -19
main.py CHANGED
@@ -155,13 +155,40 @@ UPLOAD_DIRECTORY = "./uploads"
155
 
156
  class YouTubeBase(BaseModel):
157
  link: str
 
 
 
 
 
158
 
159
  def secs_to_mins(secs: int) -> str:
160
  mins, secs = divmod(secs, 60)
161
  return f"{mins}:{secs}"
162
 
163
- @app.post("/akeno/youtube-video", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
164
- async def youtube_video(payload: YouTubeBase):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  status, url = YoutubeDriver.check_url(payload.link)
166
  if not status:
167
  return SuccessResponse(
@@ -169,23 +196,45 @@ async def youtube_video(payload: YouTubeBase):
169
  randydev={"error": url}
170
  )
171
  try:
172
- with YoutubeDL(YoutubeDriver.video_options()) as ytdl:
173
- yt_data = ytdl.extract_info(url, download=True)
174
- yt_file = yt_data["id"]
175
- time_second = time.time()
176
- with open(f"{yt_file}.mp4", "rb") as video_file:
177
- encoded_string = base64.b64encode(video_file.read())
178
- return SuccessResponse(
179
- status="True",
180
- randydev={
181
- "video_data": encoded_string,
182
- "title": yt_data['title'],
183
- "views": yt_data['view_count'],
184
- "duration": secs_to_mins(int(yt_data['duration'])),
185
- "thumbnail": f"https://i.ytimg.com/vi/{yt_data['id']}/hqdefault.jpg",
186
- "time_second": time_second
187
- }
188
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  except Exception as e:
190
  return SuccessResponse(
191
  status="False",
 
155
 
156
  class YouTubeBase(BaseModel):
157
  link: str
158
+ only_audio: bool = False
159
+
160
+ class YouTubeSearch(BaseModel):
161
+ query: str
162
+ limit: int = 7
163
 
164
  def secs_to_mins(secs: int) -> str:
165
  mins, secs = divmod(secs, 60)
166
  return f"{mins}:{secs}"
167
 
168
+ @app.get("/akeno/youtube-search", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
169
+ async def youtube_search(payload: YouTubeSearch):
170
+ try:
171
+ results = YoutubeDriver(payload.query, payload.limit).to_dict()
172
+ except Exception as e:
173
+ return SuccessResponse(
174
+ status="False",
175
+ randydev={"error": str(e)}
176
+ )
177
+ if not results:
178
+ return SuccessResponse(
179
+ status="False",
180
+ randydev={"error": "No results found."}
181
+ )
182
+ text = f"**πŸ”Ž π–³π—ˆπ—π–Ίπ—… π–±π–Ύπ—Œπ—Žπ—…π—π—Œ π–₯π—ˆπ—Žπ—‡π–½:** `{len(results)}`\n\n"
183
+ for result in results:
184
+ text += f"**𝖳𝗂𝗍𝗅𝖾:** `{result['title'][:50]}`\n**𝖒𝗁𝖺𝗇𝗇𝖾𝗅:** `{result['channel']}`\n**π–΅π—‚π–Ύπ—π—Œ:** `{result['views']}`\n**π–£π—Žπ—‹π–Ίπ—π—‚π—ˆπ—‡:** `{result['duration']}`\n**𝖫𝗂𝗇𝗄:** `https://youtube.com{result['url_suffix']}`\n\n"
185
+ return SuccessResponse(
186
+ status="True",
187
+ randydev={"results": text}
188
+ )
189
+
190
+ @app.post("/akeno/youtube", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
191
+ async def youtube_api(payload: YouTubeBase):
192
  status, url = YoutubeDriver.check_url(payload.link)
193
  if not status:
194
  return SuccessResponse(
 
196
  randydev={"error": url}
197
  )
198
  try:
199
+ if payload.only_audio:
200
+ with YoutubeDL(YoutubeDriver.song_options()) as ytdl:
201
+ yt_data = ytdl.extract_info(url, download=True)
202
+ yt_file = ytdl.prepare_filename(yt_data)
203
+ ytdl.process_info(yt_data)
204
+ time_second = time.time()
205
+ with open(f"{yt_file}.mp3", "rb") as audio_file:
206
+ encoded_string = base64.b64encode(audio_file.read())
207
+ return SuccessResponse(
208
+ status="True",
209
+ randydev={
210
+ "video_data": encoded_string,
211
+ "channel": yt_data['channel'],
212
+ "title": yt_data['title'],
213
+ "views": yt_data['view_count'],
214
+ "duration": secs_to_mins(int(yt_data['duration'])),
215
+ "thumbnail": f"https://i.ytimg.com/vi/{yt_data['id']}/hqdefault.jpg",
216
+ "time_second": time_second
217
+ }
218
+ )
219
+ else:
220
+ with YoutubeDL(YoutubeDriver.video_options()) as ytdl:
221
+ yt_data = ytdl.extract_info(url, download=True)
222
+ yt_file = yt_data["id"]
223
+ time_second = time.time()
224
+ with open(f"{yt_file}.mp4", "rb") as video_file:
225
+ encoded_string = base64.b64encode(video_file.read())
226
+ return SuccessResponse(
227
+ status="True",
228
+ randydev={
229
+ "video_data": encoded_string,
230
+ "channel": yt_data['channel'],
231
+ "title": yt_data['title'],
232
+ "views": yt_data['view_count'],
233
+ "duration": secs_to_mins(int(yt_data['duration'])),
234
+ "thumbnail": f"https://i.ytimg.com/vi/{yt_data['id']}/hqdefault.jpg",
235
+ "time_second": time_second
236
+ }
237
+ )
238
  except Exception as e:
239
  return SuccessResponse(
240
  status="False",