fm123 = fzf + mpg123

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
franksinistra
Ivana Fukalot
Posts: 1093
Joined: Mon Jan 27, 2014 2:03 am
Location: 印尼国

fm123 = fzf + mpg123

Unread post by franksinistra » Sun Jan 31, 2016 7:30 am

A shitty script to play / stream music using fzf + mpg123.

Code: Select all


#!/bin/sh


usage() {
  echo -e "
usage: $(basename $0) [-ls] [-dqru args]

switches:
-l      Loop forever
-s      Shuffle shit
-d      Specify the directory $(basename $0) will find your music
-q      Filter fzf result by your query
-r      Filter item by range (as in date range)
-u      Specify the directory where your channels database (ex: channels.csv) file exists.

\"-q\", \"-d\", \"-s\", and \"-r\" switches aren't available in url-stream (\"-u\") mode
"
}

while getopts ":d:lq:r:s:u:" opt; do
  case $opt in
    d)
      # Option d is to specify directory
			dir="$OPTARG"
			;;
		l)
      # Option 'l' is for looping infinitely
			loop="true"
			;;
		q)
      # Option 'q' to query
			query="$OPTARG"
			;;
		r)
      # Use option 'r' to specify range (integer as in number of days)
			if ! [[ $OPTARG =~ ^[0-9]+$ ]] ; then
			  echo "-r requires an integer number"
			  exit 1
			fi
			range="$OPTARG"
			;;
		s)
      # Self explanatory
			shuffle=true
			;;
    u)
      # To select a dir that contains multiple .csv (contains channels for streaming)
      url_dir="$OPTARG"
      ;;
		:)
			printf "Option -$OPTARG requires an argument.\n\n" >&2
			usage
			exit 1
			;;
		:)
  esac
done

if [[ $dir = "" ]]; then
  dir=.
fi

# If date range is provided.
if [[ -n $range ]]; then
  selection() {
	  find $dir -type d -mtime +1 -mtime -$range
	}
# If range isn't provided.
else
	selection() {
	  find $dir -type d
	}
fi

# Querying.
if [[ -n $query ]]; then
  eval "$(echo "query_selection()"; declare -f selection | tail -n +2)"
	selection() {
	  query_selection | grep -i "$query"
	}
fi

# Shuffle Music in a dir.
if [[ $shuffle = true ]]; then
  eval "$(echo "shuffle_selection()"; declare -f selection | tail -n +2)"
	selection() {
	  shuffle_selection | perl -MList::Util=shuffle -e "print shuffle(<STDIN>);" | head -n 1
	}
fi

if [[ -n $url_dir ]]; then
  selection() {
    sel="$(find $url_dir -type f -name "*.csv" )"
    for x in "$sel"; do
      cat $x;
    done
  }
fi

play_selection() {
  fzf_selection=$(selection | fzf -1)
	if [[ -n $fzf_selection ]]; then
    if [[ -n $url_dir ]]; then
      rad_sel=$(echo "$fzf_selection" | grep -Eo '(http|ftp|https)://[[:alnum:][:punct:]]*[[:alnum:]]')
      mpg123 -C --title --utf8 --buffer 2048 --smooth -@ "$rad_sel"
    else
		  mpg123 -C --title "$fzf_selection"/*
    fi
	else
		echo "Nothing found or selected!"
	fi
}

if [[ $loop = true ]]; then
  while :
	do
	  play_selection
	done
else
  play_selection
fi

Stations list example (my channels.csv) :

Code: Select all

# Station Name, URL
SomA: 7 Inch Soul, http://ice.somafm.com/7soul
SomA: BAGeL Radio, http://ice.somafm.com/bagel
SomA: Beat Blender, http://ice.somafm.com/beatblender
SomA: Black Rock FM, http://ice.somafm.com/brfm
SomA: Boot Liquor, http://ice.somafm.com/bootliquor
SomA: Cliqhop idm, http://ice.somafm.com/cliqhop
SomA: Covers, http://ice.somafm.com/covers
SomA: DEF CON Radio, http://ice.somafm.com/defcon
SomA: Deep Space One, http://ice.somafm.com/deepspaceone
SomA: Digitalis, http://ice.somafm.com/digitalis
SomA: Doomed, http://ice.somafm.com/doomed
SomA: Drone Zone, http://ice.somafm.com/dronezone
SomA: Dub Step Beyond, http://ice.somafm.com/dubstep
SomA: Earwaves, http://ice.somafm.com/earwaves
SomA: Fluid, http://ice.somafm.com/fluid
SomA: Folk Forward, http://ice.somafm.com/folkfwd
SomA: Groove Salad, http://ice.somafm.com/groovesalad
SomA: Illinois Street Lounge, http://ice.somafm.com/illstreet
SomA: Indie Pop Rocks, http://ice.somafm.com/indiepop
SomA: Left Coast 70s, http://ice.somafm.com/seventies
SomA: Lush, http://ice.somafm.com/lush
SomA: Mission Control, http://ice.somafm.com/missioncontrol
SomA: PopTron, http://ice.somafm.com/poptron
SomA: SF 10–33, http://ice.somafm.com/sf1033
SomA: Secret Agent, http://ice.somafm.com/secretagent
SomA: Sonic Universe, http://ice.somafm.com/sonicuniverse
SomA: Space Station Soma, http://ice.somafm.com/spacestation
SomA: Suburbs of Goa, http://ice.somafm.com/suburbsofgoa
SomA: The Trip, http://ice.somafm.com/thetrip
SomA: ThistleRadio, http://ice.somafm.com/thistle
SomA: Underground 80s, http://ice.somafm.com/u80s
using fifo would be a better implementation..... oh well.....
rice no more.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: fm123 = fzf + mpg123

Unread post by machinebacon » Sun Jan 31, 2016 11:57 am

Love the comma separated list and the perl, clever idea
..gnutella..

Post Reply