Skip to content

Instantly share code, notes, and snippets.

@rfc-2549
Created February 26, 2022 14:54
Show Gist options
  • Save rfc-2549/b26557ba8190fe500bc429c523f06717 to your computer and use it in GitHub Desktop.
Save rfc-2549/b26557ba8190fe500bc429c523f06717 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use LWP::UserAgent;
use JSON;
my $ua = LWP::UserAgent->new;
my $API_KEY = "XXXXXXXXXXXXXXXXXXX";
my $USER = "xxx";
my $url = "/2.0/?method=user.getrecenttracks&user=$USER&api_key=$API_KEY&format=json&limit=2";
my $base_url = "http://ws.audioscrobbler.com";
my $hash = decode_json($ua->get($base_url . $url)->content);
my $artist_name = $hash->{recenttracks}->{track}->[0]->{artist}->{'#text'};
my $song_name = $hash->{recenttracks}->{track}->[0]->{name};
my $now_playing = $hash->{recenttracks}->{track}->[0]->{'@attr'}->{nowplaying};
if($now_playing eq "true") {
printf("%s - %s\n", $artist_name, $song_name);
} else {
printf "Offline";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment