-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplemusicrp.py
90 lines (70 loc) · 2.12 KB
/
applemusicrp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3.11
from ScriptingBridge import SBObject, SBApplication
from pypresence import Presence
from rumps import *
from threading import Thread
import datetime
import time
import asyncio
import syslog
import requests
import StatusBar
import Music
def richPresenceLoop(musicInfo):
# create asynchronous event loop for multithreaded execution
try:
asyncio.get_event_loop()
except:
asyncio.set_event_loop(asyncio.new_event_loop())
# application info
client_id = "1084641054055211128"
RPC = Presence(client_id)
# connect to discord
while True:
try:
RPC.connect()
except:
time.sleep(2)
else:
break
# update rich presence
while True:
try:
trackName, album, artist, artURL = musicInfo.getTrackInfo()
playerPosition = musicInfo.getPlayerState()[0]
RPC.update(
large_image = artURL,
details=trackName
+ " - "
+ album,
state="by "
+ artist
+ " - "
+ str(
datetime.timedelta(minutes=0, seconds=int(playerPosition))
)[-5:])
except:
time.sleep(1)
RPC.connect()
else:
time.sleep(1)
def main():
# create music object, get initial info
music = Music.Music()
trackInfo = music.getTrackInfo()
# start rich presence
richPresence = Thread(target=richPresenceLoop, args=[music])
richPresence.start()
# create status bar app
app = StatusBar.StatusBarApp(name="Apple Music Rich Presence", icon="imgs/trayIcon.png", music=music)
try:
app.menu.add(rumps.MenuItem(
title = trackInfo[0] + " - " + trackInfo[1],
icon='imgs/default_img.png',
dimensions=(35,35)))
app.run()
except Exception as error:
syslog.syslog(syslog.LOG_ALERT, str(error))
syslog.syslog(syslog.LOG_ALERT, "except ran")
if __name__ == "__main__":
main()