#!/bin/sh [ "$#" -eq 0 ] && { echo "Usage: lyry [options] Options: None Retrive top first result -f Select from results -i Retrieves artist, song title, album, and link -u Update frontend instance by looking for a healthy instance -n Number of pages to query, 10 results/page (e.g., -n 3 = 30 results, 3 pages) "; exit 1; } instance="https://intellectual.insprill.net"; fzf=0; info=0; lastpage=1 [ -z "$1" ] && { echo "Error: No search query."; exit 1; } sedz()( sed -e "s/'/'/g" -e 's/&/\&/g' -e "s/"/\"/g"; ) infos()( echo "$html" | sedz | sed -n -e '/

/s:.*

\(.*\)

.*:\1:p' -e '//s:.*\(.*\).*:\1:p' | awk 'NR==1{title=$0} NR==2{artist=$0} NR==3{album=$0} END{print artist " - " title; if (album) print "Album: " album}'; ) while getopts "fin:u" opt; do case $opt in f) fzf=1 ;; i) info=1 ;; n) lastpage="$OPTARG" ;; u) curl -s "$instance/search?q=test" | grep -q "class=\"song" && echo "Current instance '$instance' already works" && exit 0; [ -w "$0" ] || { echo "Error: You cannot perform this operation unless you are root."; exit 1; } curl -sSL "https://raw.githubusercontent.com/Insprill/intellectual/c9f7766f507a18f5cf581fcabd148b3dcf5e8ba1/instances.json" | grep clearnet | sed -n 's/.* "\([^"]*\)\/".*/\1/p' | while read -r i; do curl -s "${i}/search?q=test" | grep -q "class=\"song" && echo "Instance found: $i" && sed -i "s%instance=\"[^\"]*\"%instance=\"$i\"%g" "$0" && break done; exit 1 ;; esac done shift $((OPTIND-1)) page=1 search=$(while curl -ssL "$instance/search?q=$(printf '%s' "$*" | sed 's/ /%20/g')&page=$page" | grep "class=\"song" | grep -vE "thumbnail|list|views" | awk '/annotated\?id=/{skip=2; next} skip{skip--; next} 1'; do page=$((page + 1)); [ $page -gt $lastpage ] && break; done) [ -n "$search" ] || { echo "No results for query or no internet connection."; exit 1; } [ $fzf -eq 1 ] && grepz() { sedz | awk -v inst="$instance" '//,"");link=$0}/

/{gsub(/.*

/,"");gsub(/<\/h2>.*/,"");title=$0}/

/{gsub(/.*

/,"");gsub(/<\/h3>.*/,"");artist=$0;header=artist" - "title;hl=length(header);pad=(hl>100?hl+25:100)-hl;printf "%s%"pad"s%s\n",header,"",inst link}' | fzf --exact --reverse --no-sort | awk '{print $NF}'; } || grepz()( grep "class=\"song\"" | grep lyrics | sed -n 's/.*href="\([^"]*\)".*/\1/p' | awk -v inst="$instance" '{print inst $0}' | head -n 1; ) selected="$(echo "$search" | grepz)" html="$(curl -ssL "$selected" 2>/dev/null)" lyrics="$(echo "$html" | sed -n '/song-lyric/{n; :a; p; n; ba}' | grep -E '|<[^>]+>[^<]+]+>' | sed -n '/song-lyric/,$p' | grep -E '|<[^>]+>[^<]+]+>' | sed -E 's/<[^>]+>//g' | sed 's/^[[:space:]]*//' | sedz | sed -e 's/<[^>]*>//g')" [ $info -eq 1 ] && [ -n "$selected" ] && infos && echo "$selected\n" [ -z "$lyrics" ] && { [ -z "$selected" ] && { echo "Error: No result selected."; exit 1; } || infos && echo "$selected\n" && echo "Error: Possible Timeout. Try again."; } || printf "%s\n" "$lyrics"