Skip to content

Instantly share code, notes, and snippets.

@a3f
Last active August 16, 2016 09:30
Show Gist options
  • Select an option

  • Save a3f/064ba8b47f9e316163c09a78c965bfd6 to your computer and use it in GitHub Desktop.

Select an option

Save a3f/064ba8b47f9e316163c09a78c965bfd6 to your computer and use it in GitHub Desktop.
print a user's playing track on last.fm (or Spotify if last.fm is connected)
apikey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
user = xxx
^F3::
{
dhw := A_DetectHiddenWindows
DetectHiddenWindows On
Run "%ComSpec%" /k,, Hide, pid
while !(hConsole := WinExist("ahk_pid" pid))
Sleep 10
DllCall("AttachConsole", "UInt", pid)
DetectHiddenWindows %dhw%
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec("perl playingTrack.pl " apikey " " secret " " user)
track := regexreplace(exec.StdOut.ReadAll(), "[\r\n]+$")
if track is not space
SendInput % track
else
SendInput, n/a
DllCall("FreeConsole")
Process Exist, %pid%
if (ErrorLevel == pid)
Process Close, %pid%
}
cpan Net::LastFM
#!/usr/bin/perl
use strict;
use warnings;
use Net::LastFM;
# you can add your info here if you want, or over command line
my $api_key;
my $secret;
my $user;
my $lastfm = Net::LastFM->new(
api_key => shift // $api_key,
api_secret => shift // $secret,
);
my $data = $lastfm->request_signed(
method => 'user.getRecentTracks',
user => shift // $user,
);
my $p;
foreach my $track (@{$data->{recenttracks}{track}}) {
if ($track->{'@attr'}{nowplaying}) {
$p = $track;
last;
}
}
print "$p->{artist}{'#text'} ~ $p->{name} ($p->{album}{'#text'})\n" if defined $p;
@a3f
Copy link
Copy Markdown
Author

a3f commented Aug 16, 2016

How to use:

  • Register for an API key here: http://www.last.fm/api/account/create
  • Install Perl and AutoHotkey
  • Download the scripts into the same folder
  • Double click the installDeps.cmd script to install dependencies
  • Edit the bindPastePlayingTrack.ahk script to add your API key and username. The fourth Line is the shortcut (Ctrl+F3)
  • Run bindPastePlayingTrack.ahk and enjoy :)

Note: This does a HTTP request and might take a second or two

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