Started work on persistent session data

This commit is contained in:
Davide Passoni
2024-12-01 12:40:07 +01:00
parent 42e62be0f5
commit 5e40d7abf1
20 changed files with 794 additions and 252 deletions

View File

@@ -1,7 +1,6 @@
import express = require("express");
import fs = require("fs");
var gtts = require("node-gtts")("en");
const gtts = require("node-gtts")("en");
const speech = require("@google-cloud/speech");
const router = express.Router();
module.exports = function () {
@@ -10,37 +9,30 @@ module.exports = function () {
gtts.stream(req.body.text).pipe(res);
});
router.get("/recognize", (req, res, next) => {
//// Imports the Google Cloud client library
//const speech = require("@google-cloud/speech");
//
//// Creates a client
//const client = new speech.SpeechClient();
//
//// The path to the remote LINEAR16 file
//const gcsUri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw";
//
//// The audio file's encoding, sample rate in hertz, and BCP-47 language code
//const audio = {
// uri: gcsUri,
//};
//const config = {
// encoding: "LINEAR16",
// sampleRateHertz: 16000,
// languageCode: "en-US",
//};
//const request = {
// audio: audio,
// config: config,
//};
//
//// Detects speech in the audio file
//client.recognize(request).then((response) => {
// const transcription = response.results
// .map((result) => result.alternatives[0].transcript)
// .join("\n");
// console.log(`Transcription: ${transcription}`);
//});
router.put("/recognize", (req, res, next) => {
// Creates a client
const client = new speech.SpeechClient();
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: req.body.data.substring(req.body.data.indexOf("base64,") + 7),
};
const config = {
encoding: "WEBM_OPUS",
languageCode: "en-US"
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
client.recognize(request).then((response) => {
const transcription = response[0].results
.map((result) => result.alternatives[0].transcript)
.join("\n");
res.send(transcription)
}).catch((error) => res.sendStatus(400));
});
return router;

View File

@@ -2,6 +2,9 @@ import express = require("express");
import fs = require("fs");
const router = express.Router();
let sessionHash = "";
let sessionData = {}
module.exports = function (configLocation) {
router.get("/config", function (req, res, next) {
if (fs.existsSync(configLocation)) {
@@ -70,5 +73,29 @@ module.exports = function (configLocation) {
}
});
router.put("/sessiondata/save/:profileName", function (req, res, next) {
if (req.body.sessionHash === undefined || req.body.sessionData === undefined) res.sendStatus(400);
let thisSessionHash = req.body.sessionHash;
if (thisSessionHash !== sessionHash) {
sessionHash = thisSessionHash;
sessionData = {};
}
sessionData[req.params.profileName] = req.body.sessionData;
res.end()
})
router.put("/sessiondata/load/:profileName", function (req, res, next) {
if (req.body.sessionHash === undefined) res.sendStatus(400);
let thisSessionHash = req.body.sessionHash;
if (thisSessionHash !== sessionHash) {
sessionHash = thisSessionHash;
sessionData = {};
res.sendStatus(404);
} else {
res.send(sessionData[req.params.profileName]);
res.end();
}
})
return router;
};