-
Notifications
You must be signed in to change notification settings - Fork 0
/
azlyrics.sh
executable file
·80 lines (71 loc) · 1.85 KB
/
azlyrics.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
#!/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%<br>%%' \
| sed 's%<.*>%%' \
| awk 'BEGIN { print "<pre>" } END { print "</pre>"} {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