This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
letrax.sh
executable file
·94 lines (85 loc) · 3.23 KB
/
letrax.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# This file is part of LetraX
# LetraX is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# LetraX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. <http://www.gnu.org/licenses/>
#
# Author(s):
# © 2018 Kasra Madadipouya <[email protected]>
#!/bin/bash
function main() {
letrax
}
function show_search_menu() {
back_title="LetraX -- Ncurses interface for AZLyrics.com"
search_query=$(dialog --keep-tite --backtitle "$back_title" --nocancel --output-fd 1 --inputbox "Search for a song or artist:" 8 70 | sed -e "s/ /+/g")
query_results=$(curl -s "https://search.azlyrics.com/search.php?q=$search_query" | hxnormalize -x | hxselect 'table.table.table-condensed tbody tr td.text-left.visitedlyr' | hxselect 'td')
}
function extract_search_results_index() {
IFS=$'\n'
links=($(echo $query_results | grep -Po '(?<=href=")[^"]*'))
songs_artists=($(echo $query_results | grep -oE '<b>[^<]*</b>' | sed 's/<b>//g' | sed 's/<\/b>//g'))
options=()
j=-1
for ((i = 0; i < ${#links[@]}; ++i)); do
j=$(( $j + 1 ))
song=$(echo "${songs_artists[$j]}" | tr -s " ")
j=$(( $j + 1 ))
artist=$(echo ${songs_artists[$j]} | tr -s " ")
song_artist="$song, $artist"
options+=($(( $i + 1 )))
options+=($song_artist)
done
}
function show_search_results() {
song_index=$(dialog --keep-tite --backtitle $back_title --title "Search Result" --scrollbar --cancel-label "BACK" --menu --output-fd 1 "Select a lyrics:" 30 100 30 ${options[*]})
keypressed=$?
if [ $keypressed -eq 255 ]
then
quit
elif [ $keypressed -eq 1 ]
then
letrax
elif [ $keypressed -eq 0 ]
then
show_selected_lyrics
fi
}
function show_selected_lyrics() {
song_index=$(( $song_index - 1 ))
result=$(curl -s "${links[$song_index]}" -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://search.azlyrics.com/search.php?q=man+of+the+year' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' | hxnormalize -x)
lyrics=$(echo $result | hxselect "div.col-xs-12:nth-child(2) > div:nth-child(8)")
if [ -z "$lyrics" ]
then
lyrics=$(echo $result | hxselect "div.col-xs-12:nth-child(2) > div:nth-child(10)")
fi
lyrics=$(echo $lyrics | sed 's/<div>//g' | sed 's/<\/div>//g' | sed 's/<br\/>/\n/g' | sed 's/<br>//g' | sed 's/<\/br>/\n/g' | tr -s " ")
title_index=$(( $song_index * 2 + 1 ))
dialog --keep-tite --backtitle $back_title --title ${options[$title_index]} --scrollbar --yes-label "OK" --no-label "BACK" --yesno "$lyrics" 35 100
keypressed=$?
if [ $keypressed -eq 255 ]
then
quit
elif [ $keypressed -eq 1 ]
then
show_search_results
elif [ $keypressed -eq 0 ]
then
letrax
fi
unset IFS
}
function letrax() {
show_search_menu
extract_search_results_index
show_search_results
show_selected_lyrics
}
function quit() {
kill -INT $$
}
main "$@"