pyratelog

personal blog
git clone git://git.pyratebeard.net/pyratelog.git
Log | Files | Refs | README

commit e954f90cd4db3201c0e00c89aaeffde28ff4c9a1
parent b34d3092abc7c7bdc402379ad078ea802c77fda7
Author: pyratebeard <root@pyratebeard.net>
Date:   Wed,  5 Oct 2022 22:33:34 +0100

Merge branch 'weeklymusictoot'

Diffstat:
AMakefile | 33+++++++++++++++++++++++++++++++++
Aentry/20221005-weeklymusictoot.md | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aentry/make.md | 1+
Ascripts/draft | 20++++++++++++++++++++
Ascripts/preview | 40++++++++++++++++++++++++++++++++++++++++
Ascripts/publish | 41+++++++++++++++++++++++++++++++++++++++++
6 files changed, 243 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,33 @@ +#TITLE := $(shell git branch --show-current) +# https://stackoverflow.com/a/14061796 +# If the first argument is "draft" +ifeq (draft,$(firstword $(MAKECMDGOALS))) + # use the rest as arguments for "draft" + DRAFT_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) + # ...and turn them into do-nothing targets + $(eval $(DRAFT_ARGS):;@:) +else +ifeq (preview,$(firstword $(MAKECMDGOALS))) + VIEW_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) + $(eval $(VIEW_ARGS):;@:) +else +ifeq (publish,$(firstword $(MAKECMDGOALS))) + PUB_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) + $(eval $(PUB_ARGS):;@:) +endif +endif +endif + +prog: # ... + # ... + +.PHONY: draft preview publish + +draft : prog + scripts/draft $(DRAFT_ARGS) + +preview : prog + scripts/preview $(VIEW_ARGS) + +publish : prog + scripts/publish $(PUB_ARGS) diff --git a/entry/20221005-weeklymusictoot.md b/entry/20221005-weeklymusictoot.md @@ -0,0 +1,108 @@ +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. + +This is an automated post which lists some of the music I listened to that week. + +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.). + +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. + +``` +#!/bin/sh + +output() +{ + # write status to ~/cmus-status.txt (not very useful though) + echo "$*" >> ~/.cmus/wmt.txt 2>&1 +} + +while test $# -ge 2 +do + eval _$1='$2' + shift + shift +done + +if test -n "$_file" && test -n "$_title" && [[ "$_status" == "playing" ]] ; then + if [[ $(cmus-remote -Q | grep "set play_library" | awk '{print $NF}') == "false" ]] ; then + _underscore=$(echo $_title | sed 's/\ /_/g') + _playlist=$(grep -i -e "$_title\|$_underscore" ~/.cmus/playlists/* | awk -F: '{print $1}' | awk -F/ '{print $NF}' | head -n1) + test -n "$_playlist" && output "custom $_playlist playlist" + elif [[ $(cmus-remote -Q | grep "set play_library" | awk '{print $NF}') == "true" ]] ; then + output "$_album by $_artist" + fi +elif test -n "$_url" && [[ "$_status" == "playing" ]] ; then + output "$_url" +fi +``` + +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. + +``` +#!/bin/sh +# ██ +# ░██ +# ███ ██ ██████████ ██████ +# ░░██ █ ░██ ░░██░░██░░██ ░░░██░ +# ░██ ███░██ ░██ ░██ ░██ ░██ +# ░████░████ ░██ ░██ ░██ ░██ +# ███░ ░░░██ ███ ░██ ░██ ░░██ +# ░░░ ░░░ ░░░ ░░ ░░ ░░ +# w e e k l y m u s i c t o o t + +# output of cmus-status-display script +music_list="$HOME/.cmus/wmt.txt" + +# toot command path +toot="$HOME/src/warez/toot/bin/toot" + +# sort list by number of plays +weekmusic=$(sort $music_list | uniq -c | sort -nr | \ + awk '{$1=""; print $0}' | \ + tr '[:upper:]' '[:lower:]' | \ + sed 's/^/\*\ /') + +# generate toot and count characters +char=$(cat <<wmt | wc -m +(automated) #weeklymusictoot + +this week i listened to: + +$weekmusic +wmt +) + +# if characters is more than 500 the toot will fail +# we delete the last entry in the list until +# there are less than 500 chars +until [ ${char} -lt 500 ] ; do +weekmusic=$(echo "$weekmusic" | sed '$d') +cat <<wmt +$weekmusic +wmt +char=$(cat <<wmt | wc -m +(automated) #weeklymusictoot + +this week i listened to: + +$weekmusic +wmt +) +sleep 1 +done + +# generate toot then delete list for next week +# if it fails page me +cat <<wmt | $toot post && rm -f ${music_list} || curl -d "wmt failed to send" https://pager.pyratebeard.net/wmt +(automated) #weeklymusictoot + +this week i have been listening to: + +$weekmusic +wmt +``` + +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. + +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. + +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 ¯\_(ツ)_/¯ . diff --git a/entry/make.md b/entry/make.md @@ -0,0 +1 @@ +this is a test diff --git a/scripts/draft b/scripts/draft @@ -0,0 +1,20 @@ +#!/bin/sh + +# use arg as title and set post file path +TITLE=$1 +POST="entry/${TITLE}.md" + +# checkout the correct branch +git branch | grep ${TITLE} && \ + git checkout ${TITLE} || \ + git checkout -b ${TITLE} + +# if post file does not exist yet touch it +[ -f "${POST}" ] || touch "${POST}" + +# open post file in favourite editor +vim "${POST}" + +# when vim closes add and commit the changes +git add "${POST}" +git commit -S -m "${TITLE}" diff --git a/scripts/preview b/scripts/preview @@ -0,0 +1,40 @@ +#!/bin/bash + +# use arg as title and set post file path +TITLE=$1 +POST="entry/${TITLE}.md" + +# create a tmp dir +mkdir demo + +# copy post file, post_template and css +# to temp dir +cp ${POST} demo/index.md +cp post_template.html style.css demo/ + +# small change to post_template for stylesheet +sed -i 's%\.\./%%' demo/post_template.html + +# generate html file +pandoc -s \ + --template=demo/post_template.html \ + --metadata title="demolog" \ + -f markdown \ + -t html \ + -o demo/index.html \ + demo/index.md + +# start web server and capture pid for later +busybox httpd -f -h ./demo -p 8080 & +busypid=$! + +# open demo post file in browser +xdg-open http://localhost:8080 + +# wait until ready to stop web server +echo press any key to cancel +read -n 1 + +# kill the web server and remove temp dir +kill -9 ${busypid} +rm -rf demo/ diff --git a/scripts/publish b/scripts/publish @@ -0,0 +1,41 @@ +#!/bin/sh + +# use arg as title and set post file path +# set published file path with date +TITLE=$1 +POST="entry/${TITLE}.md" +PUBLISH="entry/$(date +%Y%m%d)-${TITLE}.md" +LINK="https://log.pyratebeard.net/entry/$(date +%Y%m%d)-${TITLE}.html" + +# toot command path +TOOT="$HOME/src/warez/toot/bin/toot" + +# checkout the correct branch +git branch | grep ${TITLE} && \ + git checkout ${TITLE} || \ + git checkout -b ${TITLE} + +# rename post to set date +git mv "${POST}" "${PUBLISH}" + +# commit the rename as published +git commit -S -m "publish ${TITLE}" + +# checkout main branch and merge published post +git checkout main +git merge "${TITLE}" + +# push new post +git push + +#cat <<log | $TOOT post || curl -d "log toot failed" https://pager.pyratebeard.net/logtoot +#${LINK} +# +#${TITLE} - <description> +# +##/100 #100daystooffload +# +##tag #tag #tag +# +#rss: https://log.pyratebeard.net/rss.xml +#log