75 lines
6.4 KiB
HTML
75 lines
6.4 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head><script src="/livereload.js?mindelay=10&v=2&port=1313&path=livereload" data-no-instant defer></script>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<link rel="shortcut icon" href="http://localhost:1313/favicon.ico">
|
|
<link id="stylesheet" rel="stylesheet" href="/css/light.css">
|
|
|
|
<link rel="canonical" href="http://localhost:1313/rudimentary-local-scrobbling-with-bash/" />
|
|
<title>Rudimentary local scrobbling with bash</title>
|
|
</head>
|
|
<body><header id="banner">
|
|
<nav class="navbar">
|
|
<div class="nav-left">
|
|
|
|
<a href="http://localhost:1313/" class="home">~ 🏠</a>
|
|
|
|
<a
|
|
href="/info/"
|
|
title="--help"
|
|
>--help</a
|
|
>
|
|
</div>
|
|
<div class="nav-right">
|
|
|
|
<button id="toggle-button" class="toggle-button" onclick="toggleTheme()">🌚</button>
|
|
</div>
|
|
</nav>
|
|
</header>
|
|
<main id="content">
|
|
<article>
|
|
<header id="post-header">
|
|
<h3>Rudimentary local scrobbling with bash</h3>
|
|
<div>
|
|
<time>September 13, 2022</time>
|
|
</div>
|
|
</header><p>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.</p>
|
|
<h3 id="lets-get-down-to-business">LET’S GET DOWN TO BUSINESS</h3>
|
|
<p>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:</p>
|
|
<p><code>/home/randy/music/Beyonce/Lemonade/</code></p>
|
|
<p>To append this path to a file called scrobble while removing the first three directories (or the first 18 characters) rather inelegantly:</p>
|
|
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">printf</span> <span class="s2">"%s"</span> <span class="s2">"</span><span class="nv">$selected_path</span><span class="s2">"</span> <span class="p">|</span> cut -c 18- >> scrobble
|
|
</span></span></code></pre></div><p>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.</p>
|
|
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">Abdullah Ibrahim/South Africa
|
|
</span></span><span class="line"><span class="cl">Darkside/Psychic
|
|
</span></span><span class="line"><span class="cl">SOPHIE/OIL OF EVERY PEARL<span class="err">'</span>S UN-INSIDES
|
|
</span></span><span class="line"><span class="cl">Nicolas Jaar/Space Is Only Noise/2 Colomb.flac
|
|
</span></span><span class="line"><span class="cl">Townes Van Zandt
|
|
</span></span></code></pre></div><p>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:</p>
|
|
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tac scrobble <span class="p">|</span> column -t -s <span class="s2">"/"</span> -N <span class="s2">" ,artist,album,track"</span>
|
|
</span></span></code></pre></div><p>Maybe you only want the last ten things you listened to:</p>
|
|
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tail -n <span class="m">10</span> scrobble <span class="p">|</span> column -t -s <span class="s2">"/"</span> -N <span class="s2">" ,artist,album,song"</span>
|
|
</span></span></code></pre></div><p>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:</p>
|
|
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="k">for</span> album in <span class="k">$(</span>awk -F/ <span class="s1">'{ print $2 }'</span> scrobble <span class="p">|</span> sort -u<span class="k">)</span>
|
|
</span></span><span class="line"><span class="cl"> <span class="k">do</span>
|
|
</span></span><span class="line"><span class="cl"> <span class="nb">echo</span> <span class="s2">"'</span><span class="nv">$album</span><span class="s2">'/"</span><span class="k">$(</span>awk -F/ <span class="s1">'{ print $2 }'</span> <span class="s2">"</span><span class="nv">$scrob_file</span><span class="s2">"</span> <span class="p">|</span> grep <span class="nv">$album</span> <span class="p">|</span> wc -l<span class="k">)</span><span class="s2">""</span> >> temp
|
|
</span></span><span class="line"><span class="cl"> <span class="k">done</span>
|
|
</span></span></code></pre></div><p>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.</p>
|
|
<p>Finally, disclaimer: I am a bash amateur so I hope nothing you’ve seen here was too upsetting. Lots of love x</p>
|
|
</article>
|
|
</main>
|
|
|
|
<footer id="footer">
|
|
<p>-----------------</p>
|
|
<small>
|
|
made with <a href="https://gohugo.io">hugo</a> and my bastardised version of
|
|
<a href="https://github.com/LukasJoswiak/etch">this nice theme</a>
|
|
</small>
|
|
|
|
<script src="/js/search.js"></script>
|
|
<script src="/js/toggle.js"></script>
|
|
</footer>
|
|
|
|
</body>
|
|
</html>
|