commit ab87df608675e68c0136dc44cee44f891c4bb382 Author: Jaidyn Ann Date: Tue Dec 15 12:27:04 2020 -0600 Init diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..7c5fb3f --- /dev/null +++ b/README.txt @@ -0,0 +1,32 @@ +=============================================================================== +AZLYRICS-FETCH +=============================================================================== + +Fetch lyrics from azlyrics.com. + +---------------------------------------- +PRE-REQUISITES +---------------------------------------- +* curl +* lynx +* a shell + +---------------------------------------- +USAGE +---------------------------------------- +AZLYRICS +-------------------- +You can get the lyrics of a specific song with + + azlyrics.sh https://www.azlyrics.com/lyrics/$ARTIST/$SONG.html + +You can get a list of all song URLs of an artist with: + + azlyrics.sh https://www.azlyrics.com/$A/$ARTIST.*.html + +---------------------------------------- +BORING STUFF +---------------------------------------- +License is CC-0 +Author is Jaidyn Ann +Sauce is at https://git.feneas.org/detruota/azlyrics-fetch.git diff --git a/azlyrics.sh b/azlyrics.sh new file mode 100755 index 0000000..9cc3b44 --- /dev/null +++ b/azlyrics.sh @@ -0,0 +1,80 @@ +#!/bin/sh +# -------------------------------------- +# name: azlyric.sh +# lisc: CC0 +# date: 2020 +# desc: fetches lyrics from artist/song +# azlyrics URL +# -------------------------------------- + +USER_AGENT="Mozilla/5.0 (Linux; Android 8.1.0) AppleWebKit/537.36 (KHTML, like" +USER_AGENT="${USER_AGENT} Gecko) Chrome/67.0.3396.68 Mobile Safari/537.36" + +function fetch { + local url="$1" + curl -s --user-agent "$USER_AGENT" "$url" +} + +# ====================================== +# ARTIST +# ====================================== +function is_artist_url { + local url="$1" + test -n "$(echo "$url" | awk -F '/' '{print $5}')" \ + -a -z "$(echo "$url" | awk -F '/' '{print $6}')" + return "$?" +} + +function artist_song_urls { + grep "../lyrics/" \ + | sed 's%.*href="..%%' \ + | sed 's%".*%%' \ + | sed 's%^%https://www.azlyrics.com%' +} + +# ====================================== +# SONG +# ====================================== +function is_song_url { + local url="$1" + test -n "$(echo "$url" | awk -F '/' '{print $6}')" + return "$?" +} + +function song_lyrics { + sed '1,/Sorry about that/d' \ + | sed '1,/<\/div>/!d' \ + | sed 's%
%%' \ + | sed 's%<.*>%%' \ + | awk 'BEGIN { print "
" } END { print "
"} {print $0}' \ + | lynx -stdin -dump +} + +# ====================================== +# INVOCATION +# ====================================== +function usage { + echo 'usage: azlyric.sh URL ... URL' + echo + echo 'If a URL is of an artist (e.g., https://www.azlyrics.com/X/X.*.html),' + echo 'then the URLs of each song page will be printed.' + echo + echo 'If a URL is of a song (e.g., https://www.azlyrics.com/lyrics/X/Y.html),' + echo 'then the lyrics will be printed.' + exit 2 +} + +if test -z "$1"; then usage; fi + +for url in $@; do + if is_artist_url "$url"; then + fetch "$url" \ + | artist_song_urls + elif is_song_url "$url"; then + fetch "$url" \ + | song_lyrics + else + echo "Invalid URL: $url" + usage + fi +done