Get spotify currently playing track
The Spotify client on Linux implements a D-Bus interface called MPRIS - Media Player Remote Interfacing Specification.
http://specifications.freedesktop.org/mpris-spec/latest/index.html
You could access the title (and other metadata) from python like this:
import dbus
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2")
spotify_properties = dbus.Interface(spotify_bus,
"org.freedesktop.DBus.Properties")
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
# The property Metadata behaves like a python dict
for key, value in metadata.items():
print(key, value)
# To just print the title
print(metadata['xesam:title'])
How do I get the audio features of current playing track with Spotify Web API?
setInterval(function(){
callAjax();
}, 1000);
var apiData;
var audioData;
var callAjax = function(){
$.ajax({
url: 'https://api.spotify.com/v1/me/player/currently-playing',
headers: {
'Authorization': 'Bearer ' + access_token
},
success: function(response) {
currentPlayingPlaceholder.innerHTML = currentPlayingTemplate(response);
apiData = response;
console.log(response);
$('#login').hide();
$('#loggedin').show();
}
});
if(apiData != undefined){
$.ajax({
url: "https://api.spotify.com/v1/audio-features/" + apiData.item.id,
//url: "https://api.spotify.com/v1/audio-features/06AKEBrKUckW0KREUWRnvT",
headers: {
'Authorization': 'Bearer ' + access_token
},
success: function(response) {
audioFeaturesPlaceholder.innerHTML = audioFeaturesTemplate(response);
//audioData = response;
console.log(response);
$('#login').hide();
$('#loggedin').show();
}
});
}
I made a variable called apiData. Then you can identify the id of currently playing track by using item.id
See this screenshot of the console log:
enter image description here
You can see a subfolder 'item' and in that folder you can find 'id'.
So I replaced this: url: "https://api.spotify.com/v1/audio-features/06AKEBrKUckW0KREUWRnvT"
For this: url: "https://api.spotify.com/v1/audio-features/" + apiData.item.id
It seems to work, maybe it can help others.
Apple Music API Get Currently Playing Song
Unfortunately their API is pretty lame. If you not play anything at the moment you still got a song that was played recently even if that recently was a day ago. Even worse there is no unixdate attached result so you don't know when the song was last accessed.
How to get Spotify current playing song in Python (Windows)?
I encountered the same issue, so I wrote a library to solve this issue. The library can be found at github: https://github.com/XanderMJ/spotilib. Keep in mind that this is still work in progress.
Just copy the file and place it in your Python/Lib directory.
import spotilib
spotilib.artist() #returns the artist of the current playing song
spotilib.song() #returns the song title of the current playing song
spotilib.artist() returns only the first artist. I started working on an other library spotimeta.py to solve this issue. However, this is not working at 100% yet.
import spotimeta
spotimeta.artists() #returns a list of all the collaborating artists of the track
If an error occurs, spotimeta.artists() will return only the first artist (found with spotilib.artist())
Hope this will help you (if still needed)!
Getting currently playing song with Spotipy gives error: 'Spotify' object has no attribute 'currently_playing'
Even if PyPi claims that the version of Spotipy is the latest there, 2.4.4, in fact, it is not. I noticed that after installing Spotipy with pip, its source code is different from the head of the master branch on the GitHub. And the PyPi version doesn't have the currently_playing
method.
What worked for me is to uninstall Spotipy by running pip uninstall spotipy
and to install it again directly from GitHub:
pip install git+https://github.com/plamere/spotipy.git@master
Related Topics
Compare Two Files for Differences in Python
How to Open (Read-Write) or Create a File with Truncation Allowed
What Is the Python Equivalent for a Case/Switch Statement
Removing Elements That Have Consecutive Duplicates
Determine Whether Integer Is Between Two Other Integers
Matplotlib Different Size Subplots
Get a Variable from the Url in a Flask Route
Why Does Python Code Run Faster in a Function
Downloading a Picture via Urllib and Python
Python None Comparison: Should I Use "Is" or ==
Magicexception:File 5.41 Supports Only Version 16 Magic File, Magic.Mgc Is Version 14
Unicodedecodeerror, Invalid Continuation Byte
Extracting Just Month and Year Separately from Pandas Datetime Column
Why Does Id({}) == Id({}) and Id([]) == Id([]) in Cpython
What Does the _File_ Variable Mean/Do
Best Way to Replace Multiple Characters in a String