---
title: Rudimentary local scrobbling with bash 
date: 2022-09-13
tags:
  - music
  - bash
draft: false
---


There are lots of music players on linux. I have used lots of them, I quite like some of them. But for some reason I decided I wanted more. With this in mind, over the past few months I have been constructing a sprawling ecosystem of bash scripts all geared towards delivering a customised listening experience tailored perfectly to my every need. In short, the setup uses a simple dmenu file manager to browse my local files and mpv to play them. Today I'll be talking specifically about my setup for recording the albums I've been listening to.

### LET'S GET DOWN TO BUSINESS

Whenever I select a file to be played with my script I am effectively selecting a path to a file or a path to a directory with files in it which is then fed to mpv. For example, if I'm playing the classic album Lemonade by Beyonce it would look like this:

`/home/randy/music/Beyonce/Lemonade/`

To append this path to a file called scrobble while removing the first three directories (or the first 18 characters) rather inelegantly:

```bash
printf "%s" "$selected_path" | cut -c 18- >> scrobble
```

As new paths are appended to the file, this will will result in a scrobble file made up of three columns: the first for artists, the second for albums, and the third for songs.

```bash 
Abdullah Ibrahim/South Africa
Darkside/Psychic
SOPHIE/OIL OF EVERY PEARL'S UN-INSIDES
Nicolas Jaar/Space Is Only Noise/2 Colomb.flac
Townes Van Zandt
```

As you can see here, unless you only play music song by song, not all columns will always be populated. Now we have a file that contains this information we can do stuff to it. For example, to show our listening history and display it in columns with some pretty labels:

```bash
tac scrobble | column -t -s "/" -N " ,artist,album,track"
```

Maybe you only want the last ten things you listened to:

```bash
tail -n 10 scrobble | column -t -s "/" -N " ,artist,album,song"
```

To find our most played atrists it's a little more complicated. We can use awk to extract the artist column and remove all duplicate entries with sort -u to get a list of all played artists to iterate over. Then for each unique artist we grep for instances of them in the artist column and append that number of instances and the artist to a temporary file. This can then displayed as you see fit:

```bash
for album in $(awk -F/ '{ print $2 }' scrobble | sort -u)
	do
		echo "'$album'/"$(awk -F/ '{ print $2 }' "$scrob_file" | grep $album | wc -l)"" >> temp
	done
```
	  
So these are just a few examples; the real point is once you have that file of three columns the world is your oyster. You could probably even use something a little less cumbersome such as python.

Finally, disclaimer: I am a bash amateur so I hope nothing you've seen here was too upsetting. Lots of love x
