20221005-weeklymusictoot.md (4206B)
1 For those that follow me on Mastodon or Twitter you may have seen a #weeklymusictoot being posted on my feed over the last few weeks. 2 3 This is an automated post which lists some of the music I listened to that week. 4 5 My music player of choice is [cmus](https://cmus.github.io/){target="_blank" rel="noreferrer"} and one of the cool things it can do is run scripts on a song change or when the status changes (pause, play, stop, etc.). 6 7 I have seen people use this to display album artwork or send a desktop notification each time a song changes. There are plenty of examples on the cmus [wiki](https://github.com/cmus/cmus/wiki/status-display-programs){target="_blank" rel="noreferrer"}. In the cmus repository there is a script called `cmus-status-display` which outputs the currently playing song to a text file. I made some modifications to the script to display the output in my preferred way, or to display the name of the playlist the song is in if applicable. 8 9 ``` 10 #!/bin/sh 11 12 output() 13 { 14 # write status to ~/cmus-status.txt (not very useful though) 15 echo "$*" >> ~/.cmus/wmt.txt 2>&1 16 } 17 18 while test $# -ge 2 19 do 20 eval _$1='$2' 21 shift 22 shift 23 done 24 25 if test -n "$_file" && test -n "$_title" && [[ "$_status" == "playing" ]] ; then 26 if [[ $(cmus-remote -Q | grep "set play_library" | awk '{print $NF}') == "false" ]] ; then 27 _underscore=$(echo $_title | sed 's/\ /_/g') 28 _playlist=$(grep -i -e "$_title\|$_underscore" ~/.cmus/playlists/* | awk -F: '{print $1}' | awk -F/ '{print $NF}' | head -n1) 29 test -n "$_playlist" && output "custom $_playlist playlist" 30 elif [[ $(cmus-remote -Q | grep "set play_library" | awk '{print $NF}') == "true" ]] ; then 31 output "$_album by $_artist" 32 fi 33 elif test -n "$_url" && [[ "$_status" == "playing" ]] ; then 34 output "$_url" 35 fi 36 ``` 37 38 Then I created a script which is run each Friday via cron to take the contents of the music list and send out a toot on mastodon. 39 40 ``` 41 #!/bin/sh 42 # ██ 43 # ░██ 44 # ███ ██ ██████████ ██████ 45 # ░░██ █ ░██ ░░██░░██░░██ ░░░██░ 46 # ░██ ███░██ ░██ ░██ ░██ ░██ 47 # ░████░████ ░██ ░██ ░██ ░██ 48 # ███░ ░░░██ ███ ░██ ░██ ░░██ 49 # ░░░ ░░░ ░░░ ░░ ░░ ░░ 50 # w e e k l y m u s i c t o o t 51 52 # output of cmus-status-display script 53 music_list="$HOME/.cmus/wmt.txt" 54 55 # toot command path 56 toot="$HOME/src/warez/toot/bin/toot" 57 58 # sort list by number of plays 59 weekmusic=$(sort $music_list | uniq -c | sort -nr | \ 60 awk '{$1=""; print $0}' | \ 61 tr '[:upper:]' '[:lower:]' | \ 62 sed 's/^/\*\ /') 63 64 # generate toot and count characters 65 char=$(cat <<wmt | wc -m 66 (automated) #weeklymusictoot 67 68 this week i listened to: 69 70 $weekmusic 71 wmt 72 ) 73 74 # if characters is more than 500 the toot will fail 75 # we delete the last entry in the list until 76 # there are less than 500 chars 77 until [ ${char} -lt 500 ] ; do 78 weekmusic=$(echo "$weekmusic" | sed '$d') 79 cat <<wmt 80 $weekmusic 81 wmt 82 char=$(cat <<wmt | wc -m 83 (automated) #weeklymusictoot 84 85 this week i listened to: 86 87 $weekmusic 88 wmt 89 ) 90 sleep 1 91 done 92 93 # generate toot then delete list for next week 94 # if it fails page me 95 cat <<wmt | $toot post && rm -f ${music_list} || curl -d "wmt failed to send" https://pager.pyratebeard.net/wmt 96 (automated) #weeklymusictoot 97 98 this week i have been listening to: 99 100 $weekmusic 101 wmt 102 ``` 103 104 The second week this ran I found that the toot failed as the character count was greater than 500. I amended the script to sort the list by most entries, i.e. most played album, then count the characters and remove the least played album until the toot is viable. 105 106 My Mastodon and Twitter accounts are linked using [Mastodon Twitter Crossposter](https://crossposter.masto.donte.com.br/){target="_blank" rel="noreferrer"}, so soon after my automatic toot goes out it is tweeted as well. 107 108 There is no real reason for this except being a bit bored for an hour so writing a tooting script. But maybe somebody will discover some good music from it ¯\_(ツ)_/¯ .