taslim19
commited on
Commit
·
f11256b
1
Parent(s):
72f2bb7
fix: Use correct error handler in Youtube.py
Browse files- DragMusic/platforms/Youtube.py +17 -17
DragMusic/platforms/Youtube.py
CHANGED
@@ -10,14 +10,14 @@ from youtubesearchpython.__future__ import VideosSearch
|
|
10 |
|
11 |
from DragMusic.utils.database import is_on_off
|
12 |
from DragMusic.utils.downloader import yt_dlp_download, download_audio_concurrent
|
13 |
-
from DragMusic.utils.errors import
|
14 |
from DragMusic.utils.formatters import time_to_seconds
|
15 |
|
16 |
cookies_file = "cookies.txt"
|
17 |
_cache = {}
|
18 |
|
19 |
|
20 |
-
@
|
21 |
async def shell_cmd(cmd: str) -> str:
|
22 |
proc = await asyncio.create_subprocess_shell(
|
23 |
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
@@ -26,7 +26,7 @@ async def shell_cmd(cmd: str) -> str:
|
|
26 |
return (out or err).decode()
|
27 |
|
28 |
|
29 |
-
@
|
30 |
async def cached_youtube_search(query: str) -> List[Dict]:
|
31 |
if query in _cache:
|
32 |
return _cache[query]
|
@@ -53,11 +53,11 @@ class YouTubeAPI:
|
|
53 |
link = self.base_url + link.split("/")[-1].split("?")[0]
|
54 |
return link.split("&")[0]
|
55 |
|
56 |
-
@
|
57 |
async def exists(self, link: str, videoid: Union[str, bool, None] = None) -> bool:
|
58 |
return bool(self._url_pattern.search(self._prepare_link(link, videoid)))
|
59 |
|
60 |
-
@
|
61 |
async def url(self, message: Message) -> Optional[str]:
|
62 |
msgs = [message] + ([message.reply_to_message] if message.reply_to_message else [])
|
63 |
for msg in msgs:
|
@@ -70,7 +70,7 @@ class YouTubeAPI:
|
|
70 |
return ent.url
|
71 |
return None
|
72 |
|
73 |
-
@
|
74 |
async def _fetch_video_info(self, query: str, *, use_cache: bool = True) -> Optional[Dict]:
|
75 |
if use_cache and not query.startswith("http"):
|
76 |
result = await cached_youtube_search(query)
|
@@ -79,7 +79,7 @@ class YouTubeAPI:
|
|
79 |
result = (await search.next()).get("result", [])
|
80 |
return result[0] if result else None
|
81 |
|
82 |
-
@
|
83 |
async def is_live(self, link: str) -> bool:
|
84 |
prepared = self._prepare_link(link)
|
85 |
proc = await asyncio.create_subprocess_exec(
|
@@ -95,7 +95,7 @@ class YouTubeAPI:
|
|
95 |
except json.JSONDecodeError:
|
96 |
return False
|
97 |
|
98 |
-
@
|
99 |
async def details(self, link: str, videoid: Union[str, bool, None] = None) -> Tuple[str, Optional[str], int, str, str]:
|
100 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
101 |
if not info:
|
@@ -111,22 +111,22 @@ class YouTubeAPI:
|
|
111 |
info.get("id", ""),
|
112 |
)
|
113 |
|
114 |
-
@
|
115 |
async def title(self, link: str, videoid: Union[str, bool, None] = None) -> str:
|
116 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
117 |
return info.get("title", "") if info else ""
|
118 |
|
119 |
-
@
|
120 |
async def duration(self, link: str, videoid: Union[str, bool, None] = None) -> Optional[str]:
|
121 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
122 |
return info.get("duration") if info else None
|
123 |
|
124 |
-
@
|
125 |
async def thumbnail(self, link: str, videoid: Union[str, bool, None] = None) -> str:
|
126 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
127 |
return (info.get("thumbnail") or info.get("thumbnails", [{}])[0].get("url", "")).split("?")[0] if info else ""
|
128 |
|
129 |
-
@
|
130 |
async def video(self, link: str, videoid: Union[str, bool, None] = None) -> Tuple[int, str]:
|
131 |
link = self._prepare_link(link, videoid)
|
132 |
proc = await asyncio.create_subprocess_exec(
|
@@ -136,7 +136,7 @@ class YouTubeAPI:
|
|
136 |
stdout, stderr = await proc.communicate()
|
137 |
return (1, stdout.decode().split("\n")[0]) if stdout else (0, stderr.decode())
|
138 |
|
139 |
-
@
|
140 |
async def playlist(self, link: str, limit: int, user_id, videoid: Union[str, bool, None] = None) -> List[str]:
|
141 |
if videoid:
|
142 |
link = self.playlist_url + str(videoid)
|
@@ -148,7 +148,7 @@ class YouTubeAPI:
|
|
148 |
data = await shell_cmd(cmd)
|
149 |
return [item for item in data.strip().split("\n") if item]
|
150 |
|
151 |
-
@
|
152 |
async def track(self, link: str, videoid: Union[str, bool, None] = None) -> Tuple[Dict, str]:
|
153 |
try:
|
154 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
@@ -178,7 +178,7 @@ class YouTubeAPI:
|
|
178 |
}
|
179 |
return details, info.get("id", "")
|
180 |
|
181 |
-
@
|
182 |
async def formats(self, link: str, videoid: Union[str, bool, None] = None) -> Tuple[List[Dict], str]:
|
183 |
link = self._prepare_link(link, videoid)
|
184 |
opts = {"quiet": True, "cookiefile": cookies_file}
|
@@ -202,7 +202,7 @@ class YouTubeAPI:
|
|
202 |
print(f"[formats()] yt-dlp error: {e}")
|
203 |
return formats, link
|
204 |
|
205 |
-
@
|
206 |
async def slider(self, link: str, query_type: int, videoid: Union[str, bool, None] = None) -> Tuple[str, Optional[str], str, str]:
|
207 |
search = VideosSearch(self._prepare_link(link, videoid), limit=10)
|
208 |
results = (await search.next()).get("result", [])
|
@@ -216,7 +216,7 @@ class YouTubeAPI:
|
|
216 |
res.get("id", ""),
|
217 |
)
|
218 |
|
219 |
-
@
|
220 |
async def download(
|
221 |
self,
|
222 |
link: str,
|
|
|
10 |
|
11 |
from DragMusic.utils.database import is_on_off
|
12 |
from DragMusic.utils.downloader import yt_dlp_download, download_audio_concurrent
|
13 |
+
from DragMusic.utils.errors import capture_err
|
14 |
from DragMusic.utils.formatters import time_to_seconds
|
15 |
|
16 |
cookies_file = "cookies.txt"
|
17 |
_cache = {}
|
18 |
|
19 |
|
20 |
+
@capture_err
|
21 |
async def shell_cmd(cmd: str) -> str:
|
22 |
proc = await asyncio.create_subprocess_shell(
|
23 |
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
|
|
26 |
return (out or err).decode()
|
27 |
|
28 |
|
29 |
+
@capture_err
|
30 |
async def cached_youtube_search(query: str) -> List[Dict]:
|
31 |
if query in _cache:
|
32 |
return _cache[query]
|
|
|
53 |
link = self.base_url + link.split("/")[-1].split("?")[0]
|
54 |
return link.split("&")[0]
|
55 |
|
56 |
+
@capture_err
|
57 |
async def exists(self, link: str, videoid: Union[str, bool, None] = None) -> bool:
|
58 |
return bool(self._url_pattern.search(self._prepare_link(link, videoid)))
|
59 |
|
60 |
+
@capture_err
|
61 |
async def url(self, message: Message) -> Optional[str]:
|
62 |
msgs = [message] + ([message.reply_to_message] if message.reply_to_message else [])
|
63 |
for msg in msgs:
|
|
|
70 |
return ent.url
|
71 |
return None
|
72 |
|
73 |
+
@capture_err
|
74 |
async def _fetch_video_info(self, query: str, *, use_cache: bool = True) -> Optional[Dict]:
|
75 |
if use_cache and not query.startswith("http"):
|
76 |
result = await cached_youtube_search(query)
|
|
|
79 |
result = (await search.next()).get("result", [])
|
80 |
return result[0] if result else None
|
81 |
|
82 |
+
@capture_err
|
83 |
async def is_live(self, link: str) -> bool:
|
84 |
prepared = self._prepare_link(link)
|
85 |
proc = await asyncio.create_subprocess_exec(
|
|
|
95 |
except json.JSONDecodeError:
|
96 |
return False
|
97 |
|
98 |
+
@capture_err
|
99 |
async def details(self, link: str, videoid: Union[str, bool, None] = None) -> Tuple[str, Optional[str], int, str, str]:
|
100 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
101 |
if not info:
|
|
|
111 |
info.get("id", ""),
|
112 |
)
|
113 |
|
114 |
+
@capture_err
|
115 |
async def title(self, link: str, videoid: Union[str, bool, None] = None) -> str:
|
116 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
117 |
return info.get("title", "") if info else ""
|
118 |
|
119 |
+
@capture_err
|
120 |
async def duration(self, link: str, videoid: Union[str, bool, None] = None) -> Optional[str]:
|
121 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
122 |
return info.get("duration") if info else None
|
123 |
|
124 |
+
@capture_err
|
125 |
async def thumbnail(self, link: str, videoid: Union[str, bool, None] = None) -> str:
|
126 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
127 |
return (info.get("thumbnail") or info.get("thumbnails", [{}])[0].get("url", "")).split("?")[0] if info else ""
|
128 |
|
129 |
+
@capture_err
|
130 |
async def video(self, link: str, videoid: Union[str, bool, None] = None) -> Tuple[int, str]:
|
131 |
link = self._prepare_link(link, videoid)
|
132 |
proc = await asyncio.create_subprocess_exec(
|
|
|
136 |
stdout, stderr = await proc.communicate()
|
137 |
return (1, stdout.decode().split("\n")[0]) if stdout else (0, stderr.decode())
|
138 |
|
139 |
+
@capture_err
|
140 |
async def playlist(self, link: str, limit: int, user_id, videoid: Union[str, bool, None] = None) -> List[str]:
|
141 |
if videoid:
|
142 |
link = self.playlist_url + str(videoid)
|
|
|
148 |
data = await shell_cmd(cmd)
|
149 |
return [item for item in data.strip().split("\n") if item]
|
150 |
|
151 |
+
@capture_err
|
152 |
async def track(self, link: str, videoid: Union[str, bool, None] = None) -> Tuple[Dict, str]:
|
153 |
try:
|
154 |
info = await self._fetch_video_info(self._prepare_link(link, videoid))
|
|
|
178 |
}
|
179 |
return details, info.get("id", "")
|
180 |
|
181 |
+
@capture_err
|
182 |
async def formats(self, link: str, videoid: Union[str, bool, None] = None) -> Tuple[List[Dict], str]:
|
183 |
link = self._prepare_link(link, videoid)
|
184 |
opts = {"quiet": True, "cookiefile": cookies_file}
|
|
|
202 |
print(f"[formats()] yt-dlp error: {e}")
|
203 |
return formats, link
|
204 |
|
205 |
+
@capture_err
|
206 |
async def slider(self, link: str, query_type: int, videoid: Union[str, bool, None] = None) -> Tuple[str, Optional[str], str, str]:
|
207 |
search = VideosSearch(self._prepare_link(link, videoid), limit=10)
|
208 |
results = (await search.next()).get("result", [])
|
|
|
216 |
res.get("id", ""),
|
217 |
)
|
218 |
|
219 |
+
@capture_err
|
220 |
async def download(
|
221 |
self,
|
222 |
link: str,
|