summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2020-11-22 01:30:55 +0100
committerJohn Ankarström <john@ankarstrom.se>2020-11-22 01:31:32 +0100
commit850f917b6aaea6f25bab8860ede290eb66fe958b (patch)
tree469403bd96d4ea98bc0e0288c7f763457aed51e9
parent8b4e99e55b54b61a9b0c7063e9ee8482f99537b0 (diff)
downloadweb-850f917b6aaea6f25bab8860ede290eb66fe958b.tar.gz
blq: Add -r option
The -r option processes the search results with blm and opens the mbox file in a given mail reader.
-rwxr-xr-xblm5
-rwxr-xr-xblq29
-rwxr-xr-xxii5
3 files changed, 31 insertions, 8 deletions
diff --git a/blm b/blm
index 5b70f0d..f00e254 100755
--- a/blm
+++ b/blm
@@ -1,11 +1,10 @@
#!/bin/sh
-# Format Blocket.se search results
+# Format Blocket.se search results as mboxu
-# Convert json to mbox
jq -r '.data[] |
"From BLOCKET " + (.list_time | sub("\\+[0-9][0-9]:00$"; "Z") | fromdate | strftime("%a %b %e %T %Y")) + "\n" +
- "From: " + .advertiser.name + ", " + .location[0].name + " (" + .advertiser.type + ")\n" +
+ "From: \"" + .advertiser.name + ", " + .location[0].name + " (" + .advertiser.type + ")\" <noreply@localhost>\n" +
"Subject: " + "(" + (.price.value | tostring) + ") " + .subject + "\n" +
([.images[].url] | map("X-Image: " + . + "?type=original") | join("\n")) + "\n" +
"\n" + .body + "\n" +
diff --git a/blq b/blq
index 0941da8..085136b 100755
--- a/blq
+++ b/blq
@@ -2,7 +2,24 @@
# Perform a search on Blocket.se (API v6, Nov 2020)
-test -z "$1" && { echo usage: $0 query 1>&2; exit 1; }
+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=
+while getopts rc o
+do
+ case $o in
+ r) r=1 ;; # 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'`
@@ -11,4 +28,12 @@ q=`printf '%s\n' "$1" | sed 's/ /%20/g'`
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"
+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 > $f
+ eval $MAILER -f $f < /dev/tty
+ rm $f
+ } || cat
+}
diff --git a/xii b/xii
index dc4a620..c79e967 100755
--- a/xii
+++ b/xii
@@ -8,13 +8,12 @@
dir=/tmp/$RANDOM
mkdir $dir
-grep '^X-Image: ' |
-sed 's/^X-Image: //' |
+sed -n 's/^X-Image: //p' |
while read url
do
curl "$url" > $dir/$RANDOM
done
# Display images and delete them afterwards
-$VIEWER $dir/* < /dev/tty
+eval $VIEWER $dir/* < /dev/tty
rm -r $dir