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.

112 lines
2.9KB

  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. function deezer_record_type
  20. switch $argv[1]
  21. case (seq 100)CD CD CDA
  22. printf '%s' "album"
  23. case CDEP EP
  24. printf '%s' "ep"
  25. case CDS CDM
  26. printf '%s' "single"
  27. case WEB SINGLE-WEB EP-WEB
  28. printf '%s' "single"
  29. case '*'
  30. printf ''
  31. end
  32. end
  33. while read -l -d (printf '\t') ORG A T C B F E Y
  34. set _in_A "$A"; set _in_T "$T"; set _in_C "$C";
  35. set _in_B "$B"; set _in_F "$F"; set _in_E "$E"; set _in_Y "$Y"
  36. sleep .2
  37. 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")
  38. if test $status -ne 0
  39. echo "curl API request failed" >&2
  40. rm "$API_OUT"
  41. continue
  42. end
  43. if test $API_CODE -ne 200
  44. begin
  45. echo "Deezer API status code $CODE"
  46. echo $ORG
  47. end >&2
  48. rm "$API_OUT"
  49. continue
  50. end
  51. set API_COUNT (jq -r .total "$API_OUT")
  52. if test $status -ne 0
  53. begin
  54. echo "Failed parsing API result"
  55. echo $ORG
  56. end >&2
  57. rm "$API_OUT"
  58. continue
  59. end
  60. if test -n "$F"
  61. set RECORD_TYPE (deezer_record_type "$F")
  62. end
  63. if test $API_COUNT -eq 0
  64. echo '{}' | jq -SMc --arg rls "$ORG" --arg api_count "$API_COUNT" \
  65. --arg a "$_in_A" --arg t "$_in_T" --arg c "$_in_C" \
  66. --arg b "$_in_B" --arg f "$_in_F" --arg e "$_in_E" --arg y "$_in_Y" \
  67. '{
  68. "_scene_release_name": $rls,
  69. "_source": "api.deezer.com",
  70. "_search_result_count": $api_count | tonumber,
  71. "_artist": $a,
  72. "_title": $t,
  73. "_catno": $c,
  74. "_barcode": $b,
  75. "_format": $f,
  76. "_edition": $e,
  77. "_year": $y
  78. }'
  79. rm "$API_OUT"
  80. continue
  81. else if test $API_COUNT -ge 1
  82. jq -SMc --arg rls "$ORG" --arg api_count "$API_COUNT" \
  83. --arg a "$_in_A" --arg t "$_in_T" --arg c "$_in_C" \
  84. --arg b "$_in_B" --arg f "$_in_F" --arg e "$_in_E" --arg y "$_in_Y" \
  85. '. + {
  86. "_scene_release_name": $rls,
  87. "_source": "api.deezer.com",
  88. "_search_result_count": $api_count | tonumber,
  89. "_artist": $a,
  90. "_title": $t,
  91. "_catno": $c,
  92. "_barcode": $b,
  93. "_format": $f,
  94. "_edition": $e,
  95. "_year": $y
  96. }' "$API_OUT"
  97. end
  98. rm "$API_OUT"
  99. end