Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions pkg/detectors/audd/audd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package audd

import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"strings"

Expand Down Expand Up @@ -47,26 +49,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}

if verify {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.audd.io/setCallbackUrl/?api_token=%s&url=https://yourwebsite.com/callbacks_handler/", resMatch), nil)
if err != nil {
continue
}
res, err := client.Do(req)
if err == nil {
bodyBytes, err := io.ReadAll(res.Body)
if err == nil {
bodyString := string(bodyBytes)
validResponse := strings.Contains(bodyString, `"status":"success"`)
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
if validResponse {
s1.Verified = true
} else {
s1.Verified = false
}
}
}
}
isVerified, err := verifyMatch(ctx, client, resMatch)
s1.Verified = isVerified
s1.SetVerificationError(err, resMatch)
}

results = append(results, s1)
Expand All @@ -75,10 +60,50 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
return results, nil
}

func verifyMatch(ctx context.Context, client *http.Client, key string) (bool, error) {
url := "https://api.audd.io/getStreams"

var data bytes.Buffer
writer := multipart.NewWriter(&data)
if err := writer.WriteField("api_token", key); err != nil {
return false, err
}
writer.Close()

req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &data)
if err != nil {
return false, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())

res, err := client.Do(req)
if err != nil {
return false, err
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()

switch res.StatusCode {
case http.StatusOK:
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return false, err
}
body := string(bodyBytes)
return strings.Contains(body, `"status":"success"`), nil
case http.StatusUnauthorized:
return false, nil
default:
return false, fmt.Errorf("unexpected status code %d", res.StatusCode)
}
}

func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_Audd
}

func (s Scanner) Description() string {
return "Audd is a music recognition service. Audd API tokens can be used to access the Audd API services for recognizing music and obtaining metadata."
return "AudD is a music recognition service. AudD API tokens can be used to access the AudD API services for recognizing music and obtaining metadata."
}