Music listening via

Once upon a time, before the Raspberry Pi allowed for cheap DIY projects, I purchased a Logitech Squeezebox Radio. The greatest feature was the music streaming service of Then came Spotify and others; and eventually ceased streaming.

But, what it’s always done (setup dependent), is log what you listen to via its scrobbling service. Effectively, every time you listen to Spotify, radio stations, music on your smart phone… you POST data (track, song title, album, artist etc.) to your account.

My musical taste hasn’t changed much over a decade, as I’m too long in the tooth to change my listening flavour, which is predominantly the music I grew up with in the 80s and 90s. Of course, I’m not adverse to discovering great new music, but most people have a staple diet.

Recently however, my listening library has been blemished by my children’s listening habits as they monopolise the devices around the house. I love them with all my heart. But we’ve got to do something about their tastes in music.

So, I’m automating a weekly script to grab the top 5 artists per week – scrobbled from various sources to my account. Regular monitoring will tell if their listening is outweighing mine.

I make use of the api. I’m using Perl, and although there are a few dedicated modules available to garner data from, I’ll stick to LWP::UserAgent as the client, and place all the required parameters into the url.

Oh… and let’s visualise the results with a bar chart.

The script

# last fm weekly artists #

# top 5 weekly artists to barchart
# not using Last FM cpan module

use strict ;
use warnings ;
use LWP::UserAgent;
use JSON qw(decode_json);
use Config::Tiny;
use POSIX qw (strftime);
use GD::Graph::bars;

my $date = strftime "%e-%b-%Y",localtime;

my $config_file = "$ENV{HOME}/.lastfm.cnf";
die "$config_file not there" unless -e $config_file;

my $config = Config::Tiny->read($config_file);

my $user    = $config->{lastfm}->{user};
my $api_key = $config->{lastfm}->{api_key};
my $base_url    = "";
my $method_url  = "user.getweeklyartistchart";
my $format      = "json";

my $request_url = "$base_url/?method=$method_url&user=$user&api_key=$api_key&format=$format";

# send the request and decode json to perl data structure
# not too much held in memory - no need for content_reference
# or write to disk
my $ua        = LWP::UserAgent->new();
my $request   = $ua->get($request_url);
my $json      = $request->decoded_content;
my $perl_data = decode_json($json);

my (@artists, @playcounts);

# only need the top 5 weekly artists in barchart
my $x = 0;
foreach my $thing ( @{$perl_data->{weeklyartistchart}->{artist} } ) {
push (@artists,$thing->{'name'});
push (@playcounts,$thing->{'playcount'});
# exit when 5 is reached
last DATUM if $x == 5;

# artists and playcounts to bar chart
# create the layout
my $data = GD::Graph::Data->new( [ \@artists,\@playcounts ] );
my $graph =  GD::Graph::bars->new();

x_label => 'ARTISTS',
y_label => 'PLAYCOUNT',
x_labels_vertical => 1,
bar_spacing       => 1,
title   => " data $date",
) or die $graph->error;

$graph->plot($data) or die $graph->error;

# barchart to image file
my $file = "WeeklyArtists_$date.png";
open (my $picture,'>',$file) or die "Cannot open file $file $!";
binmode $picture;
print $picture $graph->gd->png;
close $picture;

Which gives me a little image file that displays…

All looks ok. The last artist I know little about. But, if you’ve ever seen the movie Drive (2011), then this is a standout track

It’s quite easy to build on this, and obtain different data from If you don’t yet scrobble to and you’d like to; simply head over to and create an account. Then on whatever listening platform; Spotify, Deezer etc. enable scrobbling from the settings.

To my knowledge, Amazon Music does not scrobble to – so this might prove a forlorn exercise.

Until the next time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: