Browse Source

v0.0.3

tags/v0.0.3
bux 2 years ago
parent
commit
a7204e17a2
8 changed files with 562 additions and 1 deletions
  1. +65
    -1
      README.md
  2. +3
    -0
      in-ws-predb.ovh.sh
  3. +4
    -0
      out-fallback.jq
  4. +8
    -0
      out-ids.jq
  5. +2
    -0
      out-simple.jq
  6. +191
    -0
      parse-rls.fish
  7. +104
    -0
      search-discogs-api.fish
  8. +185
    -0
      search-discogs-elastic.fish

+ 65
- 1
README.md View File

@@ -1,3 +1,67 @@
# rls-discogs

scene release to discogs release
scene release to discogs release

# using

- [fish](https://fishshell.com/)
- [discogs-xml-to-elasticsearch](https://github.com/SuperToma/discogs-xml-to-elasticsearch)
- [curl](https://curl.se/)
- [jq](https://stedolan.github.io/jq/)
- [websocat](https://github.com/vi/websocat)

# flow

scene rls name > parse > artist / title / year as .tsv > search discogs > .json

# tools

- `in-ws-predb.ovh.sh` read scene release names from predb websocket
- `parse-rls.fish` parse scene release name
- `search-discogs-api.fish` search discogs release using Discogs REST API
- `search-discogs-elastic.fish` search discogs release using ElasticSearch
- `out-fallback.jq` fallback search helper
- `out-ids.jq` filter ids from json output
- `out-simple.jq` filter genre from json output

# usage

parse rls name as tab separated values
```
$ echo 'Kraftwerk-Autobahn-Remastered-CD-FLAC-2009-BCC' | ./parse-rls.fish | column -t -s "$(printf '\t')"
Kraftwerk-Autobahn-Remastered-CD-FLAC-2009-BCC Kraftwerk Autobahn CD Remastered 2009
```

parse rls name as json
```
$ echo 'Kraftwerk-Autobahn-Remastered-CD-FLAC-2009-BCC' | ./parse-rls.fish -j | jq .
{
"artist": "Kraftwerk",
"barcode": null,
"catno": null,
"edition": "Remastered",
"format": "CD",
"rls": "Kraftwerk-Autobahn-Remastered-CD-FLAC-2009-BCC",
"title": "Autobahn",
"year": "2009"
}
```

parse rls name, search local ElasticSearch, output discogs ids
```
$ echo 'Kraftwerk-Autobahn-Remastered-CD-FLAC-2009-BCC' | ./parse-rls.fish | ./search-discogs-elastic.fish | ./out-ids.jq
{"artist_id":4654,"master_id":2994,"release_id":1966322,"rls":"Kraftwerk-Autobahn-Remastered-CD-FLAC-2009-BCC"}
```

get rls names from websocket, parse rls name, search with api.discogs.com, write .json
```
$ ./in-ws-predb.ovh.sh | ./parse-rls.fish | search-discogs-api.fish > out.api.json
```

parse rls name, search local ElasticSearch, write json, filter empty results and convert json to tsv, fallback search with Discogs API, write json
```
$ echo 'Band_Of_Horses-Things_Are_Great-CD-2022-FATHEAD' | ./parse-rls.fish | ./search-discogs-elastic.fish | tee out.es.json | ./out-fallback.jq | ./search-discogs-api.fish > out.api.json
$ jq ._search_result_count out.es.json out.api.json
0
1
```

+ 3
- 0
in-ws-predb.ovh.sh View File

@@ -0,0 +1,3 @@
#!/bin/bash
set -ueo pipefail
websocat wss://predb.ovh/api/v1/ws | jq -r 'select(.action=="insert" and (.row.cat=="MP3" or .row.cat=="FLAC")).row.name'

+ 4
- 0
out-fallback.jq View File

@@ -0,0 +1,4 @@
#!/usr/bin/env -S jq -cfMr
# Convert input JSON into TSV for fallback API search
# cat predb | ./parse-rls.fish | ./search-discogs-elastic.fish | tee es.result.json | ./out-fallback.jq | ./search-discogs-api.fish > api.result.json
select(._search_result_count == 0) | [ ._scene_release_name, ._artist, ._title, ._catno, ._barcode, ._format, ._edition, ._year] | @tsv

+ 8
- 0
out-ids.jq View File

@@ -0,0 +1,8 @@
#!/usr/bin/env -S jq -cfS
select(._search_result_count > 0) |
{
"rls": ._scene_release_name,
"release_id": .id | tonumber,
"master_id": (if (.master_id | length) > 0 then .master_id | tonumber else null end),
"artist_id": .artists[0].id | tonumber
}

+ 2
- 0
out-simple.jq View File

@@ -0,0 +1,2 @@
#!/usr/bin/env -S jq -cfMr
select(._search_result_count > 0) | [ ._scene_release_name, (.genre? + .genres?), (.style? + .styles?), .country, .released ] | flatten | join(" : ")

+ 191
- 0
parse-rls.fish View File

@@ -0,0 +1,191 @@
#!/usr/bin/env -S fish
#set fish_trace on

function usage
begin
echo "Split scene release name into artist, title, barcode, catno, format, edition, year"
echo
echo "Usage: $0 [-j][-t][-v]"
echo
echo " -t / --tsv Output tab separated (default)"
echo " -j / --json Output as JSON"
echo " -v / --verbose Output unmatched to stderr"
echo " -s / --stats Output stats to stderr"
exit 1
end >&2
end

argparse -n (status current-filename) -x 'j,t' 'h/help' 'v/verbose' 'j/json' 't/tsv' 's/stats' -- $argv
if test $status -ne 0
usage
end

if set -q _flag_help
usage
end


# supported patterns
# artist-title-(catno)-edition-format-year
# artist-title-(catno)-lang-format-year
# artist-title-(catno)-format-year
# artist-title(catno)-format-year
# artist-title(catno)-year
# artist-title-(barcode)-lang-format-year
# artist-title-(barcode)-edition-format-year
# artist-title-(barcode)-format-year
# artist-title-format-lang-year
# artist-title-edition-format-lang-year
# artist-title-edition-format-year
# artist-title-edition-lang-year
# artist-title-edition-year
# artist-title-format-edition-year
# artist-title-format-year
# artist-title-lang-format-year
# artist-title-lang-year
# artist-title-year

set CNT_ATCEFY 0; set CNT_ATCLFY 0; set CNT_ATCFY 0; set CNT_ATCY 0
set CNT_ATBLFY 0; set CNT_ATBEFY 0; set CNT_ATBFY 0
set CNT_ATFLY 0; set CNT_ATEFLY 0; set CNT_ATEFY 0; set CNT_ATELY 0; set CNT_ATFEY 0
set CNT_ATEY 0; set CNT_ATLFY 0; set CNT_ATFY 0; set CNT_ATLY 0; set CNT_ATY 0

set TOTAL 0; set IGNORED 0; set UNMATCHED 0; set ONEMATCH 0; set MMATCH 0

set EDITION '\(?(?<E>REMASTERED|BOOTLEG|DEMO|PROMO|REISSUE|EXPANDED_EDITION|DELUXE_EDITION|REMASTERED_DELUXE_EDITION|LIMITED_EDITION|BONUS_EDITION|SPECIAL_EDITION|REMASTERED_EXPANDED_BOXSET|REMASTERED_BOXSET|Remastered_Edition|Limited_Edition_Vinyl|PROMO_VINYL|Platinum_Edition)\)?'
set FORMAT '\(?(?<F>[0-9]{1,2}CD|[0-9]{1,2}LP|LP|[0-9]{1,2}VINYL|CD|CDA|CDS|CDM|CDEP|CDR|CDREP|EP|VINYL|VLS|TAPE|WEB|SINGLE-WEB|EP-WEB|BOXSET|MAG)\)?'

set ISO3166 'AW|AF|AO|AI|AX|AL|AD|AE|AR|AM|AS|AQ|TF|AG|AU|AT|AZ|BI|BE|BJ|BQ|BF|BD|BG|BH|BS|BA|BL|BY|BZ|BM|BO|BR|BB|BN|BT|BV|BW|CF|CA|CC|CH|CL|CN|CI|CM|CD|CG|CK|CO|KM|CV|CR|CU|CW|CX|KY|CY|CZ|DE|DJ|DM|DK|DO|DZ|EC|EG|ER|EH|ES|EE|ET|FI|FJ|FK|FR|FO|FM|GA|GB|GE|GG|GH|GI|GN|GP|GM|GW|GQ|GR|GD|GL|GT|GF|GU|GY|HK|HM|HN|HR|HT|HU|ID|IM|IN|IO|IE|IR|IQ|IS|IL|IT|JM|JE|JO|JP|KZ|KE|KG|KH|KI|KN|KR|KW|LA|LB|LR|LY|LC|LI|LK|LS|LT|LU|LV|MO|MF|MA|MC|MD|MG|MV|MX|MH|MK|ML|MT|MM|ME|MN|MP|MZ|MR|MS|MQ|MU|MW|MY|YT|NA|NC|NE|NF|NG|NI|NU|NL|NO|NP|NR|NZ|OM|PK|PA|PN|PE|PH|PW|PG|PL|PR|KP|PT|PY|PS|PF|QA|RE|RO|RU|RW|SA|SD|SN|SG|GS|SH|SJ|SB|SL|SV|SM|SO|PM|RS|SS|ST|SR|SK|SI|SE|SZ|SX|SC|SY|TC|TD|TG|TH|TJ|TK|TM|TL|TO|TT|TN|TR|TV|TW|TZ|UG|UA|UM|UY|US|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|YE|ZA|ZM|ZW'

set ISO639 'AA|AB|AE|AF|AK|AM|AN|AR|AS|AV|AY|AZ|BA|BE|BG|BH|BI|BM|BN|BO|BR|BS|CA|CE|CH|CO|CR|CS|CU|CV|CY|DA|DE|DV|DZ|EE|EL|EN|EO|ES|ET|EU|FA|FF|FI|FJ|FO|FR|FY|GA|GD|GL|GN|GU|GV|HA|HE|HI|HO|HR|HT|HU|HY|HZ|IA|ID|IE|IG|II|IK|IO|IS|IT|IU|JA|JV|KA|KG|KI|KJ|KK|KL|KM|KN|KO|KR|KS|KU|KV|KW|KY|LA|LB|LG|LI|LN|LO|LT|LU|LV|MG|MH|MI|MK|ML|MN|MR|MS|MT|MY|NA|NB|ND|NE|NG|NL|NN|NO|NR|NV|NY|OC|OJ|OM|OR|OS|PA|PI|PL|PS|PT|QU|RM|RN|RO|RU|RW|SA|SC|SD|SE|SG|SI|SK|SL|SM|SN|SO|SQ|SR|SS|ST|SU|SV|SW|TA|TE|TG|TH|TI|TK|TL|TN|TO|TR|TS|TT|TW|TY|UG|UK|UR|UZ|VE|VI|VO|WA|WO|XH|YI|YO|ZA|ZH|ZU'

set LANG "(?<L>$ISO3166|$ISO639)"

set TEAM '[a-zA-Z0-9_]+'
set YEAR '\(?(?<Y>(19|20)[0-9]{2})\)?'
set SEP '(-|_-_|--)'
set CATNO '\((?<C>[a-zA-Z]+[0-9]+|[a-zA-Z]+[_-][0-9]+|[a-zA-Z]+[0-9]+[a-zA-Z]+|[a-zA-Z]+[_-][0-9]+[a-zA-Z]+|[a-zA-Z]+[_-][0-9]+[_-][0-9]+|[a-zA-Z]+[0-9]+[_-][a-zA-Z]+)\)'
set BARCODE '\(?(?<B>[0-9_-]{5,15})\)?'
set ARTIST '(?<A>[a-zA-Z0-9._]+)'
set TITLE '(?<T>[a-zA-Z0-9_.()]+)'

while read -l REL
set TOTAL (math $TOTAL+1)

if string match -iq -r -- "(LINE|DVBS|DVBC|FM|DAB|SBD|CABLE|SAT)-[0-9]{2}-[0-9]{2}-[0-9]{4}-$TEAM\$" "$REL"
set IGNORED (math $IGNORED+1)
continue
end
# remove stuff unusuable in search
set ORG $REL
for N in 'FLAC' 'WEBFLAC' 'DIRFIX' 'PROOFFIX' 'NFOFIX' 'REPACK' 'PROPER' 'RETAIL' 'READ_NFO' 'OST' 'RERIP'
set REL (string replace -ia -- "-$N-" '-' $REL)
set REL (string replace -ia -- "_$N-" '-' $REL)
end

# reset named regex groups
set -e A; set -e T; set -e B; set -e C; set -e L; set -e F; set -e E; set -e Y

if string match -iq -r -- "^$ARTIST$SEP$TITLE-$CATNO-$EDITION-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATCEFY (math $CNT_ATCEFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$CATNO-$LANG-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATCLFY (math $CNT_ATCLFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$CATNO-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATCFY (math $CNT_ATCFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$CATNO-$YEAR-$TEAM\$" "$REL"
set CNT_ATCY (math $CNT_ATCY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE$CATNO-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATCFY (math $CNT_ATCFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE$CATNO-$YEAR-$TEAM\$" "$REL"
set CNT_ATCY (math $CNT_ATCY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$BARCODE-$LANG-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATBLFY (math $CNT_ATBLFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$BARCODE-$EDITION-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATBEFY (math $CNT_ATBEFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$BARCODE-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATBFY (math $CNT_ATBFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$FORMAT-$LANG-$YEAR-$TEAM\$" "$REL"
set CNT_ATFLY (math $CNT_ATFLY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$EDITION-$FORMAT-$LANG-$YEAR-$TEAM\$" "$REL"
set CNT_ATEFLY (math $CNT_ATEFLY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$EDITION-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATEFY (math $CNT_ATEFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$EDITION-$LANG-$YEAR-$TEAM\$" "$REL"
set CNT_ATELY (math $CNT_ATELY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$LANG-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATLFY (math $CNT_ATLFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$FORMAT-$EDITION-$YEAR-$TEAM\$" "$REL"
set CNT_ATFEY (math $CNT_ATFEY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$FORMAT-$YEAR-$TEAM\$" "$REL"
set CNT_ATFY (math $CNT_ATFY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$EDITION-$YEAR-$TEAM\$" "$REL"
set CNT_ATEY (math $CNT_ATEY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$LANG-$YEAR-$TEAM\$" "$REL"
set CNT_ATLY (math $CNT_ATLY+1)
else if string match -iq -r -- "^$ARTIST$SEP$TITLE-$YEAR-$TEAM\$" "$REL"
set CNT_ATY (math $CNT_ATY+1)
else
set UNMATCHED (math $UNMATCHED+1)
if set -q _flag_verbose
echo "$ORG" >&2
end
continue
end

if set -q A; and test -n "$A"
set A (string replace -a '_' ' ' "$A")
set A (string trim "$A")
end
if set -q T; and test -n "$T"
set T (string replace -a '_' ' ' "$T")
set T (string trim "$T")
end
if set -q B; and test -n "$B"
set B (string replace -a '_' ' ' "$B")
set B (string trim "$B")
end
if set -q E; and test -n "$E"
set E (string replace -a '_' ' ' "$E")
set E (string trim "$E")
end

if set -q _flag_json
echo '{}' | jq -cSM --arg rls "$ORG" --arg a "$A" --arg t "$T" \
--arg c "$C" --arg b "$B" --arg f "$F" --arg e "$E" --arg y "$Y" '{
"rls": $rls,
"artist": $a,
"title": $t,
"catno": (if ($c | length == 0) then null else $c end),
"barcode": (if ($b | length == 0) then null else $b end),
"format": (if ($f | length == 0) then null else $f end),
"edition": (if ($e | length == 0) then null else $e end),
"year": (if ($y | length == 0) then null else $y end)
}'
else
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "$ORG" "$A" "$T" "$C" "$B" "$F" "$E" "$Y"
end
end

# to stderr
if set -q _flag_stats
begin
echo "ATCEFY $CNT_ATCEFY"
echo "ATCLFY $CNT_ATCLFY"
echo "ATCFY $CNT_ATCFY"
echo "ATBLFY $CNT_ATBLFY"
echo "ATBEFY $CNT_ATBEFY"
echo "ATBFY $CNT_ATBFY"
echo "ATFLY $CNT_ATFLY"
echo "ATEFLY $CNT_ATEFLY"
echo "ATEFY $CNT_ATEFY"
echo "ATELY $CNT_ATELY"
echo "ATLFY $CNT_ATLFY"
echo "ATFY $CNT_ATFY"
echo "ATEY $CNT_ATEY"
echo "ATLY $CNT_ATLY"
echo "ATY $CNT_ATY"
echo "Unmatched $UNMATCHED"
echo "Ignored $IGNORED"
echo "Total $TOTAL"
end >&2
end

+ 104
- 0
search-discogs-api.fish View File

@@ -0,0 +1,104 @@
#!/usr/bin/env fish

function usage
begin
echo "Read TSV release data from sdtin > query Discogs API > output JSON"
exit 1
end
end

argparse -n (status current-filename) 'h/help' -- $argv
if test $status -ne 0
usage
end

if set -q _flag_help
usage
end

set API_OUT (mktemp -t rls.API.XXXXXXXX.json)
set DAPI "https://api.discogs.com/database/search"
set UA "rls-discogs.fish/v0.0.3"

if test -f ~/.discogs.token
set TOKEN (cat ~/.discogs.token)
end

if test -z "$TOKEN"
echo "Please place access token from https://www.discogs.com/settings/developers into ~/.discogs.token"
exit 1
end

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
sleep 1
set API_CODE (curl -s -G "$DAPI" --user-agent "$UA" -w '%{http_code}' -o "$API_OUT" -H "Authorization: Discogs token=$TOKEN" \
--data-urlencode "type=release" \
--data-urlencode "artist=$A" \
--data-urlencode "release_title=$T" \
--data-urlencode "catno=$C" \
--data-urlencode "barcode=$B" \
--data-urlencode "format=$(discord_format $F)" \
--data-urlencode "year=$Y")

if test $status -ne 0
echo "curl API request failed" >&2
rm "$API_OUT"
continue
end

if test $API_CODE -ne 200
begin
echo "Discogs API status code $CODE"
echo $ORG
end >&2
rm "$API_OUT"
continue
end

set API_COUNT (jq -r .pagination.items "$API_OUT")
if test $status -ne 0
begin
echo "Failed parsing API result"
echo $ORG
end >&2
rm "$API_OUT"
continue
end

if test $API_COUNT -eq 0
echo '{}' | jq -SMc --arg rls "$ORG" --arg api_count "$API_COUNT" '
{
"_scene_release_name": $rls,
"_query": "API",
"_search_result_count": $api_count | tonumber,
}'
rm "$API_OUT"
continue
else if test $API_COUNT -ge 1
jq -SMc --arg rls "$ORG" --arg api_count "$API_COUNT" '.results[0] |
. + {
"_scene_release_name": $rls,
"_query": "API",
"_search_result_count": $api_count | tonumber
}' "$API_OUT"
end
rm "$API_OUT"
end

+ 185
- 0
search-discogs-elastic.fish View File

@@ -0,0 +1,185 @@
#!/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

Loading…
Cancel
Save