summaryrefslogtreecommitdiff
path: root/lf/lfu
blob: 415f4488401ecee357706dbecae7b9a140213a46 (plain)
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
#!/bin/sh

# Search for track(s) on Last.fm, retrieve URL(s)

usage() { echo usage: $0 [-a] [-f] [-m max] base-url 1>&2; exit 1; }

# Parse options
a=
f=
m=10
while getopts afm: o
do
	case $o in
	a) a=1 ;; # all
	f) f=1 ;; # first
	m) m=$OPTARG ;; # max
	?) usage
	esac
done
shift $((OPTIND-1))
q=$1

m=$((m+0))
test $m -lt 1 && m=10

# Retrieve search results
tmp=/tmp/lfu.$RANDOM
curl -s -G --data-urlencode "q=$q" 'https://www.last.fm/search/tracks' |
sed '/^[ 	]*$/d' |
awk -F \" '
	/class="chartlist-name"/ {
		getline;
		getline;
		getline;
		getline;
		getline;
		getline;
		print $2;
		i++;
		if (i > '"$((m-1))"') exit;
	}
' |
sed 's,^,https://www.last.fm,' > $tmp

# Select/print results
if test ! -z "$a"
then
	cat $tmp
elif test ! -z "$f"
then
	sed 1q < $tmp
else
	< $tmp lfp | nl -w2 -s'. ' > /dev/tty
	read n < /dev/tty
	n=$((n+0))
	test $n -lt 1 && n=1
	awk "NR == $n {print}" < $tmp
fi
rm $tmp