Before going to bed, I sleep on my PC with Spotify music. However, I was worried that it would keep ringing until morning, so I checked if there was a Spotify sleep timer on my PC, but there was only an article on iPhone and Android, and I could not find one for PC, so I used Spotify Web API in Python. I decided to make it using the usable library spotipy.
$ sw_vers
ProductName: macOS
ProductVersion: 11.1
BuildVersion: 20C69
You can create a client by following the above steps. It's easy. Make a note of the Client ID and Client Secret as you will use them later.
Make a note of the username as you will use it later.
From here, I will write a program in Python. First, install the library to use the Spotify Web API with Python.
$ pip install spotipy
I will write while looking at the Official Reference of spotipy.
For scope
, look at the Spotify Web API Reference (https://developer.spotify.com/documentation/general/guides/scopes/#streaming) and select the features you are likely to use.
# -*- coding: utf-8 -*-
import spotipy
from spotipy import Spotify, SpotifyOAuth
# Settings
client_id: str = "Client ID"
client_secret: str = "Client Secret"
username: str = "Username"
scope: str = "user-modify-playback-state user-read-currently-playing"
redirect_uri: str = "http://localhost:8080"
auth_manager: SpotifyOAuth = spotipy.SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=scope,
username=username,
)
spotify: Spotify = spotipy.Spotify(auth_manager=auth_manager)
spotify.pause_playback()
If you run the program while playing music on Spotify, the music will stop playing.
In the above program, it will be executed and stopped immediately, so implement the timer function. Also, if you call pause_playback ()
while it is not playing, an error will be returned, so check if it is playing or by calling currently_playing ()
.
The timer function is easy if you use datetime.timedelta of the python default library. It also allows you to receive input from command line arguments.
# -*- coding: utf-8 -*-
import sys
from time import sleep
from datetime import datetime, timedelta
import spotipy
from spotipy import Spotify, SpotifyOAuth
client_id: str = "Client ID"
client_secret: str = "Client Secret"
username: str = "Username"
scope: str = "user-modify-playback-state user-read-currently-playing"
redirect_uri: str = "http://localhost:8080"
auth_manager: SpotifyOAuth = spotipy.SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=scope,
username=username,
)
spotify: Spotify = spotipy.Spotify(auth_manager=auth_manager)
start_time: datetime = datetime.now()
stop_time: timedelta = timedelta(seconds=int(sys.argv[1]))
while True:
elapsed_time: timedelta = datetime.now() - start_time
if elapsed_time > stop_time:
break
sleep(1)
is_playing: bool = response['is_playing'] if (response := spotify.currently_playing()) is not None else False
if is_playing:
spotify.pause_playback()
The sleep timer function has been created using the Spotify Web API. The entire source code is posted on github. For your information.
Until writing a blog (Adding a song to a playlist from the Spotify API in Python (spotipy))
Recommended Posts