diff options
author | Yigit Sever | 2019-10-30 00:36:19 +0300 |
---|---|---|
committer | Yigit Sever | 2019-10-30 00:36:19 +0300 |
commit | d1c0127a9c3292239ef7ac357566d0575cd026f7 (patch) | |
tree | 8ec7f9c98f21283c263f11be737bbe00d5858bfe /.config/polybar/spotify_status.py | |
parent | cd5002de11e6fc5ebd7c354f23342d4be04470e0 (diff) | |
download | dotfiles-d1c0127a9c3292239ef7ac357566d0575cd026f7.tar.gz dotfiles-d1c0127a9c3292239ef7ac357566d0575cd026f7.tar.bz2 dotfiles-d1c0127a9c3292239ef7ac357566d0575cd026f7.zip |
Polybar spotify update
Diffstat (limited to '.config/polybar/spotify_status.py')
-rw-r--r-- | .config/polybar/spotify_status.py | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/.config/polybar/spotify_status.py b/.config/polybar/spotify_status.py new file mode 100644 index 0000000..3a56096 --- /dev/null +++ b/.config/polybar/spotify_status.py | |||
@@ -0,0 +1,123 @@ | |||
1 | #!/bin/python | ||
2 | |||
3 | import sys | ||
4 | import dbus | ||
5 | import argparse | ||
6 | |||
7 | parser = argparse.ArgumentParser() | ||
8 | parser.add_argument( | ||
9 | '-t', | ||
10 | '--trunclen', | ||
11 | type=int, | ||
12 | metavar='trunclen' | ||
13 | ) | ||
14 | parser.add_argument( | ||
15 | '-f', | ||
16 | '--format', | ||
17 | type=str, | ||
18 | metavar='custom format', | ||
19 | dest='custom_format' | ||
20 | ) | ||
21 | parser.add_argument( | ||
22 | '-p', | ||
23 | '--playpause', | ||
24 | type=str, | ||
25 | metavar='play-pause indicator', | ||
26 | dest='play_pause' | ||
27 | ) | ||
28 | parser.add_argument( | ||
29 | '--font', | ||
30 | type=str, | ||
31 | metavar='the index of the font to use for the main label', | ||
32 | dest='font' | ||
33 | ) | ||
34 | parser.add_argument( | ||
35 | '--playpause-font', | ||
36 | type=str, | ||
37 | metavar='the index of the font to use to display the playpause indicator', | ||
38 | dest='play_pause_font' | ||
39 | ) | ||
40 | |||
41 | |||
42 | args = parser.parse_args() | ||
43 | |||
44 | def fix_string(string): | ||
45 | # corrects encoding for the python version used | ||
46 | if sys.version_info.major == 3: | ||
47 | return string | ||
48 | else: | ||
49 | return string.encode('utf-8') | ||
50 | |||
51 | # Default parameters | ||
52 | output = fix_string(u'{play_pause} {artist}: {song}') | ||
53 | trunclen = 25 | ||
54 | play_pause = fix_string(u'\u25B6,\u23F8') # first character is play, second is paused | ||
55 | |||
56 | label_with_font = '%{{T{font}}}{label}%{{T-}}' | ||
57 | font = args.font | ||
58 | play_pause_font = args.play_pause_font | ||
59 | |||
60 | # parameters can be overwritten by args | ||
61 | if args.trunclen is not None: | ||
62 | trunclen = args.trunclen | ||
63 | if args.custom_format is not None: | ||
64 | output = args.custom_format | ||
65 | if args.play_pause is not None: | ||
66 | play_pause = args.play_pause | ||
67 | |||
68 | try: | ||
69 | session_bus = dbus.SessionBus() | ||
70 | spotify_bus = session_bus.get_object( | ||
71 | 'org.mpris.MediaPlayer2.spotify', | ||
72 | '/org/mpris/MediaPlayer2' | ||
73 | ) | ||
74 | |||
75 | spotify_properties = dbus.Interface( | ||
76 | spotify_bus, | ||
77 | 'org.freedesktop.DBus.Properties' | ||
78 | ) | ||
79 | |||
80 | metadata = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'Metadata') | ||
81 | status = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus') | ||
82 | |||
83 | # Handle play/pause label | ||
84 | |||
85 | play_pause = play_pause.split(',') | ||
86 | |||
87 | if status == 'Playing': | ||
88 | play_pause = play_pause[0] | ||
89 | elif status == 'Paused': | ||
90 | play_pause = play_pause[1] | ||
91 | else: | ||
92 | play_pause = str() | ||
93 | |||
94 | if play_pause_font: | ||
95 | play_pause = label_with_font.format(font=play_pause_font, label=play_pause) | ||
96 | |||
97 | # Handle main label | ||
98 | |||
99 | artist = fix_string(metadata['xesam:artist'][0]) if metadata['xesam:artist'] else '' | ||
100 | song = fix_string(metadata['xesam:title']) if metadata['xesam:title'] else '' | ||
101 | album = fix_string(metadata['xesam:album']) if metadata['xesam:album'] else '' | ||
102 | |||
103 | if not artist and not song and not album: | ||
104 | print('') | ||
105 | else: | ||
106 | if len(song) > trunclen: | ||
107 | song = song[0:trunclen] | ||
108 | song += '...' | ||
109 | if ('(' in song) and (')' not in song): | ||
110 | song += ')' | ||
111 | |||
112 | if font: | ||
113 | artist = label_with_font.format(font=font, label=artist) | ||
114 | song = label_with_font.format(font=font, label=song) | ||
115 | album = label_with_font.format(font=font, label=album) | ||
116 | |||
117 | print(output.format(artist=artist, song=song, play_pause=play_pause, album=album)) | ||
118 | |||
119 | except Exception as e: | ||
120 | if isinstance(e, dbus.exceptions.DBusException): | ||
121 | print('') | ||
122 | else: | ||
123 | print(e) | ||