-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbus_api.py
114 lines (90 loc) · 3.8 KB
/
dbus_api.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
# Methods to get media info through DBus.
import platform
from src.notifications.notif_handler import send_notif
import dbus # Already on most Linux systems, seems broken so we won't use it.
# Many of the methods just parse the DBus media output into a standard format.
# Seems broken, will just use virtual key api
class DBusApi:
def __init__(self):
self.current_os = platform.system()
self.player = 'org.mpris.MediaPlayer2.Player'
self.set_dbus_connections()
# Sets the variables we will need to communicate with Spotify on DBus.
def set_dbus_connections(self):
self.session_bus = dbus.SessionBus()
self.dbus_connected = False
try:
self.spotify_bus = self.session_bus.get_object('org.mpris.MediaPlayer2.spotify',
'/org/mpris/MediaPlayer2')
self.interface = dbus.Interface(self.spotify_bus, self.player)
self.spotify_properties = dbus.Interface(self.spotify_bus,
'org.freedesktop.DBus.Properties')
self.metadata = self.spotify_properties.Get(self.player, 'Metadata')
self.dbus_connected = True
except dbus.DBusException:
self.dbus_disconnect()
# Spotify doesn't support the majority of available properties
def get_property(self, player_propety):
return self.spotify_properties.Get(self.player, player_propety)
# Same as above
def set_property(self, player_propety, value):
return self.spotify_properties.Set('org.mpris.MediaPlayer2.Player', player_propety, value)
# Since DBus operates like a socket connection, every time we disconnect from it (with an error),
# we have to try to start the whole connection anew.
def run_method(self, method_str, *args):
if not self.dbus_connected:
self.set_dbus_connections()
if self.dbus_connected:
try:
response = getattr(self.interface, method_str)(*args)
return response
except dbus.DBusException:
self.dbus_disconnect()
# def get_info(self, info_str):
# return self.ensure_dbus_connection(self.metadata.get, info_str)
def dbus_disconnect(self):
self.dbus_connected = False
send_notif('Spotify closed', 'Open Spotify and try again.')
# DBus seems to stay behind on songs when changing Spotify contexts, so for now
# we defalt to using the Web API
# def get_track_id(self):
# return self.get_info('mpris:trackid').split(':')[-1]
#
# def get_album(self):
# return self.get_info('xesam:album')
#
# def get_track(self):
# return self.get_info('xesam:title')
#
# def get_artist(self):
# return str(self.get_info('xesam:artist')[0])
#
# def get_art_url(self):
# return self.get_info('mpris:artUrl')
#
# def get_current_track(self): # Tries to (partly) emulate a Spotify track object
# track = dict()
#
# track['name'] = self.get_track()
# track['artists'] = [{'name': self.get_artist()}]
# track['album'] = {'name': self.get_album(), 'images': [{'url': self.get_art_url()}]}
#
# return track
def play_pause(self):
return self.run_method('PlayPause')
def next(self):
return self.run_method('Next')
def previous(self):
return self.run_method('Previous')
# Is the same as pause()
def stop(self):
return self.run_method('Stop')
def pause(self):
return self.run_method('Pause')
def play(self):
return self.run_method('Play')
# Does nothing
def seek(self, offset):
return self.run_method('Seek', offset)
def open(self, uri):
return self.run_method('OpenUri', uri)