Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Created April 29, 2016 09:43
Show Gist options
  • Save loretoparisi/6092634d34e97a062029b078215b6bdc to your computer and use it in GitHub Desktop.
Save loretoparisi/6092634d34e97a062029b078215b6bdc to your computer and use it in GitHub Desktop.
Listen Spotify MacOS notifications via Cocoa NSDistributedNotificationCenter
'''
Listen Spotify MacOS App for Notifications
It currently supports
`com.spotify.client.PlaybackStateChanged`
@author: loretoparisi at gmail dot com
'''
import Foundation
import json
from AppKit import *
from PyObjCTools import AppHelper
class GetSongs(NSObject):
def getMySongs_(self, song):
song_details = {}
ui = song.userInfo()
song_details = dict(zip(ui.keys(), ui.values()))
json_str = json.dumps(song_details, indent=4, sort_keys=False)
print json_str
nc = Foundation.NSDistributedNotificationCenter.defaultCenter()
GetSongs = GetSongs.new()
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.spotify.client.PlaybackStateChanged',None)
NSLog("Listening for Spotify PlaybackStateChanged....")
AppHelper.runConsoleEventLoop()
@loretoparisi
Copy link
Author

Response format:

{
    "Album": "Purpose (Deluxe)", 
    "Disc Number": 1, 
    "Has Artwork": true, 
    "Name": "Sorry", 
    "Artist": "Justin Bieber", 
    "Track Number": 4, 
    "Popularity": 94, 
    "Player State": "Paused", 
    "Album Artist": "Justin Bieber", 
    "Duration": 200786, 
    "Track ID": "spotify:track:69bp2EbF7Q2rqc5N3ylezZ", 
    "Playback Position": 70.33499908447266, 
    "Play Count": 0
}

How to run:

$ python spotify_notifications.py

{
    "Album": "Purpose (Deluxe)", 
    "Disc Number": 1, 
    "Has Artwork": true, 
    "Name": "Sorry", 
    "Artist": "Justin Bieber", 
    "Track Number": 4, 
    "Popularity": 94, 
    "Player State": "Paused", 
    "Album Artist": "Justin Bieber", 
    "Duration": 200786, 
    "Track ID": "spotify:track:69bp2EbF7Q2rqc5N3ylezZ", 
    "Playback Position": 70.33499908447266, 
    "Play Count": 0
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment