Developer Journal: 16 April 2020

Posted on Thu 16 April 2020 in dev-journal

Automating developer journal entry creation

Wrote a script to help make creating developer journals easier to use. It was a simple zsh script but I had to look up a few things.

How do you check if a file already exists?

Found an example here

if test -f "$filename"; then
    echo "Entry exists - opening file"
fi

How do you get the current date?

This was fairly easy however I did run into one snag, which was creating spaces in the date. Generating a date in the form of 2020-04-16 worked as intended using the following: $(date +20%y-%m-%d) However, when I wanted to make the title of a post have a format of 16 March 2020 I was getting errors because of the expansion with the spaces. I do not do bash/zsh scripting very often and always need to look up commands. In this case an easy fix was just to put the spaces in quotes, so I ended up using $(date +%d" "%B" "20%y)"

Good reference for the different format controls of the date command can be found here

In the end the entire script was 13 lines and will create a new post with the necessary metadata or open the file if it already exists.

#! /bin/zsh
filename=$HOME/code/michaelabrahamsen.com/content/posts/dev-journal/$(date +20%y-%m-%d)-developer-journal.md
if test -f "$filename"; then
    echo "Entry exists - opening file"
else
    touch $filename
    echo "---" >> $filename
    echo "Title: Developer Journal: $(date +%d" "%B" "20%y)" >> $filename
    echo "Tags: developer journal" >> $filename
    echo "Author: Michael Abrahamsen" >> $filename
    echo "---" >> $filename
fi
nvim $filename

Automating video downloads for offline viewing

This one was easy, I have a set of videos that I have been reviewing daily for my blue belt exam. Viewing the videos on the website is not a good experience. After checking the url for a few videos I found they were all located in the same directory with each video filename just used an integer that incremented for each video. The exam is comprised of 88 different techniques that I need to be able to perform. The script was very simple. Just a for loop that used wget and a sleep for 60 seconds between videos just to ease the load on their server. The code below does not use the actual url because there is a paywall in order to view the content. Using the wget -P command was something I had never done so that was a useful trick to learn:

#! /bin/zsh
for ((i=1; i<89;i++));
do wget "https://curriculum//videos//WB//wb_$i.mp4" -P /media/bjj/white-to-blue/;
    echo "Sleeping for 60 seconds"
    sleep 60
done