Skip to content

Commit

Permalink
Add drawu's spotify widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigmanificient committed Sep 7, 2023
1 parent f17c3a4 commit fbf2246
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
networkmanagerapplet
libsForQt5.ark
libsForQt5.plasma-nm
playerctl

git
htop
Expand Down
4 changes: 2 additions & 2 deletions home/default.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{ pkgs, conf, ecsls, qtile, ... }:
{ pkgs, conf, ecsls, ... }:
{
nixpkgs.config.allowUnfree = true;

imports = [
(import ./nvim { inherit ecsls pkgs conf; })
(import ./qtile { inherit qtile; })

./btop
./neofetch
./picom
./dunst
./firefox
./qtile
./thunar
./tmux
./zsh
Expand Down
4 changes: 2 additions & 2 deletions home/qtile/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ qtile, ... }:
{ ... }:
{
home.file.qtile_configs = {
source = "${qtile}/src";
source = ./src;
target = ".config/qtile";
recursive = true;
};
Expand Down
2 changes: 2 additions & 0 deletions home/qtile/src/core/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Prompt,
QuickExit,
Separator,
SpotifyNowPlaying,
TaskList,
Wakatime,
)
Expand All @@ -30,6 +31,7 @@ class Bar(bar.Bar):
TaskList,
Separator,
Prompt,
SpotifyNowPlaying,
Wakatime,
Battery,
Memory,
Expand Down
3 changes: 3 additions & 0 deletions home/qtile/src/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
Separator,
TaskList,
)

from .spotify import SpotifyNowPlaying
from .wakatime import Wakatime

__all__ = (
Expand All @@ -22,6 +24,7 @@
"Prompt",
"QuickExit",
"Separator",
"SpotifyNowPlaying",
"TaskList",
"Wakatime",
)
76 changes: 76 additions & 0 deletions home/qtile/src/widgets/spotify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Wakatime widget inspired by drawbu."""
from typing import List, Optional
import subprocess
import urllib.request

from libqtile import qtile, widget
from libqtile.widget import base


COVER_PATH = "/tmp/spotify-now-playing.png"


def get_stdout(cmd: List[str]) -> str:
try:
sub = subprocess.Popen(cmd, stdout=subprocess.PIPE)
except FileNotFoundError:
return ""
return sub.communicate()[0].decode("utf-8").strip()


class SpotifyCover(widget.Image):
def __init__(self, **config):
super().__init__(filename=COVER_PATH, **config)
self.name = "Spotify cover"
self.filename: Optional[str] = None
self.__last_cover: Optional[str] = None

def update_cover(self) -> None:
if self.filename is None:
self.img = None
self.bar.draw()
return

cover_url = get_stdout(["playerctl", "--player=spotify", "metadata", "mpris:artUrl"])
if cover_url == "":
self.__cover.remove_cover()
return
if cover_url == self.__last_cover:
return
self.__last_cover = cover_url
urllib.request.urlretrieve(cover_url, self.filename)

old_length = self.calculate_length()
self._update_image()

if self.calculate_length() == old_length:
self.draw()
else:
self.bar.draw()

def remove_cover(self) -> None:
self.filename = None
self.__last_cover = None
self.update_cover()


class SpotifyNowPlaying(base.InLoopPollText):
def __init__(self, cover: SpotifyCover, **config):
super().__init__("", update_interval=5, qtile=qtile, **config)
self.name = "Spotify now playing"
self.__cover = cover

def poll(self) -> str:
is_playing = get_stdout(["playerctl", "--player=spotify", "status"])
if is_playing != "Playing":
self.__cover.remove_cover()
return ""
artist = get_stdout(["playerctl", "--player=spotify", "metadata", "artist"])
title = get_stdout(["playerctl", "--player=spotify", "metadata", "title"])
if artist == "" or title == "":
self.__cover.remove_cover()
return ""
if self.__cover.filename is None:
self.__cover.filename = COVER_PATH
self.__cover.update_cover()
return f" {artist} - {title}"

0 comments on commit fbf2246

Please sign in to comment.