105 lines
2.6 KiB
Fish
105 lines
2.6 KiB
Fish
|
#!/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
|