add deezer search
This commit is contained in:
parent
d9e026a4ff
commit
631f6e3fba
111
search-deezer-api.fish
Executable file
111
search-deezer-api.fish
Executable file
@ -0,0 +1,111 @@
|
||||
#!/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"
|
||||
|
||||
function deezer_record_type
|
||||
switch $argv[1]
|
||||
case (seq 100)CD CD CDA
|
||||
printf '%s' "album"
|
||||
case CDEP EP
|
||||
printf '%s' "ep"
|
||||
case CDS CDM
|
||||
printf '%s' "single"
|
||||
case WEB SINGLE-WEB EP-WEB
|
||||
printf '%s' "single"
|
||||
case '*'
|
||||
printf ''
|
||||
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"
|
||||
|
||||
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 -n "$F"
|
||||
set RECORD_TYPE (deezer_record_type "$F")
|
||||
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
|
Loading…
Reference in New Issue
Block a user