I know that spotDL is a command-line application built in python, but is there any way to use it inside a script?
I know that spotDL is a command-line application built in python, but is there any way to use it inside a script?
You could use os.popen like this:
import os
os.popen("spotdl https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M")
Or os.system:
os.system("spotdl https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M")
Actually spotDL has got an API and here's the DOCS.
That's a basic example on what can you do based from one of the given examples.
from spotdl.command_line.core import Spotdl
args = {
"no_encode": True,
}
with Spotdl(args) as spotdl_handler:
spotdl_handler.download_track("https://open.spotify.com/track/2lfPecqFbH8X4lHSpTxt8l")
print("Downloading 2nd track.")
spotdl_handler.download_track("ncs spectre")
print("Downloading from file.")
spotdl_handler.download_tracks_from_file("file_full_of_tracks.txt")
The examples and links are for v2 of spotDL, using v3 (the common one you install with PyPI) will not contain these features.
You can download v2 from here.
You see, spotDL is a command-line application, and requires the use of command-line arguments. In python, there isn't a way (as of I know) to set these arguments.
I believe you are asking on how to use an import and use a function directly such as spotdl.download(spotifylink), however, the application is only designed for command-line use. So lets do that then!
Well, we can use the subprocess module to launch up spotdl via the python executable.
import subprocess
import sys # for sys.executable (The file path of the currently using python)
from spotdl import __main__ as spotdl # To get the location of spotdl
spotifylink = "Whatever you want to download"
subprocess.check_call([sys.executable, spotdl.__file__, spotifylink])
Edit: There is a better way to do it (Requires pytube)
from spotdl import __main__ as start # To initialize
from spotdl.search.songObj import SongObj
from pytube import YouTube
spotifylink = "Whatever you want to download"
song = SongObj.from_url(spotifylink)
url = song.get_youtube_link() # Yay you have a youtube link!
yt = YouTube(url)
yts = yt.steams.get_audio_only()
fname = yts.download()
print(f"Downloaded to {fname}")