Skip to content

Instantly share code, notes, and snippets.

@Techno-coder
Last active December 23, 2020 03:55

Revisions

  1. Techno-coder revised this gist Dec 23, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion iTunesRichPresence.py
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,8 @@
    from PyObjCTools import AppHelper
    from pypresence import Presence

    client_id = "<redacted>"
    # References image assets
    client_id = "511368737199357972"

    class SongRegister(object):
    def __init__(self):
  2. Techno-coder created this gist Dec 23, 2020.
    70 changes: 70 additions & 0 deletions iTunesRichPresence.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    #!/usr/local/opt/[email protected]/bin/python3.8

    import objc
    import time
    from Foundation import *
    from PyObjCTools import AppHelper
    from pypresence import Presence

    client_id = "<redacted>"

    class SongRegister(object):
    def __init__(self):
    self.client = Presence(client_id)
    self.client.connect()

    print("Connected to Discord");
    self.client.update(state="Waiting for song change")

    self.currentSong = None
    self.resumeTimestamp = 0
    self.timeRemaining = 0
    self.isPaused = False

    # For future reference, underscores in the method name correspond to colons in
    # pyobjc so the selector needs to be explicitly named
    selector = objc.selector(self.on_song_change_, selector="on_song_change:".encode())
    notifications = Foundation.NSDistributedNotificationCenter.defaultCenter()
    notifications.addObserver_selector_name_object_(self, selector, 'com.apple.Music.playerInfo', None)

    # Method name ends in underscore to act as selector
    def on_song_change_(self, song):
    song = song.userInfo()
    if "Name" not in song:
    self.client.clear()
    return

    uniqueSongIdentifier = song["PersistentID"]
    totalTime = song["Total Time"] / 1000;

    hasReset = (not self.isPaused) and song["Player State"] == "Playing"
    if self.currentSong != uniqueSongIdentifier or hasReset:
    self.currentSong = uniqueSongIdentifier
    self.timeRemaining = totalTime

    if song["Player State"] == "Playing":
    self.resumeTimestamp = time.time()
    self.isPaused = False
    else:
    self.timeRemaining -= time.time() - self.resumeTimestamp
    self.isPaused = True

    songState = "{}".format(song["Name"][:128])
    songDetails = "{}".format(song.get("Artist", "Unknown Artist")[:128])
    icon = "icon"

    songEnd = None
    smallIcon = None
    if song["Player State"] == "Playing":
    songEnd = int(self.resumeTimestamp + self.timeRemaining)
    smallIcon = "play"
    else:
    smallIcon = "pause"

    self.client.update(state=songState, details=songDetails, end=songEnd,
    large_image=icon, large_text="Rich Presence built by Technocoder",
    small_image=smallIcon);

    register = SongRegister()
    AppHelper.runConsoleEventLoop()