forked from minneyar/clementine-discord
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclementine_discord.py
executable file
·144 lines (124 loc) · 5.56 KB
/
clementine_discord.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
# Clementine / Discord Integration by minneyar
# fork by kitten99
import struct
import sys
import dbus
import logging
import time
import pypresence
# Any of the keys in Clementine's metadata are valid here, but note that colons
# will be replaced with dashes.
# To see a list of keys, play a song and run:
# qdbus org.mpris.MediaPlayer2.clementine /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get \
# org.mpris.MediaPlayer2.Player Metadata
DETAILS_STRING = '{artist} - {title}'
ALBUM_STRING = '{album}'
CLIENT_ID = 1163635305933451264
class PresenceUpdater:
def __init__(self):
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
self.logger = logging.getLogger(__name__)
self.logger.info("Initializing.")
self.bus = dbus.SessionBus()
self.client = pypresence.Presence(CLIENT_ID)
self.player = None
self.prop_iface = None
self.large_image_key = "logo"
self.large_image_text = "Clementine"
self.small_image_key = "play"
self.small_image_text = "Playing"
def run(self):
while True:
try:
if not self.prop_iface:
self.logger.info("Connecting to Clementine.")
self.player = self.bus.get_object('org.mpris.MediaPlayer2.clementine', '/org/mpris/MediaPlayer2')
self.prop_iface = dbus.Interface(self.player, dbus_interface='org.freedesktop.DBus.Properties')
self.logger.info("Connecting to Discord.")
self.client.connect()
self.update_loop()
except dbus.exceptions.DBusException as e:
self.logger.warning("Error communicating with Clementine: %s" % str(e))
self.logger.warning("Reconnecting in 15s.")
self.player = None
self.prop_iface = None
time.sleep(15)
except (pypresence.exceptions.InvalidID, ConnectionRefusedError, struct.error) as e:
self.logger.warning("Error communicating with Discord: %s" % str(e))
self.logger.warning("Reconnecting in 15s.")
time.sleep(15)
def update_loop(self):
while True:
self.logger.debug("Reading data from Clementine.")
try:
metadata = self.prop_iface.Get('org.mpris.MediaPlayer2.Player', 'Metadata')
position_s = self.prop_iface.Get('org.mpris.MediaPlayer2.Player', 'Position') / 1000000
playback_status = self.prop_iface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
except dbus.exceptions.DBusException as e:
self.client.clear()
raise e
time_start = None
time_end = None
artist = ', '.join([str(a) for a in metadata.get('xesam:artist', [])])
title = str(metadata.get('xesam:title', ''))
album = metadata.get('xesam:album', '')
if playback_status == 'Stopped':
details = None
state = 'Stopped'
small_image_key = "stopbut"
small_image_text = "Stopped"
elif playback_status == 'Paused':
details = DETAILS_STRING.format(artist=artist, title=title)
state = ALBUM_STRING.format(album=album)
small_image_key = "pausebut"
small_image_text = "Paused"
else:
self.logger.debug("Clementine Metadata: %s" % metadata)
details = DETAILS_STRING.format(artist=artist, title=title)
state = ALBUM_STRING.format(album=album)
small_image_key = "playbut"
small_image_text = "Playing"
# Do some trimming if needed or discord won't accept >128 chars
if details and len(details) > 128:
remaining_chars = 128 - len("...") # Leave room for "..."
artist_title_string = f'{artist} - {title}'
if len(artist_title_string) > remaining_chars:
# If both artist and title together are too long, trim both
artist = artist[:remaining_chars // 2 - 4] + "..."
title = title[:remaining_chars // 2 - 4] + "..."
else:
# Otherwise, just trim the title
title = title[:remaining_chars - len(artist) - 4] + "..."
details = DETAILS_STRING.format(artist=artist, title=title)
if playback_status == 'Playing':
try:
length_s = metadata['mpris:length'] / 1000000
time_now = time.time()
time_start = time_now - position_s
time_end = time_start + length_s
except KeyError:
# Some media types may not provide length information; just ignore it
pass
self.logger.debug("Updating Discord.")
self.client.update(
state=state,
details=details,
start=time_start,
end=time_end,
large_image=self.large_image_key,
large_text=self.large_image_text,
small_image=small_image_key,
small_text=small_image_text
)
time.sleep(15)
def close(self):
self.logger.info("Shutting down.")
if hasattr(self, 'client'):
self.client.close()
if __name__ == '__main__':
updater = PresenceUpdater()
try:
updater.run()
finally:
updater.close()