Browse code

General formatting

Jaidyn Lev authored on 2019-01-24 02:44:29
Showing 2 changed files
... ...
@@ -1,3 +1,4 @@
1
+#!/bin/sh
1 2
 ##############################
2 3
 # name: yt-desc
3 4
 # lisc: gnu gplv3
... ...
@@ -7,33 +8,37 @@
7 8
 
8 9
 # Usage: yt-desc "$url/id"
9 10
 
11
+# STRING --> STRING
12
+# Get the description of a YT video, from HTML.
13
+function video_desc {
14
+	local html="$1"
15
+
16
+	echo "$html" \
17
+	| grep "action-panel-details" \
18
+	| sed 's/.*<p .*class="" >//' \
19
+	| sed 's%</p>.*%%' \
20
+	| lynx -stdin -dump \
21
+	| sed 's/^   //'
22
+}
23
+
10 24
 # --------------------------------------
11 25
 # invocation
12 26
 
13
-USAGE="usage: yt-desc url/id"
14
-
15
-if test -z "$1"
16
-then
17
-	echo "$USAGE"
27
+function usage {
28
+	echo "usage: yt-desc url/id"
18 29
 	exit 2
19
-fi
20
-
21
-if test -n "$1"; then
22
-	if echo "$1" | grep "youtube"; then
23
-		URL="$1"
24
-	else
25
-		URL="https://www.youtube.com/watch?v=${1}"
26
-	fi
27
-fi
30
+}
28 31
 
32
+# --------------------------------------
29 33
 
34
+if test -z "$1"; then
35
+	usage
36
+elif echo "$1" | grep "youtube" > /dev/null; then
37
+	URL="$1"
38
+else
39
+	URL="https://www.youtube.com/watch?v=${1}"
40
+fi
30 41
 
31 42
 # --------------------------------------
32
-# invocation <3
33
-
34
-gendl "$URL" \
35
-| grep "action-panel-details" \
36
-| sed 's/.*<p .*class="" >//' \
37
-| sed 's%</p>.*%%' \
38
-| lynx -stdin -dump \
39
-| sed 's/^   //'
43
+
44
+video_desc "$(gendl "$URL")"
... ...
@@ -1,3 +1,4 @@
1
+#!/bin/sh
1 2
 ##############################
2 3
 # name: yt-search
3 4
 # lisc: gnu gplv3
... ...
@@ -12,8 +13,7 @@
12 13
 
13 14
 # NUMBER NUMBER --> NUMBER
14 15
 # Well, subtraction. Y'know...
15
-function subtract
16
-{
16
+function subtract {
17 17
 	local operated=$1
18 18
 	local operatee=$2
19 19
 	echo "${1}-${2}" | bc
... ...
@@ -26,29 +26,25 @@ function subtract
26 26
 
27 27
 # NIL --> STRING
28 28
 # Print an ANSI "unbold" escape string.
29
-function unbold
30
-{
29
+function unbold {
31 30
 	printf "$(tput sgr0)"
32 31
 }
33 32
 
34 33
 # STRING --> STRING
35 34
 # Print an ANSI "bold" escape string.
36
-function bold
37
-{
35
+function bold {
38 36
 	printf "$(tput bold)"
39 37
 }
40 38
 
41 39
 # STRING --> NUMBER
42 40
 # Return the length of a string
43
-function char_length
44
-{
41
+function char_length {
45 42
 	subtract $(echo $1 | wc -c) 1
46 43
 }
47 44
 
48 45
 # STRING NUMBER [STRING] --> STRING
49 46
 # pad string $1 out to the minimum length $2 with padding $3 (default: "0")
50
-function min_string_length
51
-{
47
+function min_string_length {
52 48
 	local string="$1"
53 49
 	local length=$2
54 50
 	if test -z "$3"; then
... ...
@@ -69,8 +65,7 @@ function min_string_length
69 65
 
70 66
 # STRING NUMBER --> STRING
71 67
 # get the first $2 characters in string $1
72
-function char_get
73
-{
68
+function char_get {
74 69
 	local number=$2
75 70
 	local string="$1"
76 71
 	string="$(min_string_length "$string" $number " ")"
... ...
@@ -85,8 +80,7 @@ function char_get
85 80
 
86 81
 # STRING --> STRING
87 82
 # Pass the raw search HTML, get raw video result-lines
88
-function result_lines
89
-{
83
+function result_lines {
90 84
 	local search_html="$1"
91 85
 	echo "$search_html" \
92 86
 	| grep "<a href=\"\/watch?v=" \
... ...
@@ -97,16 +91,14 @@ function result_lines
97 91
 
98 92
 # STRING --> STRING 
99 93
 # Return the video ID of a result_line.
100
-function result_id
101
-{
94
+function result_id {
102 95
 	local result_line="$1"
103 96
 	echo "$result_line" \
104 97
 	| sed 's%.*<a href="/watch?v=%%' \
105 98
 	| sed 's%".*%%'
106 99
 }
107 100
 
108
-function result_url
109
-{
101
+function result_url {
110 102
 	local result_line="$1"
111 103
 	local id="$(result_id "$result_line")"
112 104
 	echo "https://youtube.com/watch?v=${id}"
... ...
@@ -114,33 +106,23 @@ function result_url
114 106
 
115 107
 # STRING --> STRING
116 108
 # Return the title of a result-line.
117
-function result_title
118
-{
109
+function result_title {
119 110
 	local result_line="$1"
120 111
 	echo "$result_line" \
121 112
 	| sed 's%.*"  title="%%' \
122 113
 	| sed 's%".*%%'
123 114
 }
124 115
 
125
-function result_name
126
-{
116
+function result_name {
127 117
 	local result_line="$1"
128 118
 	local title="$(result_title "$result_line")"
129 119
 	echo "$(bold)${title}$(unbold)"
130 120
 }
131 121
 
132
-function result_title_backup
133
-{
134
-	local result_line="$1"
135
-	echo "$result_line" \
136
-	| sed 's%.*title="%%' \
137
-	| sed 's%".*%%'
138
-}
139 122
 
140 123
 # STRING --> STRING
141 124
 # Return the duration of a result-line.
142
-function result_duration
143
-{
125
+function result_duration {
144 126
 	local result_line="$1"
145 127
 	echo "$result_line" \
146 128
 	| sed 's%.*> - Duration: %%' \
... ...
@@ -149,8 +131,7 @@ function result_duration
149 131
 
150 132
 # STRING --> STRING
151 133
 # Return the .yt-lockup-meta-info <ul> of a result-line
152
-function result_meta_ul
153
-{
134
+function result_meta_ul {
154 135
 	local result_line="$1"
155 136
 	echo "$result_line" \
156 137
 	| sed 's%.*<ul class="yt-lockup-meta-info">%%' \
... ...
@@ -159,8 +140,7 @@ function result_meta_ul
159 140
 
160 141
 # STRING --> STRING
161 142
 # Return the "Uploaded..." string of a result-line
162
-function result_uploaded
163
-{
143
+function result_uploaded {
164 144
 	local result_line="$1"
165 145
 	local result_meta_ul="$(result_meta_ul "$result_line")"
166 146
 	echo "$result_meta_ul" \
... ...
@@ -170,8 +150,7 @@ function result_uploaded
170 150
 
171 151
 # STRING --> NUMBER
172 152
 # Return the view-count of a result-line
173
-function result_views
174
-{
153
+function result_views {
175 154
 	local result_line="$1"
176 155
 	local result_meta_ul="$(result_meta_ul "$result_line")"
177 156
 	echo "$result_meta_ul" \
... ...
@@ -182,8 +161,7 @@ function result_views
182 161
 
183 162
 # STRING --> STRING
184 163
 # Format a result-line into a mediumly-pretty, one-line string~
185
-function result_format_compact
186
-{
164
+function result_format_compact {
187 165
 	local result_line="$1"
188 166
 	local title="$(char_get "$(result_name "$result_line")" 40)$(unbold)"
189 167
 	local id="$(result_id "$result_line")"
... ...
@@ -193,8 +171,7 @@ function result_format_compact
193 171
 
194 172
 # STRING --> STRING
195 173
 # Format a result-line into a mediumly-pretty, one-line string~
196
-function result_format_small
197
-{
174
+function result_format_small {
198 175
 	local result_line="$1"
199 176
 	local name="$(char_get "$(result_name "$result_line")" 40)$(unbold)"
200 177
 	local url="$(result_url "$result_line")"
... ...
@@ -204,8 +181,7 @@ function result_format_small
204 181
 
205 182
 # STRING --> STRING
206 183
 # Format a result-line into a mediumly-pretty, one-line string~
207
-function result_format_medium
208
-{
184
+function result_format_medium {
209 185
 	local result_line="$1"
210 186
 	local name="$(char_get "$(result_name "$result_line")" 40)$(unbold)"
211 187
 	local duration="$(result_duration "$result_line")"
... ...
@@ -216,8 +192,7 @@ function result_format_medium
216 192
 
217 193
 # STRING --> STRING
218 194
 # Format a result-line into a pretty string~
219
-function result_format_big
220
-{
195
+function result_format_big {
221 196
 	local result_line="$1"
222 197
 	local name="$(char_get "$(result_name "$result_line")" 79)$(unbold)"
223 198
 	local duration="$(result_duration "$result_line")"
... ...
@@ -232,49 +207,44 @@ function result_format_big
232 207
 
233 208
 
234 209
 # --------------------------------------
235
-# cushiness, ensuring valid input, etc.
236
-
237
-USAGE="usage: yt-search [-csmb] query"
210
+# invocation <3
238 211
 
239
-if test -z "$1"
240
-then
241
-	echo "$USAGE"
212
+function usage {
213
+	echo "usage: yt-search [-csmb] query"
242 214
 	exit 2
243
-fi
215
+}
244 216
 
245
-user_query="$1"
246
-format="big"
217
+# --------------------------------------
247 218
 
248
-if test -n "$2"
249
-then
250
-	user_query="$2"
219
+if test -z "$1"; then
220
+	usage
221
+elif test -n "$2"; then
222
+	QUERY="$2"
251 223
 	case "$1" in
252
-	"-c") format="compact" ;;
253
-	"-s") format="small" ;;
254
-	"-m") format="medium" ;;
255
-	"-b") format="big" ;;
224
+		"-c") FORMAT="compact" ;;
225
+		"-s") FORMAT="small" ;;
226
+		"-m") FORMAT="medium" ;;
227
+		"-b") FORMAT="big" ;;
256 228
 	esac
229
+else
230
+	QUERY="$1"
231
+	FORMAT="big"
257 232
 fi
258 233
 
259
-
260
-
261 234
 # --------------------------------------
262
-# invocation <3
263 235
 
264
-query="$(echo "$user_query" | sed 's/ /+/g')"
265
-search_html="$(./gendl "https://youtube.com/results?search_query=$query")"
236
+QUERY="$(echo "$QUERY" | sed 's/ /+/g')"
237
+search_html="$(gendl "https://youtube.com/results?search_query=$QUERY")"
266 238
 result_lines="$(result_lines "$search_html")" 
267 239
 
268
-
269 240
 IFS='
270 241
 '
271 242
 
272
-for result in $result_lines
273
-do
274
-	case "$format" in
275
-	"compact") result_format_compact "$result" ;;
276
-	"small") result_format_small "$result" ;;
277
-	"medium") result_format_medium "$result" ;;
278
-	"big") result_format_big "$result" ;;
243
+for result in $result_lines; do
244
+	case "$FORMAT" in
245
+		"compact") result_format_compact "$result" ;;
246
+		"small") result_format_small "$result" ;;
247
+		"medium") result_format_medium "$result" ;;
248
+		"big") result_format_big "$result" ;;
279 249
 	esac
280 250
 done