-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spotify.py
68 lines (61 loc) · 2.28 KB
/
Spotify.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
from time import sleep
import dbus
class Spotify:
def __init__(self):
try:
self.spotify_bus = dbus.SessionBus().get_object("org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2")
except:
self.isSpotifyPlaying = False
else:
self.isSpotifyPlaying = True
if self.isSpotifyPlaying:
self.spotify_properties = dbus.Interface(self.spotify_bus,"org.freedesktop.DBus.Properties")
self.spotify_player = dbus.Interface(self.spotify_bus, "org.mpris.MediaPlayer2.Player")
self.metadata = self.spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
self.currentSong = self.to_song()
else:
self.currentSong="Spotify is not playing anything"
def update_song(self):
if self.isSpotifyPlaying:
self.metadata = self.spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
self.currentSong = self.to_song()
return self
else:
self.currentSong="Spotify is not playing anything"
def to_song(self):
if self.isSpotifyPlaying:
album = str(self.metadata["xesam:album"])
artist = str(self.metadata["xesam:artist"][0].strip())
title = str(self.metadata["xesam:title"])
spotifyURL = str(self.metadata["xesam:url"])
return Song(title, artist, spotifyURL, album)
def PlayPause(self):
if self.isSpotifyPlaying:
self.spotify_player.PlayPause()
def Next(self):
if self.isSpotifyPlaying:
self.spotify_player.Next()
sleep(0.5)
self.update_song()
def Previous(self):
if self.isSpotifyPlaying:
self.spotify_player.Previous()
sleep(0.5)
self.update_song()
class Song:
def __init__(self, title, artist, spotifyURL="", album=""):
self.title = title
self.artist = artist
self.album = album
self.spotifyURL = spotifyURL
def __str__(self):
return f"""\
Album: {self.album}
Artist: {self.artist}
Title: {self.title}
Spotify URL: {self.spotifyURL}"""
def __repr__(self):
return "repr: \n" + self.__str__()
if __name__ == "__main__":
print(Spotify().currentSong)