-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlyrics.zsh
More file actions
executable file
·638 lines (568 loc) · 18.8 KB
/
Copy pathlyrics.zsh
File metadata and controls
executable file
·638 lines (568 loc) · 18.8 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#!/usr/bin/env zsh
all_lyrics_websites="metrolyrics songlyrics darklyrics makeitpersonal genius \
azlyrics flashlyrics lyricsfreak musixmatch songtexte versuri"
# If lyrics envvar is not exported by user, consider all the sources,
# otherwise split the envvar string into an array. The -w option, if
# specified, makes both both these assignments obsolete.
: ${(A)LYRICS_WEBSITES:=${=all_lyrics_websites}}
: ${(A)LYRICS_WEBSITES::=${=LYRICS_WEBSITES}}
SELF=$(basename "$0")
help_str="$SELF - All the lyrics in one place
Usage: $SELF [-efdlrwsh] [string file folder]
Search and save song lyrics using one of the sources:
string - A free-form string containing artist name, song name, part of
the searched lyrics or any combination of the above. This is
the only option that does *not* save the lyrics for later retrieval.
file - A media file containing artist/song title info (mp3 supported)
folder - A folder containing media file(s).
- With no source given, get the info from the media player, if
supported and running (cmus and moc supported).
Options:
-d <folder> Run in daemon mode. Monitor the given folder for new media files
and update the database with the lyrics, if found.
-e <str> Replace the \"lyrics\" string that is appended by default to the
search string. This can help increase the chance of finding the lyrics. Only
has an effect for free-form searches
-r Raw mode. Do not remove tokens like \"live\", \"acoustic version\", etc.
from the song titles. These tokens are removed by default from the
song titles to increase the chance of finding the lyrics.
-w <str1 str2 ...> Manually select the order and the name of the websites used
to extract the lyrics from. Alternatively, the LYRICS_WEBSITES
variable can be exported. (Example: $SELF -w \"metrolyrics azlyrics\")
-W List all the websites from which the application can extract the lyrics.
-s Only save the lyrics but do not print them to stdout. If the lyrics already
exist in the database, do nothing. Enabled by default when the source is
a folder.
-g Display websites usage statistics.
-G Clear websites usage statistics.
-l Display the website where the last lyrics were found.
-h Print this help and exit"
# Extract the contents of any html tag. Pure magic!
alias hxmagic="hxnormalize -x | hxselect -c"
mycurl() {
local url=$1
local user_agent="User-Agent: Mozilla/5.0 (Macintosh; \
Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML,\
like Gecko) Chrome/44.0.2403.89 Safari/537."
curl -sLH $user_agent $1
}
clean_string() {
local dirty=$1
# Remove all tags, leading/trailing spaces and duplicate empty lines.
echo $dirty | sed -e 's/<[^>]*>//g' | sed 's/^ *//; s/ *$//' | cat -s
}
rm_extra_spaces() {
echo $1 | sed 's/^[[:space:]]*//; s/[[:space:]] *$//;
s/[[:space:]][[:space:]]*/ /g'
}
# Cleanup an artist or song item.
clean_search_item() {
local cleanup_tokens=(demo live acoustic remix bonus)
local item=$1
item=${item//[.,?!\/\']/} # remove special chars
item=$(rm_extra_spaces $item) # no extra spaces
item=${item:l} # lowercase
for token in $cleanup_tokens; do # remove tokens
item=$(echo $item | sed "s/ *( *$token.*) *//")
done
echo $item
}
makeitpersonal() {
local artist title lyrics url template err_strings
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /-}
title=${2// /-}
template="https://makeitpersonal.co/lyrics?artist=%s&title=%s"
url=$(printf $template $artist $title)
fi
lyrics=$(curl -s $url)
err_strings=("Sorry, We don't have lyrics for this song yet" \
"title is empty" \
"artist is empty" \
"Something went wrong")
for err_str in $err_strings; do
if [[ $lyrics =~ ".*$err_str.*" ]]; then
lyrics=""
fi
done
echo -n $lyrics
}
songlyrics() {
local artist title lyrics url template err_strings
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /-}
title=${2// /-}
template="http://www.songlyrics.com/%s/%s-lyrics/"
url=$(printf $template $artist $title)
fi
lyrics=$(curl -s $url | hxmagic 'p.songLyricsV14')
err_strings=("Sorry, we have no" \
"We do not have the lyrics for")
for err_str in $err_strings; do
if [[ $lyrics =~ ".*$err_str.*" ]]; then
lyrics=""
fi
done
echo -n $lyrics
}
metrolyrics() {
local artist title lyrics url template
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /-}
title=${2// /-}
template="http://www.metrolyrics.com/%s-lyrics-%s.html"
url=$(printf $template $title $artist)
fi
curl -s $url | hxmagic -s '\n\n' 'p.verse'
}
genius() {
local artist title url template
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /-}
title=${2// /-}
template="https://genius.com/%s-%s-lyrics"
url=$(printf $template $artist $title)
fi
curl -sL $url | hxmagic 'lyrics.lyrics'
}
azlyrics() {
local artist title url template
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /}
title=${2// /}
template="http://www.azlyrics.com/lyrics/%s/%s.html"
url=$(printf $template $artist $title)
fi
mycurl $url | awk '/Usage of azlyrics.com/{f=1; next}/<!--/{f=0}f==1'
}
flashlyrics() {
local artist title raw_title url
local suburl artist_template template
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /-}
title=${2// /-}
raw_title=$3
artist_template="https://www.flashlyrics.com/lyrics/%s"
url=$(printf $artist_template $artist)
url=$(curl -s $url | grep "href..*$raw_title" | hxwls | head -n1)
fi
mycurl $url 2>/dev/null | hxnormalize -x | \
awk '/main-panel-content/, /sharebar-wrapper/' | \
sed 's/main-panel-banner/exclude/' | hxprune
}
lyricsfreak() {
local artist title raw_title url template
local artist_template lyrics_template suburl
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /+}
title=${2// /+}
raw_title=$3
artist_template="http://www.lyricsfreak.com/%s/%s/"
lyrics_template="http://www.lyricsfreak.com/%s"
url=$(printf $artist_template ${artist:0:1} $artist)
suburl=$(mycurl $url | grep -i "$raw_title" | hxwls)
url=$(printf $lyrics_template $suburl)
fi
# Without awk filtering, the div.dn magic always returns something, even
# when no lyrics were found, usually something from lyricsfreak frontpage.
mycurl $url | awk '/<!-- SONG LYRICS -->/, /<!-- \/SONG LYRICS -->/' | \
hxmagic 'div.dn' | sed 's/<a data-tracking..*//g'
}
musixmatch() {
local artist title url template
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /-}
title=${2// /-}
template="https://www.musixmatch.com/lyrics/%s/%s"
url=$(printf $template $artist $title)
fi
# Add line breaks to lyrics section to prevent one big lump of words.
mycurl $url | awk '{print $0"<br></br>"}' | hxmagic 'p.mxm-lyrics__content'
}
darklyrics() {
local artist title raw_title lyrics url
local albums_template lyrics_template album awk_filter
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /}
title=${2// /}
raw_title=$3
albums_template="http://www.darklyrics.com/%s/%s.html"
lyrics_template="http://www.darklyrics.com/lyrics/%s/%s.html"
url=$(printf $albums_template ${artist:0:1} $artist)
album=$(mycurl $url | grep "href" | grep -i "$raw_title" | \
head -n1 | hxwls)
if [[ ! -z "${album// }" ]]; then
album=${$(basename $album):r}
url=$(printf $lyrics_template $artist $album)
fi
fi
awk_filter=$(printf 'BEGIN{IGNORECASE=1}/h3..*%s/{f=1;next}/h3/{f=0}f' \
$raw_title)
mycurl $url | awk $awk_filter
}
songtexte() {
local artist title lyrics url
local search_template lyrics_template suburl
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /-}
title=${2// /-}
search_template="http://www.songtexte.com/search?q=%s+%s&c=all"
lyrics_template="http://www.songtexte.com/%s"
url=$(printf $search_template $artist $title)
suburl=$(curl -s $url | grep -i "$title..*html" | hxwls | head -n1)
url=$(printf $lyrics_template $suburl)
fi
lyrics=$(curl -s $url | sed 's/div id/div class/g' | hxmagic 'div.lyrics')
if [[ $lyrics =~ "Leider kein Songtext" ]]; then
lyrics=""
fi
echo -n $lyrics
}
versuri() {
local artist title raw_artist raw_title lyrics url template
local main_template search_template lyrics_template suburl
if [[ $1 =~ "http" ]]; then
url=$1
else
artist=${1// /+}
title=${2// /+}
raw_artist=$1
raw_title=$3
main_template="http://www.versuri.ro/cat/%s.html"
artist_template="http://www.versuri.ro%s"
lyrics_template="http://www.versuri.ro%s"
url=$(printf $main_template ${artist:0:1})
suburl=$(curl -s $url | grep -i "$raw_artist" | hxwls)
url=$(printf $artist_template $suburl)
suburl=$(curl -s $url | grep -i "$raw_title" | hxwls)
url=$(printf $lyrics_template $suburl)
fi
curl -s $url | \
# Skip all the div and include everything between index0f and BOTTOM-CENTER
awk '/div/{next}/var index0f/{f=1;next}/BOTTOM-CENTER/{print;f=0}f' | \
hxmagic 'p'
}
# Get the best mached urls from the search string.
search_for_urls() {
local search_str=$1
local extra_str=$2
local template="https://www.google.com/search?q=%s+%s"
local search_url=$(printf $template ${search_str// /+} $extra_str)
mycurl $search_url | hxmagic 'h3.r' | hxwls
}
# Return the lyrics from the given url if there is a parser for that domain.
lyrics_from_url() {
local url=$1
local lyrics=""
local domain=$(echo $url | \
awk -F// '{gsub("www.", "", $2); print $2}' | awk -F. '{print $1}')
if typeset -f $domain >/dev/null; then
lyrics=$($domain $url)
fi
clean_string $lyrics
}
# The first line of the returned string is the website name where the
# lyrics were found and the rest of the string are the lyrics themselves.
lyrics_from_artist_song() {
local artist song raw_song last_website lyrics
artist=$1
song=$2
raw_song=$3
last_website=$(get_last_website)
for website in $last_website $LYRICS_WEBSITES; do
lyrics=$($website $artist $song $raw_song) # call function with website name.
if [[ ! -z "${lyrics// /}" ]]; then
echo $website # first line contains the website name
break
fi
done
clean_string $lyrics # the rest of the string contains the lyrics
}
# Return error if cmus is not available or it's not playing at the moment.
cmus_running() {
local str=$(cmus-remote -Q 2>/dev/null)
if [[ -z ${str// /} ]]; then
return 1
fi
local statuss=$(echo $str | awk -F"status " '/status/{print $2}')
if [[ $statuss =~ "stopped" ]]; then
return 1
fi
return 0
}
cmus_artist() {
cmus-remote -Q | awk -F"tag artist " '/tag artist/{print $2}'
}
cmus_song() {
cmus-remote -Q | awk -F"tag title " '/tag title/{print $2}'
}
moc_running() {
local str=$(mocp -i 2>/dev/null)
if [[ -z ${str// /} ]]; then
return 1
fi
local statuss=$(echo $str | awk -F"State: " '/State/{print $2}')
if [[ $statuss =~ "STOP" ]]; then
return 1
fi
return 0
}
moc_artist() {
mocp -i 2>/dev/null | awk -F"Artist: " '/Artist/{print $2}'
}
moc_song() {
mocp -i 2>/dev/null | awk -F"SongTitle: " '/SongTitle/{print $2}'
}
# Retrieve artist/song search string from a media file.
file_tag() {
local file=$1
local tag=$2
local id3tags=$(id3v2 -l $file)
# Make sure the file has id3v2 tags.
if [[ $id3tags =~ "No ID3v2 tag" ]]; then
id3v2 --convert $file > /dev/null
fi
local pattern=$(printf '/%s/{print $2}' $tag)
id3v2 -l $file | awk -F": " $pattern
}
file_artist() {
file_tag $1 "(TPE1|TP1)"
}
file_song() {
file_tag $1 "(TIT2|TT2)"
}
# Database functions.
db_location() {
echo "$HOME/lyrics/"
}
db_artist_location() {
local artist=${1// /-}
echo $(db_location)$artist
}
db_song_location() {
artist=${1// /-}
song=${2// /-}
echo $(db_location)$artist"/"$song
}
save_db() {
local artist=$1
local song=$2
local lyrics=$3
if [[ -z "${artist// /}" ]] || [[ -z "${song// /}" ]] || [[ -z "${lyrics// /}" ]]; then
return # No point in saving gibberish.
fi
local location=$(db_song_location $artist $song)
if [[ ! -a $location ]]; then
mkdir -p $(db_artist_location $artist)
echo $lyrics > $location
fi
}
from_db() {
local artist=$1
local song=$2
local lyrics=""
if [[ -z "${artist// /}" ]] || [[ -z "${song// /}" ]]; then
return
fi
local location=$(db_song_location $artist $song)
if [[ -a $location ]]; then
lyrics=$(<$location)
fi
echo $lyrics
}
# Collect info regarding the most-used lyrics websites.
stats_location() {
echo "$HOME/lyrics/.stats"
}
stats_add_website() {
local website pattern stats_file
website=$1
stats_file=$(stats_location)
# Increment the website count or add it if it doesn't exist.
pattern="/%s/{\$1+=1; found=1}1{print} END{if(found!=1) print \"1 %s\"}"
pattern=$(printf $pattern $website $website)
awk $pattern $stats_file > /tmp/lyrics_stats && \
mv /tmp/lyrics_stats $stats_file
}
stats_display() {
local stats_file=$(stats_location)
cat $stats_file
}
stats_clear() {
local stats_file=$(stats_location)
echo -n "" > $stats_file
}
# Store the last website where lyrics were found. In folder searches,
# the chances are high that the next search if for the same artist or
# even album. So why not search on the same website.
last_website_location() {
echo "$HOME/lyrics/.lastwebsite"
}
save_last_website() {
local last_website_file=$(last_website_location)
echo $1 > $last_website_file
}
get_last_website() {
local last_website_file=$(last_website_location)
cat $last_website_file
}
monitor_folder() {
local folder=$1
inotifywait -m -r -e create -e move --format '%w%f' "${folder}" | \
while read newfile
do
if [[ -d $newfile ]]; then # folder
for file in $newfile/**/*.mp3; do
$SELF $file
done
else # single file
$SELF -s $newfile
fi
done
}
main () {
local extra_str="lyrics"
local only_save="false"
while getopts ":d:e:rw:WsgGlh" opt; do
case $opt in
d)
monitor_folder $OPTARG
;;
e)
extra_str=$OPTARG
;;
r)
clean_tokens=()
;;
w)
: ${(A)LYRICS_WEBSITES::=${=OPTARG}}
;;
W)
echo ${(F)LYRICS_WEBSITES}
exit 0
;;
s)
only_save="true"
;;
g)
stats_display
exit 0
;;
G) stats_clear
exit 0
;;
l)
get_last_website
exit 0
;;
\?)
echo $help_str
exit 1
;;
h)
echo $help_str
exit 0
;;
esac
done
shift $((OPTIND-1))
local search_str urls artist raw_artist song raw_song
local website_and_lyrics website
local lyrics=""
# db is only used when search string is from file or folder,
# otherwise the artist and song strings are not reliable
local reliable_src="true"
# Get the search string, from one of the sources.
if [[ $# -eq 0 ]]; then # from music player
if cmus_running; then
artist=$(cmus_artist)
song=$(cmus_song)
elif moc_running; then
artist=$(moc_artist)
song=$(moc_song)
else
echo "error: No known players available or running."
exit 1
fi
elif [[ $# -eq 1 ]]; then
if [[ -d $1 ]]; then # from folder
for media_file in $1/**/*.mp3; do
$SELF -s -w "$LYRICS_WEBSITES" $media_file
done
exit 0
elif [[ -a $1 ]]; then # from file
artist=$(file_artist $1)
song=$(file_song $1)
else # from cli parameter string
reliable_src="false"
search_str=$1
fi
else # no other sources available
echo $help_str
exit 1
fi
raw_artist=$artist
raw_song=$song
artist=$(clean_search_item $artist)
song=$(clean_search_item $song)
# Actually search the lyrics
if [[ $reliable_src =~ "true" ]]; then
# In the lyrics database..
lyrics=$(from_db $artist $song)
if [[ -z "${lyrics// /}" ]]; then
# ..or on the web, when the artist and song is known
website_and_lyrics=$(lyrics_from_artist_song $artist $song $raw_song)
website=$(echo $website_and_lyrics | head -n1)
lyrics=$(echo $website_and_lyrics | tail -n+2)
# Save the lyrics if any were found and make updates.
if [[ ! -z "${lyrics// /}" ]]; then
save_db $artist $song $lyrics
stats_add_website $website
save_last_website $website
fi
fi
else
# For the free-form strings, use a search engine, as the artist and
# song name are not necessarly known or cannot be 100% infered in
# this case. Also, no saving in this case.
# Use a search engine to get candidate urls.
urls=(${(@f)$(search_for_urls $search_str $extra_str)})
# Extract the lyrics from the first url for which we have a parser.
for url in $urls; do
lyrics=$(lyrics_from_url $url)
if [[ ! -z $lyrics ]]; then
break
fi
done
fi
# Return the lyrics to stdout, if needed.
if [[ $only_save =~ "false" ]]; then
echo "$raw_artist - $raw_song\n"
echo -n $lyrics
fi
# Or report 'lyrics not found for specified media file' error.
if [[ -z "${lyrics// /}" ]]; then
>&2 echo $1
fi
}
main $@