From aab7f35c7da219b284f2c65dc63939f03a9c975e Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 19 Oct 2016 22:03:33 -0200 Subject: [PATCH] Use TimeWrapper to format length, additional clean-ups --- i3pystatus/spotify.py | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/i3pystatus/spotify.py b/i3pystatus/spotify.py index 1d5e81b..f5cfab3 100644 --- a/i3pystatus/spotify.py +++ b/i3pystatus/spotify.py @@ -1,6 +1,6 @@ -import math from i3pystatus import formatp from i3pystatus import IntervalModule +from i3pystatus.core.util import TimeWrapper import gi gi.require_version('Playerctl', '1.0') # nopep8 @@ -14,7 +14,7 @@ class Spotify(IntervalModule): .. rubric:: Available formatters * `{status}` — current status icon (paused/playing/stopped) - * `{length}` — total song duration (mm:ss format) + * `{length}` — total song duration, uses TimeWrapper formatting, default format is `%E%l:%M:%S` * `{artist}` — artist * `{title}` — title * `{album}` — album @@ -24,12 +24,10 @@ class Spotify(IntervalModule): ('format', 'formatp string'), ('format_not_running', 'Text to show if player is not running'), ('color', 'The color of the text'), - ('color_not_running', - 'The color of the text, when player is not running'), + ('color_not_running', 'The color of the text, when player is not running'), ('status', 'Dictionary mapping status to output'), ('player_name', - 'Name of music player, use `playerctl -l` with player running' - 'to get. If None, tries to autodetect.'), + 'Name of music player, use `playerctl -l` with player running to get. If None, tries to autodetect.'), ) # default settings @@ -37,7 +35,6 @@ class Spotify(IntervalModule): color_not_running = '#ffffff' format = '{status} {length} {artist} - {title}' format_not_running = 'Not running' - interval = 1 status = { 'paused': '▷', 'playing': '▶', @@ -50,23 +47,17 @@ class Spotify(IntervalModule): on_upscroll = 'next_song' on_downscroll = 'previous_song' - def _get_length(self, metadata): + def _get_length_in_secs(self, metadata): if not metadata: - return "" - + return 0 try: - time = metadata["mpris:length"] / 60.0e6 - minutes = math.floor(time) - seconds = round(time % 1 * 60) - if seconds < 10: - seconds = "0" + str(seconds) - length = "{}:{}".format(minutes, seconds) + time = metadata["mpris:length"] / 1.0e6 + seconds = round(time) + return seconds except KeyError: - length = "" + return 0 - return length - - def get_info(self, player): + def get_formatted_info(self, player): """Get player track info from playerctl""" result = { @@ -83,7 +74,8 @@ class Spotify(IntervalModule): result["artist"] = player.get_artist() result["title"] = player.get_title() result["album"] = player.get_album() - result["length"] = self._get_length(player.props.metadata) + length_in_secs = self._get_length_in_secs(player.props.metadata) + result["length"] = TimeWrapper(length_in_secs, "%E%l%M:%S") return result @@ -91,9 +83,9 @@ class Spotify(IntervalModule): """Main statement, executes all code every interval""" self.player = Playerctl.Player(player_name=self.player_name) - data = self.get_info(self.player) - if data.get("status", ""): - self.output = {"full_text": formatp(self.format, **data), + fdict = self.get_formatted_info(self.player) + if fdict.get("status", ""): + self.output = {"full_text": formatp(self.format, **fdict), "color": self.color} else: self.output = {"full_text": self.format_not_running,