dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

lyrics (2396B)


      1 #!/bin/sh
      2 # requirements: hurl or curl, w3m
      3 
      4 version=0.1.0
      5 
      6 show_help()
      7 {
      8 	echo "usage: ${0##*/} [OPTIONS] [ARTIST - SONG]"
      9 	echo "fetches lyrics from lyricwiki. The song is identified by artist name"
     10 	echo "and song title in the format 'ARTIST - SONG'. If these are not"
     11 	echo "passed as an argument or read from mpd (option -m), the artist"
     12 	echo "and song title is expected as stdin."
     13 	echo
     14 	echo "OPTIONS are one or more of the following:"
     15 	echo "   -h   show this message"
     16 	echo "   -v   show version and license information"
     17 	echo "   -V   show verbose information during execution"
     18 	echo "   -m   fetch lyrics for song currently playing on mpd"
     19 	echo "   --   do not consider any following args as options"
     20 }
     21 
     22 show_version()
     23 {
     24 	echo "${0##*/} version $version"
     25 	echo "licensed under the ISC License"
     26 	echo "written by Anders Damsgaard, anders@adamsgaard.dk"
     27 	echo "https://src.adamsgaard.dk/dotfiles"
     28 }
     29 
     30 die()
     31 {
     32 	printf '%s: error: %s\n' "${0##*/}" "$1" >&2
     33 	exit 1
     34 }
     35 
     36 fetch_lyrics_lyricwiki()
     37 {
     38 	local _a, _s, _qanswer
     39 	_a="$(echo "$1" | sed 's/ - .*//;s/ /_/g')"
     40 	_s="$(echo "$1" | sed 's/.* - //;s/ /_/g')"
     41 	if [ -z "$_a" ]; then
     42 		die 'artist name not specified'
     43 	elif [ -z "$_s" ]; then
     44 		die 'song title not specified'
     45 	fi
     46 	_qanswer="$($fetcher "https://lyrics.fandom.com/api.php?fmt=json&func=getSong&artist=${_a}&song=${_s}")"
     47 	if echo "$_qanswer" | grep -q "'lyrics':'Not found'"; then
     48 		die 'no lyrics found'
     49 	fi
     50 	local _url="$(echo "$_qanswer" | grep "'url'" | \
     51 		sed "s/.*http:/https:/;s/'$//;s/wikia\.com/fandom.com\/wiki/" )"
     52 	if [ "$verbose" = 1 ]; then
     53 		echo "artist: $_a"
     54 		echo "song: $_s"
     55 		echo "query answer: $_qanswer"
     56 		echo "lyricwiki url: $_url"
     57 	fi
     58 	$fetcher "$_url" | grep "class='lyricbox'" | w3m -dump -T text/html
     59 }
     60 
     61 if command -v hurl >/dev/null 2>&1; then
     62 	fetcher=hurl
     63 elif command -v curl >/dev/null 2>&1; then
     64 	fetcher="curl -s"
     65 else
     66 	die 'hurl or curl not installed'
     67 fi
     68 
     69 verbose=0
     70 mpd=0
     71 while :; do
     72 	case "$1" in
     73 		-h)
     74 			show_help
     75 			exit 0
     76 			;;
     77 		-v)
     78 			show_version
     79 			exit 0
     80 			;;
     81 		-V)
     82 			verbose=1
     83 			;;
     84 		-m)
     85 			mpd=1
     86 			;;
     87 		--) # end all options
     88 			shift
     89 			break
     90 			;;
     91 		-?*)
     92 			die 'unknown option specified'
     93 			;;
     94 		*)  # No more options
     95 			break
     96 	esac
     97 	shift
     98 done
     99 
    100 if [ "$mpd" = 1 ]; then
    101 	fetch_lyrics_lyricwiki "$(mpc | head -1)"
    102 else
    103 	if [ $# -lt 1 ]; then
    104 		fetch_lyrics_lyricwiki "$(cat)"
    105 	else
    106 		fetch_lyrics_lyricwiki "$*"
    107 	fi
    108 fi