Usage Examples¶
Here’s a quick example of how to use the yutipy package to search for a song:
Important
All examples here use the
withcontext manager to initialize an instance of the respective class, as those classes internally userequests.Session()for making requests to APIs. This approach ensures that the session is automatically closed once you exit the context. Although usingwithis not mandatory, if you instantiate an object without it, you are responsible for closing the session after use by calling theclose_session()method on that object.Following examples suggest to create
.envfile for storing, for example api keys. However, when hosting or deplyoing your application on production, it is suggested and you may store them in environment variable(s) provided by your hosting provider.
CLI Tool¶
You can use the CLI tool to search for music directly from the command line:
yutipy-cli "Rick Astley" "Never Gonna Give You Up" --service spotify
Deezer¶
from yutipy.deezer import Deezer
with Deezer() as deezer:
result = deezer.search("Artist Name", "Song Title")
print(result)
iTunes¶
from yutipy.itunes import Itunes
with Itunes() as itunes:
result = itunes.search("Artist Name", "Song Title")
print(result)
KKBOX¶
To use the KKBOX Open API, you need to set the KKBOX_CLIENT_ID and KKBOX_CLIENT_SECRET for KKBOX.
You can do this by creating a .env file in the root directory of your project with the following content:
.env
KKBOX_CLIENT_ID=<your_kkbox_client_id>
KKBOX_CLIENT_SECRET=<your_kkbox_client_secret>
Alternatively, you can manually provide these values when creating an object of the KKBox class:
from yutipy.kkbox import KKBox
kkbox = KKBox(client_id="your_kkbox_client_id", client_secret="your_kkbox_client_secret")
from yutipy.kkbox import KKBox
with KKBox() as kkbox:
result = kkbox.search("Artist Name", "Song Title")
print(result)
Lastfm¶
To use and retreive information from Lastfm, you need Lastfm API Key and need to set LASTFM_API_KEY.
You can do this by creating a .env file int the root directory of your project with the following content:
.env
LASTFM_API_KEY=<your_lastfm_api_key>
Alternatively, you can manually provide these values when creating an object of the LastFm class:
from yutipy.lastfm import LastFm
lastfm = LastFm(api_key="your_lastfm_api_key")
from yutipy.lastfm import LastFm
with LastFm() as lastfm:
result = lastfm.get_currently_playing(username="username")
print(result)
Spotify¶
Client Credentials Grant Type¶
To use the Spotify API with Client Credentials flow, you need to set the SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET for Spotify.
You can do this by creating a .env file in the root directory of your project with the following content:
.env
SPOTIFY_CLIENT_ID=<your_spotify_client_id>
SPOTIFY_CLIENT_SECRET=<your_spotify_client_secret>
Alternatively, you can manually provide these values when creating an object of the Spotify class:
from yutipy.spotify import Spotify
spotify = Spotify(client_id="your_spotify_client_id", client_secret="your_spotify_client_secret")
from yutipy.spotify import Spotify
with Spotify() as spotify:
result = spotify.search("Artist Name", "Song Title")
print(result)
OR, if you have the “ISRC” or “UPC” of the song, you can use the search_advanced method:
from yutipy.spotify import Spotify
with Spotify() as spotify:
# ISRC for "single" tracks & UPC for "album" tracks. Only one of them is required.
result = spotify.search_advanced("Artist Name", "Song Title", isrc="USAT29900609", upc="00602517078194")
print(result)
YouTube Music¶
from yutipy.musicyt import MusicYT
with MusicYT() as music_yt:
result = music_yt.search("Artist Name", "Song Title")
print(result)