Spaces:
Running
Running
Upload main.py
Browse files
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.
|
164 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
"
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
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",
|