Csandriel

xes (light radio-streaming script with tracks titles saving)

Dec 22nd, 2018 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.00 KB | None | 0 0
  1. #! /bin/bash
  2. #22.02.17
  3. #08.01.18
  4. #Csandriel
  5.  
  6.  
  7. #depends on: mpv
  8.  
  9. #username, will be used to launch the script, if it called by root.
  10. #To avoid ussues with permissions
  11. USER=user
  12. #keep track of all played traks ^_^. Must have liberal permissions
  13. #items for the best-list is taken from the tail of this "journal"
  14. LOG="/home/bin/.xes-mus.log"
  15. #these files too must be accesible for writing to everyone
  16. #best list
  17. BEST="/home/bin/.xes-bestmusic.log"
  18.  
  19. #We'll run in background. Need a reference to be killed with.
  20. PIDFILE="/home/bin/.xes.pid"
  21.  
  22. ADDRESSeS=(\
  23. #radio address:port. Put additional adresses here. Obey '\' finisher in all strings except the last
  24. "http://radio.heavy-music.ru:8107/320" \
  25. "https://stream.laut.fm/moshhead-gothic" \
  26. "http://streams.radiobob.de/gothic/mp3" \
  27. "https://stream.spreeradio.de/spree-live/mp3" \
  28. "https://0n-gothic.radionetz.de/0n-gothic.mp3" \
  29. "http://channels.fonotron.ru/stream/Chan_74_256.mp3" \
  30. "http://chanson.hostingradio.ru:8041/chanson256.mp3" \
  31. "http://ic2.101.ru:8000/v11_1" \
  32. "http://ep256.hostingradio.ru:8052/europaplus256.mp3" \
  33. "http://video.tvr.by:8000/culture" \
  34. "http://air2.radiorecord.ru:805/mdl_320"
  35. "http://ice912.echo.msk.ru:9120/32.aac" \
  36. "http://rfe04.akacast.akamaistream.net/7/894/437768/v1/ibb/akacast.akamaistream.net/rfe04" \
  37. "http://rfe10.akacast.akamaistream.net/7/894/437768/v1/ibb/akacast.akamaistream.net/rfe10" \
  38. "http://stream.euroradio.fm:8000/euroradio1"  
  39. )
  40.  
  41.  
  42.  
  43.  
  44. USAGE_MSG="  \n Usage: xes [-k|-s|-l|-c|-h <number>|-r|-v <number>] \n  
  45. where \n -k -- kill; \n -s -- save current track to the best-list \n -l -- roll-out the best-list
  46. -r -- clear the best-list \n -n -- show current playing track \n -c <number> -- select a chanell; if not selected CHAN 0 is used \n -v <number> -- set volume in percetage; if not set then 100 by default"
  47. #username.
  48. USER=user
  49. RESTART_MSG=\'"$PIDFILE' exists. It seems xes have been launched yet. Killing/Cleaning/Restart. \n Log-file will be reseted."
  50.  
  51. #if launched as root then restart script as user
  52. if [ $UID -eq 0 ] ; then echo "DROP ROOT & recurse"; STRING="$@"; su $USER -c "$0 $STRING"; exit; fi
  53.  
  54.  
  55.  
  56. dokill () {
  57.     cat "$PIDFILE"|xargs kill; rm "$PIDFILE" "$LOG"
  58.     }
  59.  
  60. doplay (){
  61.  
  62.     #-----------------------
  63.     #is xes running?
  64.     if [ -f $PIDFILE ]; then
  65.         echo -e $RESTART_MSG; dokill;
  66.         fi
  67.    
  68.     #-----------------------
  69.     #is $CHAN a valid index?  
  70.     if [ $((CHAN)) -gt $((${#ADDRESSeS[@]}-1)) ]; then
  71.         echo "lenght of ADDRESSeS= ${#ADDRESSeS[@]}"
  72.         echo "requested CHAN index exeeds existig array"
  73.         exit 1;
  74.     fi
  75.    
  76.     #-----------------------
  77.     #if $CHAN or $VOL is not set  then using by defaults
  78.     ADDRESS=${ADDRESSeS[${CHAN:-0}]}
  79.     echo "address set to $ADDRESS; "
  80.     VOLUME=${VOL:-100}
  81.     echo "volume set to $VOLUME"
  82.    
  83.     mpv --volume="$VOLUME" "$ADDRESS"  1>"$LOG" 2>/dev/null &
  84.     PID=$!
  85.     echo '$PID='"$PID"
  86.  
  87.         echo "$PID" > "$PIDFILE"
  88.     }
  89.     while getopts kslnrv:c: Option
  90.     do
  91.         case "$Option" in
  92.             "k") if [ -f $PIDFILE ] ; then dokill ;
  93.             else echo "xes is down. There is left only to kill yourself"; fi
  94.                 exit 0
  95.                 ;;
  96.             "s") cat $LOG|tail -n 2|grep icy|sed "s_.*title:\([^;]*\).*_\1_" >> $BEST
  97.                 exit 0
  98.                 ;;
  99.             "l") cat "$BEST"            
  100.                 exit 0
  101.                 ;;
  102.             "n") echo "Now playing:"
  103.                 cat $LOG|tail -n2|grep icy|sed "s_.*title:\([^;]*\).*_\1_"
  104.  
  105.                 exit 0
  106.                 ;;
  107.             "r") echo "BEST-LIST WILL BE CLEANCED! Are You sure? [y/<any key>]"
  108.                 read r; if [ "$r" == "y" ] ; then > "$BEST" ; else exit 0; fi          
  109.                 exit 0
  110.                 ;;
  111.             "c") CHAN=$OPTARG
  112.                 ;;
  113.             "v") VOL=$OPTARG
  114.                 ;;
  115.             * ) echo -e "$USAGE_MSG"
  116.             exit 1
  117.            
  118.             ;;
  119.         esac
  120.     done  
  121. doplay
  122.  
Add Comment
Please, Sign In to add comment