My NixOS configurations + dotfiles
1#!/bin/bash
2
3# https://gist.github.com/ashish-kus/dd562b0bf5e8488a09e0b9c289f4574c
4
5if [ $# -eq 0 ]; then
6 echo "Usage: $0 --title | --arturl | --artist | --length | --album | --source"
7 exit 1
8fi
9
10# Function to get metadata using playerctl
11get_metadata() {
12 key=$1
13 playerctl metadata --format "{{ $key }}" 2>/dev/null
14}
15
16# Check for arguments
17
18# Function to determine the source and return an icon and text
19get_source_info() {
20 trackid=$(get_metadata "mpris:trackid")
21 if [[ "$trackid" == *"firefox"* ]]; then
22 echo -e "Firefox "
23 elif [[ "$trackid" == *"spotify"* ]]; then
24 echo -e "Spotify "
25 elif [[ "$trackid" == *"chromium"* ]]; then
26 echo -e "Chrome "
27 elif [[ "$trackid" == *"cider"* ]]; then
28 echo -e "Apple Music " # added
29 else
30 echo ""
31 fi
32}
33
34# Parse the argument
35case "$1" in
36--title)
37 title=$(get_metadata "xesam:title")
38 if [ -z "$title" ]; then
39 echo ""
40 else
41 echo "${title:0:28}" # Limit the output to 50 characters
42 fi
43 ;;
44--arturl)
45 url=$(get_metadata "mpris:artUrl")
46 if [ -z "$url" ]; then
47 echo ""
48 else
49 if [[ "$url" == file://* ]]; then
50 url=${url#file://}
51 elif [[ "$url" == http* ]]; then
52 url="${url/640x640/1024x1024}"
53 hash=$(echo -n "$(playerctl metadata album)" | sha256sum | awk '{print $1}')
54 file_path="/tmp/${hash}.jpg"
55 if [[ ! -f "$file_path" ]]; then
56 curl -s --compressed --connect-timeout 2 -m 5 -o "$file_path" "$url" &
57 fi
58 url="file://$file_path"
59 fi
60 echo "$url"
61 fi
62 ;;
63--artist)
64 artist=$(get_metadata "xesam:artist")
65 if [ -z "$artist" ]; then
66 echo ""
67 else
68 echo "${artist:0:30}" # Limit the output to 50 characters
69 fi
70 ;;
71--length)
72 length=$(get_metadata "mpris:length")
73 if [ -z "$length" ]; then
74 echo ""
75 else
76 # Convert length from microseconds to a more readable format (seconds)
77 echo "$(echo "scale=2; $length / 1000000 / 60" | bc) m"
78 fi
79 ;;
80--status)
81 status=$(playerctl status 2>/dev/null)
82 if [[ $status == "Playing" ]]; then
83 echo ""
84 elif [[ $status == "Paused" ]]; then
85 echo ""
86 else
87 echo ""
88 fi
89 ;;
90--album)
91 album=$(playerctl metadata --format "{{ xesam:album }}" 2>/dev/null)
92 if [[ -n $album ]]; then
93 echo "$album"
94 else
95 status=$(playerctl status 2>/dev/null)
96 if [[ -n $status ]]; then
97 echo "Not album"
98 else
99 echo ""
100 fi
101 fi
102 ;;
103--source)
104 get_source_info
105 ;;
106*)
107 echo "Invalid option: $1"
108 echo "Usage: $0 --title | --url | --artist | --length | --album | --source"
109 exit 1
110 ;;
111esac