Create a gist now

Instantly share code, notes, and snippets.

This takes my playlists, managed by mpd, fixes them up, and places them in a staging folder, ready to sync with my Android phone. The script also asks if you'd like to sync the music after staging. Who needs iTunes anyway.
#!/usr/bin/env zsh
# Takes all my playlists from ~/.mpd/playlists, fixes them up, and creates a
# folder for each, along with the music they reference.
# The sync stage requires an sshd server to run on your phone, as well as the rsync executable.
# -
MPD_MUSIC_ROOT="${HOME}/Music" # Root of your MPD library
MPD_PLAYLIST_ROOT="${HOME}/.mpd/playlists" # MPD playlist folder
SYNC_MUSIC_ROOT="${HOME}/Android/Sync/Music" # Staging folder (receives all files prior to sync)
PLAYLIST_ROOT="${SYNC_MUSIC_ROOT}/playlists" # Where to place playlist files (m3u) for sync
PHONE_MUSIC_ROOT="/sdcard/Music" # Where the music will live on your phone
PHONE_ADDRESS="Nexus" # Address to access your phone over ssh for sync
# Output stuff to stdout if $DEBUG evaluates to anything
# $ DEBUG=1
function debug()
[[ ! -z "$DEBUG" ]] && echo $*
# Sync stuff with phone over rsync+ssh
function sync() {
rsync -vzrL --delete-after --no-perms --no-t ${SYNC_MUSIC_ROOT}/ ${PHONE_ADDRESS}:${PHONE_MUSIC_ROOT}/
# Ask for confirmation on something
function confirm()
echo -n "$@ [y/N] "
read answer
for response in y Y yes YES Yes Sure sure SURE OK ok Ok kk; do
[[ "_$answer" == "_$response" ]] && return 0
return 1
[[ ! -d "$PLAYLIST_ROOT" ]] && mkdir -p "$PLAYLIST_ROOT"
for pl in ~/.mpd/playlists/*.m3u; do
plname="$(basename ${pl})"
[[ -f "$plfile" ]] && rm -f "$plfile"
[[ -d "$plfolder" ]] && rm -rf "$plfolder"
[[ ! -d "$plfolder" ]] && mkdir -p "$plfolder" && touch "$plfile"
while read line; do
bn=$(basename "$f")
if [[ -f "$f" && ! -f "${plfolder}/${bn}" ]]; then
ln -s "$f" "$plfolder"
echo "${PHONE_MUSIC_ROOT}/${plname}/${bn}" >> "$plfile"
done < "$pl"
confirm "Would you like to sync with your phone? (Make sure sshd runs on the device)" && sync

How could I add a list of playlists to sync? Not every playlist should get synced. for pl in ~/.mpd/playlists/*.m3u; do?

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