Skip to main content

Command Palette

Search for a command to run...

Like current song on Spotify

Published
4 min read

Other Languages:

  • Jeżeli chcesz zobaczyć ten post po polsku to jest tutaj: Link
  • Om du vill se detta inlägg på svenska gå in här: Länk

The Problem

Spotify Client lacks one sometimes important feature for me. Let's say you listening to some random playlist you found or weekly discovery playlist of Spotify. You listening along and you like some song. Then you have to through UI click heart to add it to your liked songs. But if you currently doing something and just listening to Spotify in background this is super annoying.

The Solution

The best solution would be to add this feature to Spotify Client but feature requests like this: https://community.spotify.com/t5/Closed-Ideas/Desktop-Keyboard-shortcut-to-add-song-to-liked-songs/idi-p/4960129 don't go too far. So I made temporary solution through Python script and in my exact case Razer Synapse. But you could make your keyboard shortcut another way.

Script

Let's make this script. Firstly we need to make Spotify API App through this dashboard. In settings of App add this url to Redirect URIs section: http://localhost:7777/callback. From this dashboard we need Client ID and Client Secret you can copy that to your script because we will need it soon. Ok time to create python script. Let's start from installing requirements. We need one package to make this script called spotipy. We can install it through pip3 install spotipy. After that we create file for our script. And now let's start with writing imports.

from spotipy import Spotify, SpotifyOAuth, SpotifyException

First we importing Spotify API Client, Secondly we are importing Authentication Manager for API and at the end we are importing Exception for error handling. Now we need to declare few variables.

client_id = ''
client_secret = ''
redirect_uri = 'http://localhost:7777/callback'
scope = 'user-library-modify,user-read-currently-playing'

First variables is for our Spotify App Client ID and Client Secret. For use to authorize with API. Afterwards we have to variables for OAuth authorization. Firstly we have redirect_uri which our Authorization Manager will use to redirect OAuth redirect and get token. Secondly we have scope for token. In this script we use user-library-modify to add like for song and user-read-currently-playing to get what is current song we are listening to. Now it's time for logic of this script.

auth_manager = SpotifyOAuth(scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)

Firstly we need our authentication manager.

sc = Spotify(ouath_manager=auth_manager)

Afterwards we need our API client.

playing = sc.currently_playing()
if "item" not in playing or "id" not in playing["item"]:
    print("Error: Currently not playing anything")
    exit(255)
playing_id = playing["item"]["id"]

Now we need id of song which is currently playing.

try:
    r = sc.current_user_saved_tracks_add([playing_id])
    print(r)
except SpotifyException as e:
    print("Error: Song not found or something else")
    print(e)

Now we are trying to add song to liked and catch exception if it will fail.

Now we need to try this script in practice. Just listen to random song you don't have as liked. And run script. If it's after few seconds be shown as liked that means that this script worked.

My Setup for Windows with Razer Synapse

Now how I made shortcut for it. I made 2 additional scripts. First named start.bat with that content:

@echo off
push $~dp0
python .\main.py
popd
exit 0

And second named start.vbs with this content:

Set WshShell = CreateObject("WScript.Shell")
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim sScriptDir : sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
WshShell.Run chr(34) & sScriptDir & "/start.bat" & Chr(34), 0
Set WshShell = Nothing

First script is just to run python script. If you named it differently then change name in this script. Second script is to run start.bat file without command prompt window at all. So you will not spawn windows for liking song and getting focused out of active window.

Now in Razer Synapse i added Launch Program selected start.vbs for HyperShift + L on my keyboard.

I hope this script will help you.

Full Scripts

main.py

from spotipy import Spotify, SpotifyOAuth, SpotifyException

client_id = ''
client_secret = ''
redirect_uri = 'http://localhost:7777/callback'
scope = 'user-library-modify,user-read-currently-playing'

auth_manager = SpotifyOAuth(scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
sc = Spotify(oauth_manager=auth_manager)
playing = sc.currently_playing()
if "item" not in playing or "id" not in playing["item"]:
    print("Error: Currently not playing anything")
    exit(255)
playing_id = playing["item"]["id"]
try:
    r = sc.current_user_saved_tracks_add([playing_id])
    print(r)
except SpotifyException as e:
    print("Error: Song not found or something else")
    print(e)

start.bat

@echo off
pushd %~dp0
python .\main.py
popd
exit 0

start.vbs

Set WshShell = CreateObject("WScript.Shell") 
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim sScriptDir : sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
WshShell.Run chr(34) & sScriptDir & "/start.bat" & Chr(34), 0
Set WshShell = Nothing

Today I Scripted

Part 1 of 1

This is series with small scripts that i made when i was annoyed enough of something.

More from this blog

D

Development Journey of Patryk Adamczyk

6 posts

Hi! My name is Patryk. I trying to find good solutions for every problem on my way. I'm creator of PAiP Web. Open Team for every one wanting to make good solutions to make programming easier.