pexelgrab (2595B)
1 #!/bin/sh 2 # 3 # pexelgrab 4 # Created by Hayden Hamilton 5 # 6 # hayden@haydenvh.com 7 # Copyright (c) 2020 Hayden Hamilton 8 9 [ -f $HOME/general/security/pexels ] && read API_KEY < $HOME/general/security/pexels || API_KEY="$1" 10 [ "$API_KEY" = "" ] && echo "No api key..." && exit 1 11 download=$2 12 [ "$download" = "" ] && echo "No download folder specified... using $HOME/general/downloads" && download=$HOME/general/downloads 13 14 get(){ 15 curl -H "Authorization: $API_KEY" "https://api.pexels.com/v1/$*" 16 } 17 18 apigrep(){ 19 grep '^"'"$1" < /tmp/pexels | sed 's/^"'"$1"'":"//g;s/"$//g' | nl 20 } 21 22 grepout(){ 23 grep "^[[:space:]]*$1[[:space:]]" < /dev/stdin | tr '[[:space:]]' ' ' | awk '{print $2}' 24 } 25 26 [ -f /tmp/pexels ] && old=$(printf "yes\nno" | dmenu -i -p "Use old request?") 27 [ "$old" != "yes" ] && { 28 query=$(printf '' | dmenu -i -p "Search query (required):") 29 [ "$query" = "" ] && echo "Query required" && exit 1 || request="search?query=$(echo "$query" | tr ' ' '+')" 30 perpage=$(echo "DEFAULT" | dmenu -i -p "Results per page:") 31 [ "$perpage" != "DEFAULT" ] && request="${request}&per_page=$perpage" || perpage="15" 32 page=$(echo "DEFAULT" | dmenu -i -p "Page number:") 33 [ "$page" != "DEFAULT" ] && request="${request}&page=$page" 34 echo '"search_query":"'"$query"'"' > /tmp/pexels 35 get "$request" | tr "{},[]" "\n" >> /tmp/pexels 36 } || perpage="15" 37 38 url=$(apigrep "url") 39 width=$(apigrep "width") 40 height=$(apigrep "height") 41 original=$(apigrep "original") 42 basename=$(echo "$original" | sed 's~^.*/~~g' | nl) 43 small=$(apigrep "small") 44 id=$(apigrep "id") 45 46 47 for i in $(seq $perpage) 48 do 49 while true 50 do 51 action=$(printf "view\nview full\ndownload\ndownload landscape\nnext\nall done" | dmenu -i -p "$(echo "$url" | grepout "$i") $(echo "$width" | grepout "$i" | tr -d '"') $(echo "$height" | grepout "$i" | tr -d '"')" | tr ' ' '-') 52 case $action in 53 view) addr=$(echo "$small" | grepout "$i") ; curl "$addr" --output /tmp/pexels.png ; sxiv /tmp/pexels.png ;; 54 view-full) addr=$(echo "$original" | grepout "$i") ; curl "$addr" --output /tmp/pexels.png ; sxiv /tmp/pexels.png ;; 55 download) mkdir -p $download/pexelgrab ; ts curl "$(echo "$original" | grepout "$i")" --output $download/pexelgrab/$(echo "$basename" | grepout "$i" | tr -d '":\\') ; echo 'sent to `ts` (task spooler)' ; break ;; 56 download-landscape) mkdir -p $download/pexelgrab ; ts curl "$(echo "$original" | grepout "$i")?fit=crop&h=1080&w=1920" --output $download/pexelgrab/$(echo "$basename" | grepout "$i" | tr -d '":\\') ; echo 'sent to `ts` (task spooler)' ; break ;; 57 next) break ;; 58 all-done) exit 0 ;; 59 *) break ;; 60 esac 61 done 62 done