Skip to content

Scripting with the streamrip library, before v2.0

Nathan Thomas edited this page Jan 10, 2024 · 1 revision

If you want to make a script of your own that downloads music, the streamrip library makes it super simple! The rip command that is included in the streamrip package is just one way that the library can be used.

To get started, let's make a script that asks for a Deezer url as input, and downloads the item. First, create a file named deezer_downloader.py, and import streamrip.

# deezer_downloader.py
import streamrip  # version 0.7 or greater

This will give you access to the streamrip API, which is a set of exposed classes and functions that help you download items. There are two types of classes that you need to be familiar with: Clients and Media. Client classes communicate with with the service (in this case, Deezer) API to retrieve information such as item metadata and streaming urls. Media classes parse the information returned by Clients into a universal form that can be downloaded.

The first part of our script will ask the user for a deezer url, such as https://www.deezer.com/en/album/240155022. We can use the builtin input function for this.

# deezer_downloader.py
url = input("Please enter a deezer album url: ")

The url contains two important bits of information: the type of media (album) and the item id (240155022). For now, we will only support album urls. Let's extract the relevant information:

# deezer_downloader.py
split_url = url.split("/")
item_type = split_url[-2]  # second to last item

if item_type != "album":
    print("Only album urls are allowed!")
    exit()


album_id = split_url[-1] # last item

To download Deezer items, we need to use the DeezerClient, which is part of the streamrip.clients module. To handle the information returned by our DeezerClient, we will use the Album class from streamrip.media. The Album will need access to the client and the album id to do anything useful.

my_deezer_client = streamrip.clients.DeezerClient()
my_album = streamrip.media.Album(client=my_deezer_client, id=album_id)

Now that we have our Client and Album objects, we can actually make requests. First let's load information about the album using the load_meta method of Album. This will request all sorts of stuff from the Deezer API, such as the tracks in the album, the album artist, the links to the cover art, etc.

my_album.load_meta()

Finally, we can download the album using the download method.

my_album.download()

That's all! The format is almost identical for other services, except that you need to use the login method on the Client for Qobuz and Tidal.

Full Script:

# deezer_downloader.py
import streamrip  # version 0.7 or greater

url = input("Please enter a deezer album url: ")
split_url = url.split("/")
item_type = split_url[-2]  # second to last item

if item_type != "album":
    print("Only album urls are allowed!")
    exit()

album_id = split_url[-1] # last item

my_deezer_client = streamrip.clients.DeezerClient()
my_album = streamrip.media.Album(client=my_deezer_client, id=album_id)
my_album.load_meta()
my_album.download()

Authentication

To authenticate a client, the login method of the Client must be used. This documents the parameters you need to pass for each source.

Qobuz

  • email: The email used for the account
  • pwd: The md5 hash of the account password.
  • (optional) app_id: If this is passed in, QobuzClient won't scrape it again.
  • (optional) secrets: If this is passed in, QobuzClient won't scrape it again.

TIDAL

These are all fetched by the client, but can be passed in to save time.

  • user_id
  • country_code
  • access_token
  • refresh_token

Deezer

Clone this wiki locally