rls-discogs/search-deezer-api.fish

107 lines
2.8 KiB
Fish
Executable File

#!/usr/bin/env fish
#set fish_trace on
function usage
begin
echo "Read TSV release data from sdtin > query Deezer 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.deezer.API.XXXXXXXX.json)
set DAPI "https://api.deezer.com/search/album"
set UA "search-deezer.fish/v0.0.3"
while read -l -d (printf '\t') ORG A T C B F E Y
if test -z "$A"; or test -z "$T"
echo "WARN: Skipping empty artist / title input"
continue
end
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"
sleep .2
set API_CODE (curl -s -G "$DAPI" --user-agent "$UA" -w '%{http_code}' -o "$API_OUT" --data-urlencode "q=$A $T $E" --data-urlencode "strict=on")
if test $status -ne 0
echo "curl API request failed" >&2
rm "$API_OUT"
continue
end
if test $API_CODE -ne 200
begin
echo "Deezer API status code $CODE"
echo $ORG
end >&2
rm "$API_OUT"
continue
end
set API_COUNT (jq -r .total "$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" = "null"
begin
echo "ERR: API result has no total count"
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" \
--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,
"_source": "api.deezer.com",
"_search_result_count": $api_count | tonumber,
"_artist": $a,
"_title": $t,
"_catno": $c,
"_barcode": $b,
"_format": $f,
"_edition": $e,
"_year": $y
}'
rm "$API_OUT"
continue
else if test $API_COUNT -ge 1
jq -SMc --arg rls "$ORG" --arg api_count "$API_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,
"_source": "api.deezer.com",
"_search_result_count": $api_count | tonumber,
"_artist": $a,
"_title": $t,
"_catno": $c,
"_barcode": $b,
"_format": $f,
"_edition": $e,
"_year": $y
}' "$API_OUT"
end
rm "$API_OUT"
end