Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/acrcloud/lib/index.js
1126 views
1
"use strict";
2
/// <reference types="./" />
3
exports.__esModule = true;
4
var fetch = require("node-fetch");
5
var FormData = require("form-data");
6
var crypto = require("crypto");
7
var acr = /** @class */ (function () {
8
function acr(config) {
9
this.endpoint = "/v1/identify";
10
this.signature_version = "1";
11
var host = config.host, access_key = config.access_key, access_secret = config.access_secret, data_type = config.data_type, audio_format = config.audio_format, sample_rate = config.sample_rate, audio_channels = config.audio_channels;
12
this.host = host || "identify-us-west-2.acrcloud.com";
13
this.access_key = access_key;
14
this.access_secret = access_secret;
15
this.data_type = data_type || "audio";
16
// Optional settings
17
this.audio_format = audio_format || "";
18
this.sample_rate = sample_rate || "";
19
this.audio_channels = audio_channels || 2;
20
}
21
// Builds a signature string for making API requests
22
acr.prototype.buildStringToSign = function (method, uri, accessKey, dataType, signatureVersion, timestamp) {
23
return [method, uri, accessKey, dataType, signatureVersion, timestamp].join("\n");
24
};
25
// Signs a signature string
26
acr.prototype.sign = function (string, access_secret) {
27
return crypto
28
.createHmac("sha1", access_secret)
29
.update(Buffer.from(string, "utf-8"))
30
.digest()
31
.toString("base64");
32
};
33
// Generates form data from an object
34
acr.prototype.generateFormData = function (object) {
35
var form = new FormData();
36
Object.keys(object).forEach(function (key) {
37
form.append(key, object[key]);
38
});
39
return form;
40
};
41
/**
42
* Identify an audio track from a file path
43
* @param {Buffer} audio_sample A file path to an audio file or a buffer from an audio sample of the audio you want to identify
44
* @returns {Promise<ACRCloudResponse>} response JSON from ACRCloud https://www.acrcloud.com/docs/acrcloud/metadata/music/
45
*/
46
acr.prototype.identify = function (audio_sample) {
47
var current_date = new Date();
48
var timestamp = current_date.getTime() / 1000;
49
var stringToSign = this.buildStringToSign("POST", this.endpoint, this.access_key, this.data_type, this.signature_version, timestamp);
50
var signature = this.sign(stringToSign, this.access_secret);
51
var formData = {
52
sample: audio_sample,
53
access_key: this.access_key,
54
data_type: this.data_type,
55
signature_version: this.signature_version,
56
signature: signature,
57
sample_bytes: audio_sample.length,
58
timestamp: timestamp
59
};
60
return fetch("https://" + this.host + "/" + this.endpoint, {
61
method: "POST",
62
body: this.generateFormData(formData)
63
}).then(function (response) { return response.json(); });
64
};
65
return acr;
66
}());
67
module.exports = acr;
68
69