Skip to content

Instantly share code, notes, and snippets.

@jesvs
Created May 16, 2015 05:11
Show Gist options
  • Save jesvs/87364f128148359a62ac to your computer and use it in GitHub Desktop.
Save jesvs/87364f128148359a62ac to your computer and use it in GitHub Desktop.
#!/bin/bash
# MPD recent songs playlist creator
# Usage: mpd_recent_playlist.sh [days]
# Config starts here
MPC_CONF=/etc/mpd.conf
PLAYLIST_TITLE="Recently Added"
FILETYPES="mp3|m4a|flac|ogg"
# Config ends here
DAYS_OLD=$1
if [ -e $DAYS_OLD ]; then
DAYS_OLD=30
fi
PLAYLIST_FILE=$(gawk 'match($0, /playlist_directory.*"(.*)"/, arr) { print arr[1] }' $MPC_CONF)/${PLAYLIST_TITLE}.m3u
MUSIC_DIR=$(gawk 'match($0, /music_directory.*"(.*)"/, arr) { print arr[1] }' $MPC_CONF)
MUSIC_DIR_ESCAPED=$(echo $MUSIC_DIR | sed -e 's/[]\/$*.^|[]/\\&/g')
find -L $MUSIC_DIR -type f -mtime -${DAYS_OLD} -regex ".*\.(${FILETYPES})$" | awk "{ sub(/^${MUSIC_DIR_ESCAPED}(\/)?/, \"\"); print }" > "$PLAYLIST_FILE"
TOTAL_FILES=$(cat "$PLAYLIST_FILE" | wc -l)
echo Created the $PLAYLIST_TITLE playlist with $TOTAL_FILES songs.
@bonelifer
Copy link

Could you explain how this works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment