feat: _is_direct_url — route YouTube/SoundCloud links to direct download
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
17
musicfetch
17
musicfetch
@@ -746,6 +746,23 @@ def _is_youtube_playlist_url(url: str) -> bool:
|
|||||||
return "list" in qs and "v" not in qs
|
return "list" in qs and "v" not in qs
|
||||||
|
|
||||||
|
|
||||||
|
_DIRECT_HOSTS = ("youtube.com", "youtu.be", "music.youtube.com",
|
||||||
|
"soundcloud.com", "api.soundcloud.com")
|
||||||
|
|
||||||
|
|
||||||
|
def _is_direct_url(url: str) -> bool:
|
||||||
|
"""True for links yt-dlp downloads well directly (YouTube, SoundCloud).
|
||||||
|
These skip Odesli resolution and use the existing handle_url path."""
|
||||||
|
if not is_url(url):
|
||||||
|
return False
|
||||||
|
host = (urlparse(url).hostname or "").lower()
|
||||||
|
if host.startswith("www."):
|
||||||
|
host = host[4:]
|
||||||
|
if host.endswith(("youtube.com", "youtu.be", "soundcloud.com")):
|
||||||
|
return True
|
||||||
|
return host in _DIRECT_HOSTS
|
||||||
|
|
||||||
|
|
||||||
def _ytmusic_playlist(pid: str) -> tuple[str, list[Hit]]:
|
def _ytmusic_playlist(pid: str) -> tuple[str, list[Hit]]:
|
||||||
"""Expand a YouTube Music playlist via ytmusicapi. Returns ("", []) on failure."""
|
"""Expand a YouTube Music playlist via ytmusicapi. Returns ("", []) on failure."""
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -66,3 +66,23 @@ def test_odesli_resolve_network_error_returns_none(monkeypatch):
|
|||||||
raise mf.RequestException("down")
|
raise mf.RequestException("down")
|
||||||
monkeypatch.setattr(mf.requests, "get", boom)
|
monkeypatch.setattr(mf.requests, "get", boom)
|
||||||
assert mf.odesli_resolve("https://open.spotify.com/track/abc") is None
|
assert mf.odesli_resolve("https://open.spotify.com/track/abc") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_direct_url_youtube():
|
||||||
|
assert mf._is_direct_url("https://music.youtube.com/watch?v=abc")
|
||||||
|
assert mf._is_direct_url("https://www.youtube.com/watch?v=abc")
|
||||||
|
assert mf._is_direct_url("https://youtu.be/abc")
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_direct_url_soundcloud():
|
||||||
|
assert mf._is_direct_url("https://soundcloud.com/dj/track")
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_direct_url_other_platforms_false():
|
||||||
|
assert not mf._is_direct_url("https://open.spotify.com/track/abc")
|
||||||
|
assert not mf._is_direct_url("https://music.apple.com/us/album/x/1?i=2")
|
||||||
|
assert not mf._is_direct_url("https://tidal.com/browse/track/123")
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_direct_url_youtube_playlist_true():
|
||||||
|
assert mf._is_direct_url("https://www.youtube.com/playlist?list=PLabc")
|
||||||
|
|||||||
Reference in New Issue
Block a user