scripts/music.sh

262 lines
8.0 KiB
Bash
Raw Permalink Normal View History

2024-09-09 15:45:27 +10:00
#!/bin/bash
# Maximum title length
MAX_TITLE_LENGTH=20
MAX_ARTIST_LENGTH=20
verbose=0
# Check if any argument is passed
if [ "$#" -ne 0 ]; then
arg="$1"
2024-10-14 12:04:47 +11:00
if [ "$arg" == "p" ]; then
playerctl play-pause
exit 0
elif [ "$arg" == "play" ]; then
2024-09-09 15:45:27 +10:00
playerctl play
2024-10-14 12:04:47 +11:00
exit 0
2024-09-09 15:45:27 +10:00
elif [ "$arg" == "pause" ]; then
playerctl pause
2024-10-14 12:04:47 +11:00
exit 0
2024-09-09 15:45:27 +10:00
elif [ "$arg" == "next" ]; then
playerctl next
2024-10-14 12:04:47 +11:00
exit 0
2024-09-09 15:45:27 +10:00
elif [ "$arg" == "prev" ]; then
playerctl previous
2024-10-14 12:04:47 +11:00
exit 0
2024-09-09 15:45:27 +10:00
elif [ "$arg" == "verbose" ]; then
verbose=1
else
2024-10-14 12:04:47 +11:00
echo "Invalid argument. Please use one of the following: p (play/pause), play, pause, next, prev"
2024-09-09 15:45:27 +10:00
exit 0
fi
if [ "$#" -eq 2 ]; then
if [ "$2" == "verbose" ]; then
verbose=1
fi
2024-10-14 12:04:47 +11:00
fi
2024-09-09 15:45:27 +10:00
fi
# Check if playerctl is available
2024-10-14 12:04:47 +11:00
if ! command -v playerctl &>/dev/null; then
2024-09-09 15:45:27 +10:00
echo "playerctl is not installed. Please install it to use this script."
exit 1
fi
# Get all player names and statuses
2024-11-07 14:37:03 +11:00
players=$(playerctl -a -l 2>/dev/null)
2024-09-09 15:45:27 +10:00
active_players=()
brave=""
2024-11-07 14:37:03 +11:00
exlude=(
"Messenger"
)
2024-09-09 15:45:27 +10:00
# Loop through each player and check its status
for player in $players; do
2024-10-14 20:19:00 +11:00
if [ "$verbose" -eq 1 ]; then
echo "Checking player $player"
fi
2024-09-09 15:45:27 +10:00
status=$(playerctl -p "$player" status 2>/dev/null)
# Check if player name contains brave
if [[ "$player" == *"brave"* ]]; then
2024-10-14 12:04:47 +11:00
if [ "$verbose" -eq 1 ]; then
echo "Brave found"
fi
2024-09-09 15:45:27 +10:00
brave=$player
fi
2024-11-07 14:37:03 +11:00
title=$(playerctl -p "$player" metadata --format '{{title}}' 2>/dev/null)
if [ "$verbose" -eq 1 ]; then
echo "Title: $title"
fi
# If title contains any string in the exclude array, skip the player
for exclude in "${exlude[@]}"; do
if [[ "$title" == *"$exclude"* ]]; then
if [ "$verbose" -eq 1 ]; then
echo "Excluding player $player"
fi
continue 2
fi
done
2024-09-09 15:45:27 +10:00
if [[ "$status" == "Playing" || "$status" == "Paused" ]]; then
2024-10-14 20:19:00 +11:00
if [[ $verbose -eq 1 ]]; then
echo "Player $player is active"
fi
2024-09-09 15:45:27 +10:00
active_players+=("$player:$status")
fi
done
# Function to truncate a string to a certain length
truncate_string() {
local string="$1"
local max_length="$2"
if [ ${#string} -gt $max_length ]; then
echo "${string:0:$max_length}..."
else
echo "$string"
fi
}
# Check how many active players we found
active_count=${#active_players[@]}
2024-10-14 20:19:00 +11:00
if [[ $verbose -eq 1 ]]; then
echo "Active players: $active_count"
fi
2024-09-09 15:45:27 +10:00
if [ "$active_count" -eq 1 ]; then
# If exactly one player is active, print its metadata
2024-10-14 12:04:47 +11:00
IFS=":" read -r player status <<<"${active_players[0]}"
2024-09-09 15:45:27 +10:00
title=$(playerctl -p "$player" metadata --format '{{title}}')
# If title == Vilos Player, get the title from the brave player
if [ "$title" == "Vilos" ]; then
title=$(playerctl -p "$brave" metadata --format '{{title}}')
2024-10-14 12:04:47 +11:00
truncated_title=$(truncate_string "$title" $(($MAX_TITLE_LENGTH + $MAX_ARTIST_LENGTH)))
2024-09-09 15:45:27 +10:00
if [ "$status" == "Paused" ]; then
2024-10-16 17:46:26 +11:00
echo "󰝚 $truncated_title (Paused)"
2024-09-09 15:45:27 +10:00
else
2024-10-16 17:46:26 +11:00
echo "󰝚 $truncated_title"
2024-09-09 15:45:27 +10:00
fi
exit 0
fi
artist=$(playerctl -p "$player" metadata --format '{{artist}}')
truncated_title=$(truncate_string "$title" $MAX_TITLE_LENGTH)
truncated_artist=$(truncate_string "$artist" $MAX_ARTIST_LENGTH)
if [ "$status" == "Paused" ]; then
2024-10-16 17:46:26 +11:00
echo "󰝚 $truncated_title [$truncated_artist] (Paused)"
2024-09-09 15:45:27 +10:00
else
2024-10-16 17:46:26 +11:00
echo "󰝚 $truncated_title [$truncated_artist]"
2024-09-09 15:45:27 +10:00
fi
2024-10-14 12:04:47 +11:00
2024-09-09 15:45:27 +10:00
elif [ "$active_count" -gt 1 ]; then
# Count to see how many playing players there are
playing_count=0
output=""
for player in "${active_players[@]}"; do
2024-10-14 12:04:47 +11:00
IFS=":" read -r player status <<<"$player"
2024-09-09 15:45:27 +10:00
if [ "$status" == "Playing" ]; then
playing_count=$((playing_count + 1))
title=$(playerctl -p "$player" metadata --format '{{title}}')
if [ "$title" == "Vilos" ]; then
title=$(playerctl -p "$brave" metadata --format '{{title}}')
2024-10-14 12:04:47 +11:00
truncated_title=$(truncate_string "$title" $(($MAX_TITLE_LENGTH + $MAX_ARTIST_LENGTH)))
2024-10-16 17:46:26 +11:00
output+="󰝚 $truncated_title"
2024-09-09 15:45:27 +10:00
else
artist=$(playerctl -p "$player" metadata --format '{{artist}}')
truncated_title=$(truncate_string "$title" $MAX_TITLE_LENGTH)
truncated_artist=$(truncate_string "$artist" $MAX_ARTIST_LENGTH)
2024-10-16 17:46:26 +11:00
output+="󰝚 $truncated_title [$truncated_artist]"
2024-09-09 15:45:27 +10:00
fi
2024-10-14 20:19:00 +11:00
if [ "$verbose" -eq 1 ]; then
echo "Player $player: $title - $artist"
fi
2024-09-09 15:45:27 +10:00
fi
done
if [ "$playing_count" -eq 1 ]; then
echo "$output"
else
2024-10-14 12:04:47 +11:00
if [ -z "$brave" ]; then
echo "Multiple players playing"
if [ "$verbose" -eq 1 ]; then
for player in "${active_players[@]}"; do
IFS=":" read -r player status <<<"$player"
title=$(playerctl -p "$player" metadata --format '{{title}}')
artist=$(playerctl -p "$player" metadata --format '{{artist}}')
echo "$player: $title - $artist"
echo "Status: $status"
done
fi
else
brave_status=$(playerctl -p "$brave" status 2>/dev/null)
2024-10-14 20:19:00 +11:00
title=$(playerctl -p "$brave" metadata --format '{{title}}' 2>/dev/null)
artist=$(playerctl -p "$brave" metadata --format '{{artist}}' 2>/dev/null)
2024-10-14 12:04:47 +11:00
# Print the title and artist
if [ "$verbose" -eq 1 ]; then
echo "Brave is playing"
echo "Title: $title"
echo "Artist: $artist"
fi
# Check if artist is empty
if [ -z "$artist" ]; then
2024-10-14 20:19:00 +11:00
if [ -z "$title" ]; then
echo "Multiple players playing"
exit 0
fi
2024-10-14 12:04:47 +11:00
truncated_title=$(truncate_string "$title" $(($MAX_TITLE_LENGTH + $MAX_ARTIST_LENGTH)))
if [ "$brave_status" == "Playing" ]; then
2024-10-16 17:46:26 +11:00
echo "󰝚 $truncated_title"
2024-10-14 12:04:47 +11:00
else
2024-10-16 17:46:26 +11:00
echo "󰝚 $truncated_title (Paused)"
2024-10-14 12:04:47 +11:00
fi
exit 0
fi
truncated_title=$(truncate_string "$title" $MAX_TITLE_LENGTH)
truncated_artist=$(truncate_string "$artist" $MAX_ARTIST_LENGTH)
2024-10-16 17:46:26 +11:00
output="󰝚 $truncated_title [$truncated_artist]"
2024-10-14 12:04:47 +11:00
if [ "$brave_status" == "Playing" ]; then
echo "$output"
else
echo "$output (Paused)"
fi
2024-09-09 15:45:27 +10:00
fi
2024-10-14 12:04:47 +11:00
fi
else
# Check if brave is not empty
if [ -z "$brave" ]; then
2024-11-07 14:37:03 +11:00
echo ""
2024-10-14 12:04:47 +11:00
exit 0
fi
brave_status=$(playerctl -p "$brave" status 2>/dev/null)
2024-10-16 17:46:26 +11:00
title=$(playerctl -p "$brave" metadata --format '{{title}}' 2>/dev/null)
artist=$(playerctl -p "$brave" metadata --format '{{artist}}' 2>/dev/null)
2024-10-14 12:04:47 +11:00
# Print the title and artist
if [ "$verbose" -eq 1 ]; then
echo "Brave is playing"
echo "Title: $title"
echo "Artist: $artist"
fi
# Check if artist is empty
if [ -z "$artist" ]; then
if [ -z "$title" ]; then
2024-10-16 17:46:26 +11:00
if [ "$verbose" -eq 1 ]; then
echo "No metadata found"
fi
echo ""
2024-10-14 12:04:47 +11:00
exit 0
fi
truncated_title=$(truncate_string "$title" $(($MAX_TITLE_LENGTH + $MAX_ARTIST_LENGTH)))
if [ "$brave_status" == "Playing" ]; then
2024-10-16 17:46:26 +11:00
echo "󰝚 $truncated_title"
2024-10-14 12:04:47 +11:00
else
2024-10-16 17:46:26 +11:00
echo "󰝚 $truncated_title (Paused)"
2024-10-14 12:04:47 +11:00
fi
2024-11-07 14:37:03 +11:00
echo ""
2024-10-14 12:04:47 +11:00
exit 0
fi
truncated_title=$(truncate_string "$title" $MAX_TITLE_LENGTH)
truncated_artist=$(truncate_string "$artist" $MAX_ARTIST_LENGTH)
2024-10-16 17:46:26 +11:00
output="󰝚 $truncated_title [$truncated_artist]"
2024-10-14 12:04:47 +11:00
if [ "$brave_status" == "Playing" ]; then
echo "$output"
else
echo "$output (Paused)"
2024-09-09 15:45:27 +10:00
fi
fi