Refactor Spotify module
Simplify logic and increase code robutness by using specific instead of generic Exceptions.
This commit is contained in:
parent
2fec760d3d
commit
8b95c429d4
@ -1,7 +1,10 @@
|
|||||||
import math
|
import math
|
||||||
from i3pystatus import formatp
|
from i3pystatus import formatp
|
||||||
from i3pystatus import IntervalModule
|
from i3pystatus import IntervalModule
|
||||||
from gi.repository import Playerctl
|
|
||||||
|
import gi
|
||||||
|
gi.require_version('Playerctl', '1.0')
|
||||||
|
from gi.repository import Playerctl, GLib
|
||||||
|
|
||||||
|
|
||||||
class Spotify(IntervalModule):
|
class Spotify(IntervalModule):
|
||||||
@ -49,54 +52,40 @@ class Spotify(IntervalModule):
|
|||||||
album = player.get_album()
|
album = player.get_album()
|
||||||
status = player.props.status
|
status = player.props.status
|
||||||
|
|
||||||
# gets the length of spotify through the metadata command
|
|
||||||
length = ""
|
|
||||||
|
|
||||||
# stores the metadata and checks if it is valid
|
# stores the metadata and checks if it is valid
|
||||||
metadata = player.props.metadata
|
metadata = player.props.metadata
|
||||||
if metadata is not None:
|
try:
|
||||||
# math to convert the number stored in mpris:length to a human readable format
|
|
||||||
time = dict(metadata)["mpris:length"] / 60.0e6
|
time = dict(metadata)["mpris:length"] / 60.0e6
|
||||||
minutes = math.floor(time)
|
minutes = math.floor(time)
|
||||||
seconds = round(time % 1 * 60)
|
seconds = round(time % 1 * 60)
|
||||||
if seconds < 10:
|
if seconds < 10:
|
||||||
seconds = "0" + str(seconds)
|
seconds = "0" + str(seconds)
|
||||||
length = "{}:{}".format(minutes, seconds)
|
length = "{}:{}".format(minutes, seconds)
|
||||||
|
except (KeyError, TypeError):
|
||||||
# sets length to an empty string if it does not exist for whatever reason. This should usually not happen
|
|
||||||
else:
|
|
||||||
length = ""
|
length = ""
|
||||||
|
|
||||||
# returns a dictionary of all spotify data
|
# returns a dictionary of all spotify data
|
||||||
return {"artist": artist, "title": title, "album": album, "status": status, "length": length}
|
return {
|
||||||
|
"status": self.status[status.lower()] if status else None,
|
||||||
|
"title": title if title else "",
|
||||||
|
"album": album if album else "",
|
||||||
|
"artist": artist if artist else "",
|
||||||
|
"length": length,
|
||||||
|
}
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""Main statement, executes all code every interval"""
|
"""Main statement, executes all code every interval"""
|
||||||
|
|
||||||
# tries to create player object and get data from player
|
|
||||||
try:
|
try:
|
||||||
self.player = Playerctl.Player(player_name="spotify")
|
self.player = Playerctl.Player(player_name="spotify")
|
||||||
|
|
||||||
response = self.get_info(self.player)
|
fdict = self.get_info(self.player)
|
||||||
|
|
||||||
# creates a dictionary of the spotify data captured
|
|
||||||
fdict = {
|
|
||||||
'status': self.status[response['status'].lower()],
|
|
||||||
'title': response["title"],
|
|
||||||
'album': response.get('album', ''),
|
|
||||||
'artist': response.get('artist', ''),
|
|
||||||
'length': response.get('length', 0),
|
|
||||||
}
|
|
||||||
self.data = fdict
|
|
||||||
self.output = {"full_text": formatp(self.format, **fdict),
|
self.output = {"full_text": formatp(self.format, **fdict),
|
||||||
"color": self.color}
|
"color": self.color}
|
||||||
|
except GLib.Error:
|
||||||
# outputs the not running string if spotify is closed
|
|
||||||
except:
|
|
||||||
self.output = {"full_text": self.format_not_running,
|
self.output = {"full_text": self.format_not_running,
|
||||||
"color": self.color_not_running}
|
"color": self.color_not_running}
|
||||||
if hasattr(self, "data"):
|
|
||||||
del self.data
|
|
||||||
|
|
||||||
def playpause(self):
|
def playpause(self):
|
||||||
"""Pauses and plays spotify"""
|
"""Pauses and plays spotify"""
|
||||||
|
Loading…
Reference in New Issue
Block a user