dotfiles

<-- duh.
git clone https://hhvn.uk/dotfiles
git clone git://hhvn.uk/dotfiles
Log | Files | Refs | Submodules | LICENSE

bandcampsrch (2185B)


      1 #!/bin/sh
      2 #
      3 # bandcampsrch.sh
      4 #
      5 # hayden@haydenvh.com
      6 # Copyright (c) 2020 hhvn
      7 #
      8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     11 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     12 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     13 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     14 # SOFTWARE.
     15 #
     16 # This work is free. You can redistribute it and/or modify it under the
     17 # terms of the Do What The Fuck You Want To Public License, Version 2,
     18 # as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
     19 
     20 [ -z $1 ] 2>/dev/null && echo "usage: bandcampsrch <query>" && exit
     21 location=/exec/bandcampsrch.dcgi
     22 
     23 #
     24 # gopher: [7|bandcampsrch|bandcampsrch.dcgi|server|port]
     25 #
     26 # gopher: [1|bandcampsrch|bandcampsrch.dcgi?query|server|port]
     27 #                                          ^^^^^^
     28 
     29 # This has been one of the nicest services to frontedize yet.
     30 # Works very nicely with awk :)
     31 
     32 gopherize(){
     33 	[ "$SERVER_PROTOCOL" = "gopher/1.0" ] && {
     34 		echo "[7|Search again|$location|server|port]"
     35 		sed -E 's/ (http.*$)/[h|\1|URL:\1|server|port]/'
     36 	} || cat
     37 }
     38 		
     39 curl -H "Accept-Language: en-US" -qsL "https://bandcamp.com/search?q=$(echo "$@" | xxd -p | tr -d '\n' | sed 's/../%&/g')" | \
     40 sed 's/^[[:space:]]*//g' | awk -v "c0=$(tput sgr0)" -v "c1=$(tput setaf 4)" -v "c2=$(tput setaf 6)" -v "c3=$(tput setaf 8)" '
     41 	## Start of a "block"
     42 	/^ARTIST/ {
     43 		artist=1
     44 		geturl=1
     45 		printf("\n%s[ARTIST]%s\n", c1, c0) 
     46 		next
     47 	}
     48 	/^ALBUM/ {
     49 		album=1
     50 		geturl=1
     51 		printf("\n%s[ALBUM]%s\n", c2, c0)
     52 		next
     53 	}
     54 	/^TRACK/ {
     55 		track=1
     56 		geturl=1
     57 		printf("\n%s[TRACK]%s\n", c3, c0)
     58 		next
     59 	}
     60 
     61 	## End of a "block"
     62 	/^<div class="releases">/ {
     63 		artist=0
     64 	}
     65 	/^<div class="itemurl">/ {
     66 		album=0
     67 		track=0
     68 	}
     69 
     70 	## Generic printing
     71 	/^<a href="/ {
     72 		if (geturl == 1)
     73 			print " " $0
     74 		geturl=0
     75 	}
     76 	/^[A-Za-z0-9]/ {
     77 		if (artist == 1 || album == 1 || track == 1)
     78 			print " " $0
     79 	}
     80 ' | sed -E 's/<a href="([^?]*).*/\1/' | gopherize | less -R