I want a Spotify sleep timer on my PC

Introduction

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.

environment

$ sw_vers                                                                          
ProductName:    macOS
ProductVersion: 11.1
BuildVersion:   20C69

Client registration

  1. Go to Spotify for Developer
  2. DASHBOARD
  3. CREATE AN APP
  4. Fill in the client name and description

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.

Get username

  1. Access Spotipy
  2. Log in and go to Profile → Account
  3. Account information → user name

Make a note of the username as you will use it later.

Call API

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.

Timer function

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()

Summary

The sleep timer function has been created using the Spotify Web API. The entire source code is posted on github. For your information.

reference

Until writing a blog (Adding a song to a playlist from the Spotify API in Python (spotipy))

Recommended Posts

I want a Spotify sleep timer on my PC
I want a mox generator (2)
I installed Ubuntu on a USB stick on a dual boot PC
I want to announce my graduation thesis on IPython Notebook
I installed Linux on my Mac
I made a kitchen timer to be displayed on the status bar!
I get a firewall warning when I start gqlgen's server.go on my Mac
I want to format and check Python code to my liking on VS Code
I want to print in a comprehension
I want to build a Python environment
I want to use Linux on mac
I installed Kivy on a Mac environment
I built a TensorFlow environment on windows10
I want to develop Android apps on Android
I installed OpenCV-Python on my Raspberry Pi
I want to create a nice Python development environment for my new Mac
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I want to have recursion come to my mind
I want to easily create a Noise Model
I want to analyze songs with Spotify API 2
I want to INSERT a DataFrame into MSSQL
I want to create a window in Python
I want to make a game with Python
I don't want to take a coding test
I want to analyze songs with Spotify API 1
I want to make fits from my head
I want to create a plug-in type implementation
I want to do pyenv + pipenv on Windows
I want to easily find a delicious restaurant
I want to log file I / O on Linux
I'm a beginner, can I borrow my wisdom?
I want to write to a file with Python
Create a Python execution environment on IBM i
I did a little research on the class
I want to upload a Django app to heroku
I want to take a screenshot of the site on Docker using any font