commit 7274d19cb452264dbc6ccade4ed51c98df4bddbd
parent 7ec402f5893d54c58246ee63d8b50027cd6c260c
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Wed, 15 Jan 2020 10:34:42 +0100
Add script to fetch and parse lyrics
Diffstat:
A | .local/bin/lyrics | | | 114 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 114 insertions(+), 0 deletions(-)
diff --git a/.local/bin/lyrics b/.local/bin/lyrics
@@ -0,0 +1,114 @@
+#!/bin/sh
+
+version=0.1.0
+
+show_help()
+{
+ echo "usage: ${0##*/} [OPTIONS] [[artist -] song]"
+ echo "fetches lyrics from lyricwiki."
+ echo
+ echo "OPTIONS are one or more of the following:"
+ echo " -h, --help show this message"
+ echo " -v, --version show version and license information"
+ echo " -V, --verbose show version and license information"
+ echo " -m, --mpd fetch lyrics for song currently playing on mpd"
+ echo " -- do not consider any following args as options"
+}
+
+show_version()
+{
+ echo "${0##*/} version $version"
+ echo "Licensed under the ISC License"
+ echo "written by Anders Damsgaard, anders@adamsgaard.dk"
+ echo "https://src.adamsgaard.dk/dotfiles"
+}
+
+die()
+{
+ printf '%s: error: %s\n' "${0##*/}" "$1" >&2
+ exit 1
+}
+
+string_to_artist_and_song()
+{
+ artist="${1% - *}"
+ song="$(echo "$1" | sed 's/.* - //')"
+ if [ "$artist" = "$song" ]; then
+ artist=""
+ fi
+}
+
+fetch_lyrics_lyricwiki()
+{
+ local _a="$(echo "$1" | sed 's/ /_/g')"
+ local _s="$(echo "$2" | sed 's/ /_/g')"
+ local _url="$($fetcher "https://lyrics.fandom.com/api.php?fmt=json&func=getSong&artist=${_a}&song=${_s}" | \
+ grep "'url'" | sed "s/.*http:/https:/;s/'$//;s/wikia\.com/fandom.com\/wiki/" )"
+ if [ "$verbose" = 1 ]; then
+ echo "artist: $_a"
+ echo "song: $_s"
+ echo "lyricwiki url: $_url"
+ fi
+ $fetcher "$_url" | grep "class='lyricbox'" | w3m -dump -T text/html
+}
+
+fetch_lyrics()
+{
+ if [ "$backend" = "lyricwiki" ]; then
+ fetch_lyrics_lyricwiki "$1" "$2"
+ else
+ die "unknown backend: $backend"
+ fi
+}
+
+if command -v hurl >/dev/null 2>&1; then
+ fetcher=hurl
+elif command -v curl >/dev/null 2>&1; then
+ fetcher=curl
+else
+ die 'hurl or curl not installed'
+fi
+
+verbose=0
+mpd=0
+backend="lyricwiki"
+while :; do
+ case "$1" in
+ -h|-\?|--help)
+ show_help
+ exit 0
+ ;;
+ -v|--version)
+ show_version
+ exit 0
+ ;;
+ -V|--verbose)
+ verbose=1
+ ;;
+ -m|--mpd)
+ mpd=1
+ ;;
+ --) # end all options
+ shift
+ break
+ ;;
+ -?*)
+ die 'unknown option specified'
+ ;;
+ *) # No more options
+ break
+ esac
+ shift
+done
+
+if [ "$mpd" = 1 ]; then
+ string_to_artist_and_song "$(mpc | head -1)"
+else
+ if [ $# -lt 1 ]; then
+ string_to_artist_and_song "$(cat)"
+ else
+ string_to_artist_and_song "$*"
+ fi
+fi
+
+fetch_lyrics "$artist" "$song"