Modify Spotify module to support any player

This commit is contained in:
Thiago Kenji Okada 2016-10-19 10:09:36 -02:00
parent 8b95c429d4
commit 76cdd5487f

View File

@ -3,13 +3,13 @@ from i3pystatus import formatp
from i3pystatus import IntervalModule from i3pystatus import IntervalModule
import gi import gi
gi.require_version('Playerctl', '1.0') gi.require_version('Playerctl', '1.0') # nopep8
from gi.repository import Playerctl, GLib from gi.repository import Playerctl, GLib
class Spotify(IntervalModule): class Spotify(IntervalModule):
""" """
Gets Spotify info using playerctl Gets Spotify (or any supported player) info using playerctl
.. rubric:: Available formatters .. rubric:: Available formatters
@ -22,10 +22,12 @@ class Spotify(IntervalModule):
settings = ( settings = (
('format', 'formatp string'), ('format', 'formatp string'),
('format_not_running', 'Text to show if cmus is not running'), ('format_not_running', 'Text to show if player is not running'),
('color', 'The color of the text'), ('color', 'The color of the text'),
('color_not_running', 'The color of the text, when cmus is not running'), ('color_not_running',
'The color of the text, when player is not running'),
('status', 'Dictionary mapping status to output'), ('status', 'Dictionary mapping status to output'),
('player_name', 'Name of music player. If None, tries to autodetect'),
) )
# default settings # default settings
@ -38,6 +40,7 @@ class Spotify(IntervalModule):
'paused': '', 'paused': '',
'playing': '', 'playing': '',
} }
player_name = None
on_leftclick = 'playpause' on_leftclick = 'playpause'
on_rightclick = 'next_song' on_rightclick = 'next_song'
@ -45,7 +48,7 @@ class Spotify(IntervalModule):
on_downscroll = 'previous_song' on_downscroll = 'previous_song'
def get_info(self, player): def get_info(self, player):
"""gets spotify track info from playerctl""" """gets player track info from playerctl"""
artist = player.get_artist() artist = player.get_artist()
title = player.get_title() title = player.get_title()
@ -64,7 +67,7 @@ class Spotify(IntervalModule):
except (KeyError, TypeError): except (KeyError, TypeError):
length = "" length = ""
# returns a dictionary of all spotify data # returns a dictionary of all player data
return { return {
"status": self.status[status.lower()] if status else None, "status": self.status[status.lower()] if status else None,
"title": title if title else "", "title": title if title else "",
@ -77,7 +80,7 @@ class Spotify(IntervalModule):
"""Main statement, executes all code every interval""" """Main statement, executes all code every interval"""
try: try:
self.player = Playerctl.Player(player_name="spotify") self.player = Playerctl.Player(player_name=self.player_name)
fdict = self.get_info(self.player) fdict = self.get_info(self.player)
@ -88,7 +91,7 @@ class Spotify(IntervalModule):
"color": self.color_not_running} "color": self.color_not_running}
def playpause(self): def playpause(self):
"""Pauses and plays spotify""" """Pauses and plays player"""
self.player.play_pause() self.player.play_pause()
def next_song(self): def next_song(self):