#!/usr/bin/env -S fish #set fish_trace on function usage begin echo "Reads tsv release from sdtin, outputs JSON" echo echo "Usage: $0 [-v][-eURL]" echo echo " -eURL/--endpoint=URL Set ElasticSearch /_search endpoint URL" echo " -v/--verbose Print query and warnings" exit 1 end >&2 end argparse -n (status current-filename) 'v/verbose' 'h/help' 'e/endpoint=?' -- $argv if test $status -ne 0 usage end if set -q _flag_help usage end if not set -q _flag_endpoint set _flag_endpoint 'http://localhost:9200/_all/_search' end curl -s -o /dev/null -G "$_flag_endpoint" if test $status -ne 0 echo "ERR: Search API Endpoint $_flag_endpoint failed" exit 1 end set OUT (mktemp -t rls.ES.XXXXXXXX.json) function discord_format switch $argv[1] case (seq 100)VINYL VINYL VLS Vinyl vinyl printf '%s' "Vinyl" case (seq 100)CD CD CDA CDS CDM CDEP EP printf '%s' "CD" case CDR CDREP printf '%s' "CDr" case TAPE printf '%s' "Cassette" case WEB SINGLE-WEB EP-WEB printf '%s' "File" case '*' printf '%s' "*" end end while read -l -d (printf '\t') ORG A T C B F E Y set _in_A "$A"; set _in_T "$T"; set _in_C "$C"; set _in_B "$B"; set _in_F "$F"; set _in_E "$E"; set _in_Y "$Y" if set -q A; and test -n "$A" set A (string trim "$A") switch "$A" case VA Va va "V.A." OST "O.S.T" "Various Artists" set Q "q=artists.name:\"Various\"" case '*' set A (string replace -a ')' ' ' "$A") set A (string replace -a '(' ' ' "$A") set A (string lower "$A") set Q "q=artists.name:\"$A\"" end end if set -q T; and test -n "$T" set T (string replace -a '(' ' ' "$T") set T (string replace -a ')' ' ' "$T") set T (string trim "$T") set T (string lower "$T") set -a Q " AND title:\"$T\"~" end if set -q C; and test -n "$C" set -a Q " AND (identifiers.value:$C~ OR labels.catno:$C)" end if set -q B; and test -n "$B" set B (string trim "$B") set -a Q " AND (identifiers.value:$B~ OR labels.catno:$B)" end if set -q F; and test -n "$F" switch "$F" case CDS set -a Q " AND formats.name:CD AND formats.descriptions:Single" case CDM set -a Q " AND formats.name:CD AND formats.descriptions:Maxi-Single" case CDA set -a Q " AND formats.name:CD AND formats.descriptions:Album" case '*' set -a Q " AND formats.name:$(discord_format $F)" end else # No format given, at least exclude Vinyl which would usually be named in rls set -a Q " AND NOT formats.name:Vinyl" end if set -q Y set -a Q " AND released:[$Y-01-01 TO $Y-12-31]" end # TODO Editions if set -q Q; and test -n "$Q" if set -q _flag_verbose echo "$Q" >&2 end set CODE (curl -s -G -w '%{http_code}' -o "$OUT" --data-urlencode "$Q" --data-urlencode size=100 "$_flag_endpoint") if test $status -ne 0 echo "ERR: curl request failed" >&2 rm "$OUT" continue end if test $CODE -ne 200 begin echo "ERR: ES status code $CODE" echo $ORG echo $Q end >&2 rm "$OUT" continue end set ES_COUNT (jq -r .hits.total.value "$OUT") if test $status -ne 0 begin echo "ERR: Failed parsing result" echo $ORG echo $Q end >&2 rm "$OUT" continue end if test $ES_COUNT -eq 0 echo '{}' | jq -SMc --arg rls "$ORG" --arg query "$Q" --arg cnt "$ES_COUNT" \ --arg a "$_in_A" \ --arg t "$_in_T" \ --arg c "$_in_C" \ --arg b "$_in_B" \ --arg f "$_in_F" \ --arg e "$_in_E" \ --arg y "$_in_Y" \ ' { "_scene_release_name": $rls, "_query": $query, "_search_result_count": $cnt | tonumber, "_artist": $a, "_title": $t, "_catno": $c, "_barcode": $b, "_format": $f, "_edition": $e, "_year": $y }' else if test $ES_COUNT -eq 1 jq -SMc --arg rls "$ORG" --arg query "$Q" --arg count "$ES_COUNT" '.hits.hits[0] | ._source + { "_scene_release_name": $rls, "_query": $query, "_search_result_count": $count | tonumber, }' "$OUT" else if test $ES_COUNT -gt 1 # skip results with mixed mastere ids set UNIQUE_MASTERS (jq -r '[.hits.hits[]._source.master_id] | unique | length' $OUT) if test $UNIQUE_MASTERS -gt 1 if set -q _flag_verbose echo "WARN: Mixed results $ORG" >&2 end rm "$OUT" continue else jq -SMc --arg rls "$ORG" --arg query "$Q" --arg count "$ES_COUNT" '.hits.hits[0] | ._source + { "_scene_release_name": $rls, "_query": $query, "_search_result_count": $count | tonumber, }' "$OUT" end end rm "$OUT" end end