Skip to content

Instantly share code, notes, and snippets.

@ablakely
Last active June 13, 2022 06:22
Show Gist options
  • Save ablakely/8420231ce79d83bd4127 to your computer and use it in GitHub Desktop.
Save ablakely/8420231ce79d83bd4127 to your computer and use it in GitHub Desktop.
HexChat Spotify Script
# Spotify-DBus
# Written by Aaron Blakely <aaron@ephasic.org>
# Date: 2/13/2016
# Updated for new HexChat Perl API: 6/05/2019
# Updated for spotify control: 6/22/2022
#
# Usage:
# /np - Prints current track info to current window in /me action.
# /nplink - Prints current track URL into current window.
# /spotify <np|link|play|prev|next|stop> - Controls spotify
#
# Autoloading [Tested with HexChat 2.14.2 on Arch]
# Put np.pl in ~/.config/HexChat/addons
# Script should now be autoloaded when starting HexChat.
use warnings;
if ($^O eq "linux") {
HexChat::register("Spotify-DBus", "0.0.1", "Spotify DBus Interface Script", "Spotify DBus Interface Script");
HexChat::hook_command("spotify", "do_spotify");
HexChat::hook_command("np", "do_np");
HexChat::hook_command("nplink", "do_nplink");
} else {
HexChat::print("Warning: Spotify-NP is only compatible with linux enviornments.");
}
sub do_spotify {
my $cmd = $_[1][1] || $_[0];
if ($cmd eq "np") {
my $album = `dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'|egrep -A 2 "album"|egrep -v "album"|egrep -v "array"|cut -b 44-|cut -d '"' -f 1|egrep -v ^\$`;
my $artist = `dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'|egrep -A 2 "artist"|egrep -v "artist"|egrep -v "array"|cut -b 27-|cut -d '"' -f 1|egrep -v ^\$`;
my $song = `dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'|egrep -A 1 "title"|egrep -v "title"|cut -b 44-|cut -d '"' -f 1|egrep -v ^\$`;
chomp $album;
chomp $artist;
chomp $song;
if ($album ne "" && $artist ne "" && $song ne "") {
HexChat::command("me is listening to $song by $artist [Album: $album]");
} else {
HexChat::command("echo [Spotify-NP] Nothing appears to be playing at the moment.");
}
} elsif ($cmd eq "link") {
my $link = `dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'|egrep -A 1 "url"|egrep -v "url"|cut -b 44-|cut -d '"' -f 1|egrep -v ^\$`;
chomp $link;
HexChat::command("say $link");
} elsif ($cmd eq "play") {
system 'dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Play';
} elsif ($cmd eq "pause") {
system 'dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause';
} elsif ($cmd eq "next") {
system 'dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next';
} elsif ($cmd eq "prev") {
system 'dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous';
} else {
HexChat::command("echo Usage: /spotify <np|link|play|pause|next|prev>");
}
}
sub do_np { do_spotify("np"); }
sub do_nplink { do_spotify("link"); }
@ablakely
Copy link
Author

ablakely commented Feb 13, 2016

Spotify-DBus

HexChat Spotify script

Usage

  /spotify <np|link|play|pause|prev|next> - Controls spotify.
  /np      - Prints current track info into current in /me action.
  /nplink  - Prints Spotify Track URL to current window.

Example

  * Dark_Aaron is listening to Mother by Danzig [Album: Danzig]
  <Dark_Aaron> spotify:track:4gSiNltIPWhEMtxN8gQ9VG

Installing

  1. Put np.pl in ~/.xchat2/scripts
  2. Open XChat and type /load ~/.xchat2/scripts/np.pl

Written by Aaron Blakely aaron@ephasic.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment