Use TimeWrapper to format length, additional clean-ups

This commit is contained in:
Thiago Kenji Okada 2016-10-19 22:03:33 -02:00
parent 42b09c8834
commit aab7f35c7d

View File

@ -1,6 +1,6 @@
import math
from i3pystatus import formatp from i3pystatus import formatp
from i3pystatus import IntervalModule from i3pystatus import IntervalModule
from i3pystatus.core.util import TimeWrapper
import gi import gi
gi.require_version('Playerctl', '1.0') # nopep8 gi.require_version('Playerctl', '1.0') # nopep8
@ -14,7 +14,7 @@ class Spotify(IntervalModule):
.. rubric:: Available formatters .. rubric:: Available formatters
* `{status}` current status icon (paused/playing/stopped) * `{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 * `{artist}` artist
* `{title}` title * `{title}` title
* `{album}` album * `{album}` album
@ -24,12 +24,10 @@ class Spotify(IntervalModule):
('format', 'formatp string'), ('format', 'formatp string'),
('format_not_running', 'Text to show if player 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', ('color_not_running', 'The color of the text, when player is 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', ('player_name',
'Name of music player, use `playerctl -l` with player running' 'Name of music player, use `playerctl -l` with player running to get. If None, tries to autodetect.'),
'to get. If None, tries to autodetect.'),
) )
# default settings # default settings
@ -37,7 +35,6 @@ class Spotify(IntervalModule):
color_not_running = '#ffffff' color_not_running = '#ffffff'
format = '{status} {length} {artist} - {title}' format = '{status} {length} {artist} - {title}'
format_not_running = 'Not running' format_not_running = 'Not running'
interval = 1
status = { status = {
'paused': '', 'paused': '',
'playing': '', 'playing': '',
@ -50,23 +47,17 @@ class Spotify(IntervalModule):
on_upscroll = 'next_song' on_upscroll = 'next_song'
on_downscroll = 'previous_song' on_downscroll = 'previous_song'
def _get_length(self, metadata): def _get_length_in_secs(self, metadata):
if not metadata: if not metadata:
return "" return 0
try: try:
time = metadata["mpris:length"] / 60.0e6 time = metadata["mpris:length"] / 1.0e6
minutes = math.floor(time) seconds = round(time)
seconds = round(time % 1 * 60) return seconds
if seconds < 10:
seconds = "0" + str(seconds)
length = "{}:{}".format(minutes, seconds)
except KeyError: except KeyError:
length = "" return 0
return length def get_formatted_info(self, player):
def get_info(self, player):
"""Get player track info from playerctl""" """Get player track info from playerctl"""
result = { result = {
@ -83,7 +74,8 @@ class Spotify(IntervalModule):
result["artist"] = player.get_artist() result["artist"] = player.get_artist()
result["title"] = player.get_title() result["title"] = player.get_title()
result["album"] = player.get_album() 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 return result
@ -91,9 +83,9 @@ class Spotify(IntervalModule):
"""Main statement, executes all code every interval""" """Main statement, executes all code every interval"""
self.player = Playerctl.Player(player_name=self.player_name) self.player = Playerctl.Player(player_name=self.player_name)
data = self.get_info(self.player) fdict = self.get_formatted_info(self.player)
if data.get("status", ""): if fdict.get("status", ""):
self.output = {"full_text": formatp(self.format, **data), self.output = {"full_text": formatp(self.format, **fdict),
"color": self.color} "color": self.color}
else: else:
self.output = {"full_text": self.format_not_running, self.output = {"full_text": self.format_not_running,