Search code examples
pythonpython-3.xspotify

How can I use spotDL inside python?


I know that spotDL is a command-line application built in python, but is there any way to use it inside a script?


Solution

  • Short answer: Yes

    Long answer: Yes

    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!

    How?

    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}")