summaryrefslogtreecommitdiff
path: root/blq
blob: c740ec25b1bde90c0f0c3b55169992651234ff60 (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
#!/bin/sh

# Perform a search on Blocket.se (Nov 2020)

usage() { echo usage: $0 query 1>&2; exit 1; }

if which mutt 1>&-
then : ${MAILER:='mutt -e "set folder=."'}
else : ${MAILER:=mailx}
fi

# Parse options
r=
d=
while getopts dr o
do
	case $o in
	d) d=-d ;; # send -d (dumb) option to blm
	r) r=-r ;; # process with blm and read in MAILER
	?) usage ;;
	esac
done
shift $((OPTIND-1))
test -z "$1" && usage

# Format query string
q=`printf '%s\n' "$1" | sed 's/ /%20/g'`

# Retrieve authorization token
tok=`curl -s "https://www.blocket.se/annonser/hela_sverige?q=$q" | awk '/id="initialState"/ {getline; print}' | jq -r .authentication.bearerToken`

# Retrieve search results
curl -s -H "authorization: Bearer $tok" "https://api.blocket.se/search_bff/v1/content?lim=40&q=$q&st=s&include=all&gl=3&include=extend_with_shipping" |
{
	test ! -z "$r" && {
		f=/tmp/blq.$RANDOM
		blm $d > $f
		eval $MAILER -f $f < /dev/tty
		rm $f
	} || cat
}