Jaidyn Lev authored on 2019-01-21 05:41:10
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,59 @@
1
+===============================================================================
2
+WYRICS                                                          Git some lyrics
3
+===============================================================================
4
+
5
+Fetch lyrics for a song from lyrics.wikia.com with this script.
6
+You can fetch them by search queries, or by their URL.
7
+
8
+
9
+
10
+----------------------------------------
11
+PRE-REQUISITES
12
+----------------------------------------
13
+You'll need:
14
+	* "gendl" in your $PATH
15
+	* a POSIX-compatible shell (tested with `pdksh` and `bash`)
16
+	* "curl", "wget", or "ftp" installed
17
+	* "lynx" installed
18
+
19
+
20
+
21
+----------------------------------------
22
+USAGE
23
+----------------------------------------
24
+Just run "wyrics" like so:
25
+
26
+	wyrics -s "query"
27
+	wyrics -u page_url
28
+
29
+If you use the "-u" option, the lyrics will just be printed to stdout.
30
+
31
+If you decide to use "-s", though, search results'll be displayed to your
32
+screen (via stderr)-- each numbered from 1-10.
33
+Just type a number, hit enter, and the lyrics'll pop up.
34
+
35
+	$ wyrics -s "boston"
36
+	1       Boston
37
+	2       Sonny:We're Better Than Boston
38
+	3       Boston Manor:FY1
39
+	4       Boston Manor:Halo
40
+	5       Boston Manor:Wolf
41
+	6       Boston Manor:Stick Up
42
+	7       Boston Manor:If I Can't Have It No One Can
43
+	8       Boston Manor:Square One
44
+	9       Boston Manor:Driftwood
45
+	10      Boston Manor:Forget Me Not
46
+	>>
47
+
48
+
49
+If you want to save lyrics to a file, you'll need to redirect the
50
+output-- "wyrics -s 'bob' > BOB.txt"
51
+
52
+
53
+
54
+----------------------------------------
55
+BORING STUFF
56
+----------------------------------------
57
+License is CC-0 (though, "gendl" is GPLv3)
58
+Author is Jenga Phoenix <jadedctrl@teknik.io>
59
+Sauce is at https://git.eunichx.us/wyrics
0 60
new file mode 100755
... ...
@@ -0,0 +1,155 @@
1
+#!/bin/sh
2
+##############################
3
+# name: gendl
4
+# lisc: gnu gplv3
5
+# desc: download to stdout or
6
+#       file, independent of
7
+#       a specific downloader.
8
+#       ftp/wget/curl support.
9
+# main: jadedctrl
10
+##############################
11
+
12
+# usage: gendl [-o output] url
13
+
14
+
15
+
16
+#---------------------------------------
17
+# backend bits
18
+
19
+# NIL --> STRING
20
+# return the download program you're using
21
+function download_program
22
+{
23
+	programs="ftp curl wget"
24
+	current=""
25
+
26
+	for program in $programs
27
+	do
28
+		if whereis $program > /dev/null
29
+		then
30
+			current=$program
31
+		fi
32
+	done
33
+
34
+	if uname -s | grep -e "LibertyBSD" -e "OpenBSD" > /dev/null
35
+	then
36
+		current="ftp"
37
+	fi
38
+
39
+	echo "$current"
40
+}
41
+
42
+# STRING PATH --> NIL
43
+# download URL $1 to stdout
44
+function download_stdout
45
+{
46
+	program=$(download_program)
47
+	url=$1
48
+
49
+	case "$program" in
50
+		"ftp")
51
+			output="$(ftp -VMo- $url)"
52
+			;;
53
+		"curl")
54
+			output="$(curl $url)"
55
+			;;
56
+		"wget")
57
+			output="$(wget --quiet -O $url)"
58
+			;;
59
+	esac
60
+
61
+	return_code=$?
62
+
63
+	echo "$output"
64
+	return $return_code
65
+}
66
+
67
+# STRING PATH --> NIL
68
+# download URL $1 to path $2
69
+function download_file
70
+{
71
+	program=$(download_program)
72
+	url=$1
73
+	path=$2
74
+
75
+	case "$program" in
76
+		"ftp")
77
+			ftp -VMU "." -o $path $url
78
+			;;
79
+		"curl")
80
+			curl -o $path $url
81
+			;;
82
+		"wget")
83
+			wget --quiet -O $path $url
84
+			;;
85
+	esac
86
+
87
+	return_code=$?
88
+
89
+	if test $return_code -ne 0 2>/dev/null
90
+	then
91
+		rm $path 2> /dev/null
92
+		# for consistency in behavior; wget saves 404s anyway, whereas
93
+		# ftp doesn't save anything from 404s, etc.
94
+	fi
95
+
96
+	return $return_code
97
+}
98
+
99
+
100
+
101
+# --------------------------------------
102
+# front-end string-manip
103
+
104
+# STRING --> STRING
105
+# return the last word in a string
106
+function last_word
107
+{
108
+	string="$1"
109
+
110
+	echo "$string" \
111
+	| rev \
112
+	| sed 's% .*%%' \
113
+	| rev
114
+}
115
+
116
+function usage
117
+{
118
+	echo "usage: gendl [-o output] URL"
119
+}
120
+
121
+
122
+
123
+# --------------------------------------
124
+# invocation
125
+
126
+args="$(getopt o: $*)"
127
+if test -z "$@" 2>/dev/null
128
+then
129
+	usage
130
+	exit 2
131
+fi
132
+
133
+
134
+set -- $args
135
+while test $# -ne 0
136
+do
137
+	case "$1"
138
+	in
139
+		-o)
140
+			download_path="$2"; shift; shift;;
141
+		--)
142
+			shift; break;;
143
+	esac
144
+done
145
+
146
+
147
+url="$(last_word "$@")"
148
+
149
+
150
+if test -n "$download_path" 2> /dev/null
151
+then
152
+	download_file $url $download_path
153
+else
154
+	download_stdout "$url"
155
+fi
0 156
new file mode 100755
... ...
@@ -0,0 +1,228 @@
1
+#!/bin/sh
2
+########################################
3
+# name: wyrics
4
+# desc: fetch lyrics by URL or query
5
+#       from lyrics.wikia.com
6
+# main: Jenga Phoenix
7
+#       <jadedctrl@teknik.io>
8
+# lisc: CC 0
9
+########################################
10
+
11
+# --------------------------------------
12
+# generic
13
+
14
+# NIL --> STRING
15
+# read from stdin until eof hit; return all input
16
+# good for writing functions that take piped info
17
+function reade {
18
+        local stack=""
19
+
20
+        while read input; do
21
+		stack="$(printf '%s\n%s' "$stack" "$input")"
22
+	done
23
+
24
+	echo "$stack"
25
+}
26
+
27
+# NUMBER STRING --> [BOOLEAN]
28
+# have the user choose a number, up to a given max, with prompt.
29
+function number_choose {
30
+	local max=$1
31
+	local prompt="$2"
32
+
33
+	printf "$prompt" >&2
34
+	read response
35
+
36
+	if test $response -lt $max; then
37
+		return $response
38
+	else
39
+		number_choose $max "$prompt"
40
+	fi
41
+}
42
+
43
+# NUMBER NUMBER --> NUMBER
44
+# add two numbers together. pretty obvious.
45
+function add {
46
+	local operator=$1
47
+	local operatee=$2
48
+
49
+	echo "$1 + $2" \
50
+	| bc
51
+}
52
+
53
+# NUMBER NUMBER --> NUMBER
54
+# subtract two numbers. pretty obvious.
55
+function subtract {
56
+	local operator=$1
57
+	local operatee=$2
58
+
59
+	echo "$1 - $2" \
60
+	| bc
61
+}
62
+
63
+
64
+
65
+# --------------------------------------
66
+# searching
67
+
68
+# STRING --> STRING
69
+# return a result-list from a search query
70
+function search {
71
+	local query="$1"
72
+	local query="$(echo "$query" | sed 's/ /+/g')"
73
+
74
+	gendl "https://lyrics.wikia.com/wiki/Special:Search?query=${query}" \
75
+	| sed '1,/Results/d' \
76
+	| grep "result-link" \
77
+	| search_parse
78
+}
79
+
80
+# |STRING  --> STRING
81
+# take slightly-trimmed search HTML and turn it into result-list
82
+function search_parse {
83
+	local html="$(reade)"
84
+
85
+	local stack=""
86
+	local i=0
87
+	IFS='
88
+'
89
+	for line in $html; do
90
+		i=$(add $i 1)
91
+		local url="$(echo "$line" \
92
+				| sed 's/.*href=\"//' \
93
+				| sed 's/\" class.*//')"
94
+		local title="$(echo "$line" \
95
+				| sed 's%.*\" >%%' \
96
+				| sed 's%</a>.*%%')"
97
+
98
+		if echo "$title" | grep "http" > /dev/null; then
99
+			i=$(subtract $i 1)
100
+		else
101
+			stack="$(printf '%s\n%s@%s@%s' \
102
+				"$stack"  "$i" "$url" "$title")"
103
+		fi
104
+	done
105
+
106
+	echo "$stack" \
107
+	| head -11
108
+}
109
+
110
+
111
+
112
+# --------------------------------------
113
+# result manipulation
114
+
115
+# |STRING --> STRING
116
+# return the title of a result
117
+function result_title {
118
+	local result="$(reade)"
119
+
120
+	echo "$result" \
121
+	| awk -F "@" '{ print $3 }' \
122
+	| tr -d '\n'
123
+}
124
+
125
+# |STRING --> STRING
126
+# return the ID of a result
127
+function result_index {
128
+	local result="$(reade)"
129
+
130
+	echo "$result" \
131
+	| awk -F "@" '{ print $1 }' \
132
+	| tr -d '\n'
133
+}
134
+
135
+# |STRING --> STRING
136
+# return the URL of a result
137
+function result_url {
138
+	local result="$(reade)"
139
+
140
+	echo "$result" \
141
+	| awk -F "@" '{ print $2 }' \
142
+	| tr -d '\n'
143
+}
144
+
145
+# |STRING [STRING] --> STRING
146
+# display all results in a result-list to stderr.
147
+# if second argument is 'long', then URLs are printed, too.
148
+function results_display {
149
+	local results="$(reade)"
150
+	local long="$1"
151
+	
152
+	IFS='
153
+'
154
+	for result in $results; do
155
+		local index="$(echo "$result" | result_index)"
156
+		local title="$(echo "$result" | result_title)"
157
+		local url="$(echo "$result" | result_url)"
158
+
159
+		echo "$index	$title" >&2
160
+		if test "$long" = "long"; then
161
+			echo "$url" >&2
162
+		fi
163
+	done
164
+}
165
+
166
+
167
+# STRING --> STRING
168
+# have the user choose a single result from the result-list
169
+function result_choose {
170
+	local results="$1"
171
+	local max="$(echo "$results" | wc -l)"
172
+
173
+	number_choose $max ">> "
174
+	local response=$?
175
+
176
+	echo "$results" \
177
+	| grep "^$response@"
178
+}
179
+
180
+
181
+
182
+# --------------------------------------
183
+
184
+# STRING --> STRING
185
+# return the lyrics of a given URL
186
+function lyrics {
187
+	local url="$1"
188
+
189
+	gendl "$url" \
190
+	| grep "class=\'lyricbox" \
191
+	| sed 's/.*lyricbox.>//' \
192
+	| sed 's/;<div.*//' \
193
+	| lynx -stdin -dump \
194
+	| sed 's/^   //g'
195
+}
196
+
197
+
198
+
199
+# --------------------------------------
200
+# invocation
201
+
202
+# NIL --> STRING
203
+# print usage and abort
204
+function usage {
205
+	echo "usage: wyrics -s query"
206
+	echo "       wyrics -u url"
207
+	exit 2
208
+}
209
+
210
+# --------------------------------------
211
+
212
+case "$1" in
213
+	"-u")
214
+		lyrics "$2"
215
+		;;
216
+	"-s")	
217
+		results="$(search "$2")"
218
+
219
+		echo "$results" | results_display
220
+		url="$(result_choose "$results" | result_url)"
221
+
222
+		lyrics "$url"
223
+		;;
224
+	*)
225
+		usage
226
+		;;
227
+
228
+esac