Advertisement
GohoCraft

Playlist converter - linux to windows

Dec 14th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.42 KB | None | 0 0
  1. #!/bin/bash
  2. start_time=$(($(date +%s%N)/1000000))
  3. ################################################
  4. ################################################
  5. ################################################
  6. ###
  7. ### -------------------------------------
  8. ###    delays playlist converter script
  9. ###    LINUX to WINDOWS and visa verse
  10. ### -------------------------------------
  11. ###
  12. ###   The script compares playlists in two folders
  13. ###   with different filepathformats: Linux<->Windows,
  14. ###   sync them depending on last modification date
  15. ###   and applies changes to each line of the playlists
  16. ###
  17. ###   This script needs write permissions in
  18. ###   the playlist folders.
  19. ###
  20. ###   Installation:
  21. ###  ----------------
  22. ###   1. Set the vars to your needs!
  23. ###   2. Run script once in terminal!
  24. ###   3. After installation just add it to your crons
  25. ###
  26. ###      -----------------------
  27. ###   EXECUTE as root is important!
  28. ###      -----------------------
  29. ###
  30. ###   All errors and changes are logged to syslog
  31. ###
  32. ################################################
  33. ######### CONFIGURATION START ##################
  34. ################################################
  35. #
  36. # Permissions for the script
  37. #
  38. # User and group for the mpd folders
  39.     USER="mpd"
  40.     GRP="audio"
  41. # User and group for the windows folders
  42.     USER_WIN="delay"
  43.     GRP_WIN="delay"
  44. # New file permissions - no need for change
  45.     PERMS="0755"
  46. #
  47. # Add all your playlist filetypes here
  48.     FILETYPES="*.m3u"
  49. #
  50. # MPD Playlist folder
  51.     MPDPATH=/var/lib/mpd/playlists
  52. #
  53. # WIN Playlist folder
  54.     WINPATH=/media/raid/Musik/Playlist
  55. #
  56. # Backup file
  57.     BACKUP="$MPDPATH/playlist-backup.tar.gz"
  58. #######################################
  59. #
  60. #      Converter configuration
  61. #        -------------------
  62. # Just add a new entry for any change the script should do.
  63. # We using a little sed function to do the changes
  64. # Get information about linux sed: http://sed.sourceforge.net/sed1line.txt or http://www.pement.org/sed/sed1line.txt
  65. #    - Syntax:    change $1 's/  search reg exp  /  replace with  /'
  66.  
  67. # See the examples below:
  68. #   This example looks for all \ and converts them to / inside the playlist
  69. #       change $1 's/\\/\//g'
  70. #   Delte lines matching the pattern
  71.  #      change $1 '/pattern/d'
  72. #######################################
  73. convert_mpd() {
  74. ### Convert mpd playlists to win playlists
  75.  
  76. # Add a leading ..\
  77.     change $1 's/^/..\\/'
  78.    
  79. # Convert all / to \
  80.     change $1 's/\//\\/g'
  81.  
  82. }
  83. convert_win() {
  84. ### Convert win playlists to mpd playlists
  85.  
  86. # Remove leading subdirs
  87.     change $1 's/^\\\\MEDIA-SERVER\\Musik\\//'
  88.    
  89. # Remove leading ..\
  90.     change $1 's/^..\\//'
  91.    
  92. # Remove leading slash
  93.     change $1 's/^\///'
  94.    
  95. # Convert all \ to /
  96.     change $1 's/\\/\//g'
  97.  
  98. }
  99. ################################################
  100. ######### CONFIGURATION END ####################
  101. ################################################
  102. echolog() {
  103.     PRE="## PL-SYNC"
  104.     if [ "$1" = "=" ]; then
  105.         LOG="$PRE [=] No update needed -> $2"        
  106.     elif [ "$1" = "+" ]; then
  107.         #LOG="$PRE [+] $LINES Entries created & converted -> $2 in $3"        
  108.         LOG="$PRE [+] $2($LINES) converted -> $3"
  109.         logit "$LOG"
  110.     elif [ "$1" = "~" ]; then
  111.         LOG="$PRE [~] Converted -> $2 in $3"
  112.         logit "$LOG"
  113.     elif [ "$1" = "-" ]; then
  114.         LOG="$PRE [-] File deleted, index removed -> $2"  
  115.         logit "$LOG"  
  116.     elif [ "$1" = "!" ]; then
  117.         LOG="$PRE [!] $2 "
  118.         logit "$LOG"
  119.     else
  120.         LOG="$PRE $1"
  121.     fi
  122.     echo "$LOG"
  123.     LOG=""
  124. }
  125. logit()
  126. {
  127.     logger -p local0.notice -- PlaylistConverter: $*
  128. }
  129. echo '## ///==-- Playlist-Sync-Convert Script v2.3 --==\\\'
  130. # Check if script is running
  131. if [ -z $(pgrep -c "$BASH_ARGV") ] ; then
  132.   echolog "!" "ERROR -> Script is already running!"
  133.   exit 0
  134. fi
  135. # Is script executed as root?
  136. if [ ! $EUID = 0 ]; then
  137.   echolog "!" "ERROR -> This script must be executed as root!"
  138.   exit 0
  139. fi
  140. # Check if all vars are set
  141. if [ -z "$MPDPATH" ] || [ -z "$WINPATH" ] || [ -z "$USER" ] || [ -z "$PERMS" ] || [ -z "$GRP" ] || [ -z "$USER_WIN" ] || [ -z "$GRP_WIN" ] ; then
  142.   echolog "!" "ERROR -> Check your variables!"
  143.   exit 0
  144. fi
  145. # Check if all folders are reachable
  146. if [ ! -d "$MPDPATH" ] || [ ! -d "$WINPATH" ] ; then
  147.   echolog "!" "ERROR -> Can't find folders"
  148.   exit 0
  149. else
  150. # Check folder write permissions
  151.   if [ ! -w "$MPDPATH" ] ; then
  152.     echolog "!" "ERROR -> No write permissions: $MPDPATH"
  153.     exit 0
  154.   elif [ ! -w "$WINPATH" ] ; then
  155.     echolog "!" "ERROR -> No write permissions: $WINPATH"
  156.     exit 0
  157.   fi
  158. fi
  159. INDEX="$MPDPATH/index.tmp"
  160. # Create index file if none there
  161.     if [ ! -f $INDEX ] ; then
  162. # Ask for 1st install
  163.         echolog "-----------------------------------------"
  164.         while true; do
  165.             ASK=$(echolog '[?] Do you wish to install this program? y/n   ')
  166.             read -p "$ASK" yn
  167.             case $yn in
  168.                 [Yy]* )
  169.                     echolog "[!] Start installation..."
  170.                     start_time=$(($(date +%s%N)/1000000))
  171.                     break
  172.                     ;;
  173.                 [Nn]* )
  174.                     echolog "!" "Installation canceled by user!"
  175.                     exit
  176.                     ;;
  177.                 * ) echolog "[?] Please answer yes or no.";;
  178.             esac
  179.         done
  180.         echolog "!" "No index found, creating one -> $INDEX"
  181.         touch $INDEX
  182.         chown "$USER:$GRP" $INDEX
  183.         chmod 0777 $INDEX
  184.         echolog "[!] This is the 1st run of this script. All playlists will be synced and indexed now!"
  185.         echolog "[!] Remember: if you delete any playlists later, the other one will also be deleted during the next script run!"
  186.         echolog "-----------------------------------------"
  187.     fi
  188. PLAYLISTS="0"
  189. ENTRIES="0"
  190. LINES="0"
  191. TMP="$MPDPATH/playlist.tmp"
  192. # Create tmp file if none there
  193.     if [ ! -f $TMP ] ; then
  194.       #echolog "!" "TMP file created: $TMP"
  195.       touch $TMP
  196.       #chown "$USER:$GRP" $TMP
  197.     fi
  198. # Whitespace handling
  199. IFS=$'\n'
  200. # Global sed script
  201. change() {
  202.     # Simple stuff...
  203.     sed $2 $1 > $TMP
  204.     rm $1
  205.     cp -f $TMP $1
  206.     # clear tmp
  207.     > $TMP
  208. }
  209. # Create pl index entry
  210. index() {
  211.     # Check for old index entry
  212.     COUNT=$(grep -cx "$1" "$INDEX")
  213.     if [ "$COUNT" -gt "0" ] ; then
  214. ### Index found
  215.         # Delete file
  216.         rm $1
  217.         # Delete index entry
  218.         sed '/'"$1"'/d' $INDEX > $TMP
  219.         rm $INDEX
  220.         # Write new index
  221.         cp -f $TMP $INDEX
  222.         > $TMP
  223.         echolog "-" "$1"
  224.     else
  225. ### No index entry found
  226.         #echolog "[!] No index entry found!"
  227.         # Add index entry
  228.         echo "$1" >> $INDEX        
  229.         # Copy to new loaction, convert and change perms...
  230.         cp -f "$1" "$3"
  231.         if [ "$3" = "$MPDPATH" ] ; then
  232.             convert_win "$2"
  233.             chown "$USER:$GRP" "$2"
  234.             chmod "$PERMS" "$2"
  235.         elif [ "$3" = "$WINPATH" ] ; then
  236.             convert_mpd "$2"
  237.             chown "$USER_WIN:$GRP_WIN" "$2"
  238.             chmod "$PERMS" "$2"
  239.         fi
  240.         touch "$1"
  241.         touch "$2"
  242.         # Increase playlist count
  243.         LINES=$(wc -l < $2)
  244.         PLAYLISTS=$(($PLAYLISTS + 1))
  245.         ENTRIES=$(($ENTRIES + $LINES))
  246.         echolog "+" "$1" "$3"
  247.     fi
  248. }
  249. ### Check WINDOWS folder
  250.     echolog "[!] ---> Check WINDOWS folder..."
  251.     cd "$WINPATH"
  252.     for x in $(ls $FILETYPES); do
  253.         MPD=$(printf "$MPDPATH/$x")
  254.     ##### Check if we have a MPD version of the playlist
  255.         if [ -f "$MPD" ] ; then
  256.             # Do actions depending which file is newer or equal
  257.           ### If WIN is newer do stuff
  258.             if [ "$x" -nt "$MPD" ] ; then            
  259.                 # copy to mpd
  260.                 cp -f "$x" "$MPDPATH"
  261.                 convert_win "$MPD"
  262.                 touch "$x"
  263.                 touch "$MPD"
  264.                 echolog "~" "$x" "$MPDPATH"
  265.           ### MPD is newer do stuff
  266.             elif [ "$MPD" -nt "$x" ] ; then
  267.                 # copy to WIN
  268.                 cp -f "$MPD" "$WINPATH"
  269.                 convert_mpd "$x"
  270.                 touch "$x"
  271.                 touch "$MPD"
  272.                 echolog "~" "$x" "$WINPATH"
  273.           ### Files are the same -> exit run
  274.             else
  275.                 echolog "=" "$x"
  276.             fi
  277.         else
  278.     ##### No MPD Version, create one
  279.             # Create mpd version and change permission
  280.             index "$x" "$MPD" "$MPDPATH"
  281.         fi
  282.     done
  283. ### Check MPD folder
  284.     echolog "[!] ---> Check MPD folder..."
  285.     cd "$MPDPATH"  
  286.     for x in $(ls $FILETYPES); do
  287.         WIN=$(printf "$WINPATH/$x")
  288.     ##### Check if we have a WIN version of the playlist
  289.         if [ -f "$WIN" ] ; then
  290.             # Do actions depending which file is newer or equal
  291.           ### If MPD is newer do stuff
  292.             if [ "$x" -nt "$WIN" ] ; then
  293.                 # copy to win
  294.                 cp -f "$x" "$WINPATH"
  295.                 convert_mpd "$WIN"
  296.                 touch "$x"
  297.                 touch "$WIN"
  298.                 echolog "~" "$x" "$WINPATH"
  299.           ### WIN is newer do stuff
  300.             elif [ "$WIN" -nt "$x" ] ; then
  301.                 # copy to WIN
  302.                 cp -f "$WIN" "$MPDPATH"
  303.                 convert_win "$x"
  304.                 touch "$x"
  305.                 touch "$WIN"
  306.                 echolog "~" "$x" "$MPDPATH"
  307.           ### Files are the same -> exit run
  308.             else
  309.                 echolog "=" "$x"
  310.             fi
  311.         else
  312.     ##### No WIN Version, create one
  313.             # Create win version and change permission            
  314.             index "$x" "$WIN" "$WINPATH"
  315.         fi
  316.     done
  317. ### Delete TMP file after use
  318.     if [ -f $TMP ] ; then
  319.         rm $TMP
  320.     fi
  321.     # Calculate runtime
  322.     finish_time=$(($(date +%s%N)/1000000))
  323.     # Check if this was the first run
  324.     if [ -n "$ASK" ] ; then
  325.         echolog "-----------------------------------------"
  326.         echolog "!" "Installation & 1st run completed in $((finish_time - start_time))ms !"
  327.         echolog "!" "$PLAYLISTS playlists containing $ENTRIES entries synchronized."
  328.         echolog "[!] Remember: if you delete any playlists later, the other one will also be deleted during the next script run!"
  329.         echolog "-----------------------------------------"
  330.     else
  331.         echolog "[!] $PLAYLISTS playlists / $ENTRIES entries in $((finish_time - start_time))ms synchronized."
  332.     fi
  333. ################### ALL JOBS DONE :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement