iphone-music-sync.sh (1416B)
1 #!/usr/bin/env bash 2 3 # Syntax for playlist csv file: 4 # `TerrariaSoundtrack,Terraria Soundtrack` 5 # `local-dirname,remote-title` 6 7 PLAYLISTS_FILE="$XDG_CONFIG_HOME/regexghost/playlists-phone-sync.csv" 8 9 MOUNT_LOCATION="$HOME/Downloads/USBDrive" 10 11 # Mount phone 12 13 if ! lsusb | grep -q Apple; then 14 echo "Plug in iPhone" 15 exit 16 fi 17 18 idevicepair validate 19 idevicepair pair 20 21 [[ "$?" == "0" ]] || exit 22 23 ifuse --documents com.foobar2000.mobile "$MOUNT_LOCATION" 24 25 [[ "$?" == "0" ]] || exit 26 27 # Create folder for playlists 28 29 [ -d "${MOUNT_LOCATION}/Playlists" ] || mkdir "${MOUNT_LOCATION}/Playlists" 30 31 # Copy/update playlists 32 33 while read -r line; do 34 # Local path 35 computer="$(echo "$line" | cut -d "," -f 1)" 36 # Phone path 37 phone="$(echo "$line" | cut -d "," -f 2)" 38 echo "${computer} -> ${phone}" 39 # Create phone dir if non existant 40 [ -d "${MOUNT_LOCATION}/${phone}" ] || mkdir "${MOUNT_LOCATION}/${phone}" 41 # Copy, with --copy-links, so locally symlinked playlists (e.g. CurrentPlaylist) copy as files 42 rsync -vr --copy-links --update --delete --modify-window=1 --info=progress2 "${HOME}/Music/${computer}/" "${MOUNT_LOCATION}/${phone}/" 43 # Shuffle into playlist file 44 find "${MOUNT_LOCATION}/${phone}/" -type f | sort | sed "s|.*${phone}/|../${phone}/|g" > "/tmp/${phone}.m3u8" 45 cp "/tmp/${phone}.m3u8" "${MOUNT_LOCATION}/Playlists/" 46 done < "$PLAYLISTS_FILE" 47 48 # Unmount phone 49 50 fusermount -u "${MOUNT_LOCATION}" 51 idevicepair unpair