scene release to discogs release
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.8KB

  1. #!/usr/bin/env fish
  2. #set fish_trace on
  3. function usage
  4. begin
  5. echo "Read TSV release data from sdtin > query Deezer API > output JSON"
  6. exit 1
  7. end
  8. end
  9. argparse -n (status current-filename) 'h/help' -- $argv
  10. if test $status -ne 0
  11. usage
  12. end
  13. if set -q _flag_help
  14. usage
  15. end
  16. set API_OUT (mktemp -t rls.deezer.API.XXXXXXXX.json)
  17. set DAPI "https://api.deezer.com/search/album"
  18. set UA "search-deezer.fish/v0.0.3"
  19. while read -l -d (printf '\t') ORG A T C B F E Y
  20. if test -z "$A"; or test -z "$T"
  21. echo "WARN: Skipping empty artist / title input"
  22. continue
  23. end
  24. set _in_A "$A"; set _in_T "$T"; set _in_C "$C";
  25. set _in_B "$B"; set _in_F "$F"; set _in_E "$E"; set _in_Y "$Y"
  26. sleep .2
  27. 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")
  28. if test $status -ne 0
  29. echo "curl API request failed" >&2
  30. rm "$API_OUT"
  31. continue
  32. end
  33. if test $API_CODE -ne 200
  34. begin
  35. echo "Deezer API status code $CODE"
  36. echo $ORG
  37. end >&2
  38. rm "$API_OUT"
  39. continue
  40. end
  41. set API_COUNT (jq -r .total "$API_OUT")
  42. if test $status -ne 0
  43. begin
  44. echo "Failed parsing API result"
  45. echo $ORG
  46. end >&2
  47. rm "$API_OUT"
  48. continue
  49. end
  50. if test "$API_COUNT" = "null"
  51. begin
  52. echo "ERR: API result has no total count"
  53. echo $ORG
  54. end >&2
  55. rm "$API_OUT"
  56. continue
  57. end
  58. if test $API_COUNT -eq 0
  59. echo '{}' | jq -SMc --arg rls "$ORG" --arg api_count "$API_COUNT" \
  60. --arg a "$_in_A" --arg t "$_in_T" --arg c "$_in_C" \
  61. --arg b "$_in_B" --arg f "$_in_F" --arg e "$_in_E" --arg y "$_in_Y" \
  62. '{
  63. "_scene_release_name": $rls,
  64. "_source": "api.deezer.com",
  65. "_search_result_count": $api_count | tonumber,
  66. "_artist": $a,
  67. "_title": $t,
  68. "_catno": $c,
  69. "_barcode": $b,
  70. "_format": $f,
  71. "_edition": $e,
  72. "_year": $y
  73. }'
  74. rm "$API_OUT"
  75. continue
  76. else if test $API_COUNT -ge 1
  77. jq -SMc --arg rls "$ORG" --arg api_count "$API_COUNT" \
  78. --arg a "$_in_A" --arg t "$_in_T" --arg c "$_in_C" \
  79. --arg b "$_in_B" --arg f "$_in_F" --arg e "$_in_E" --arg y "$_in_Y" \
  80. '. + {
  81. "_scene_release_name": $rls,
  82. "_source": "api.deezer.com",
  83. "_search_result_count": $api_count | tonumber,
  84. "_artist": $a,
  85. "_title": $t,
  86. "_catno": $c,
  87. "_barcode": $b,
  88. "_format": $f,
  89. "_edition": $e,
  90. "_year": $y
  91. }' "$API_OUT"
  92. end
  93. rm "$API_OUT"
  94. end