mozhi/pages/api.go

102 lines
3.3 KiB
Go
Raw Normal View History

2023-07-06 16:24:45 +05:30
package pages
import (
"codeberg.org/aryak/libmozhi"
2023-09-10 18:04:08 +05:30
"codeberg.org/aryak/mozhi/utils"
2023-07-06 16:24:45 +05:30
"github.com/gofiber/fiber/v2"
)
2023-08-27 21:27:46 +05:30
// HandleSourceLanguages godoc
2023-08-29 14:50:56 +05:30
//
2023-08-27 21:27:46 +05:30
// @Summary Show list of available source languages for engine
// @Param engine query string true "Engine name"
// @Success 200 {object} libmozhi.List
// @Router /api/source_languages [get]
2023-07-06 16:24:45 +05:30
func HandleSourceLanguages(c *fiber.Ctx) error {
2023-08-15 21:41:02 +05:30
engine := utils.Sanitize(c.Query("engine"), "alpha")
2023-07-06 16:24:45 +05:30
if engine == "" {
return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
2023-07-06 16:24:45 +05:30
}
data, err := libmozhi.LangList(engine, "sl")
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
2023-07-22 19:52:38 +05:30
}
2023-07-06 16:24:45 +05:30
return c.JSON(data)
}
2023-08-27 19:54:30 +05:30
2023-08-27 21:27:46 +05:30
// HandleTargetLanguages godoc
2023-08-29 14:50:56 +05:30
//
2023-08-27 21:27:46 +05:30
// @Summary Show list of available target languages for engine
// @Param engine query string true "Engine name"
// @Success 200 {object} libmozhi.List
// @Router /api/target_languages [get]
2023-07-06 16:24:45 +05:30
func HandleTargetLanguages(c *fiber.Ctx) error {
2023-08-15 21:41:02 +05:30
engine := utils.Sanitize(c.Query("engine"), "alpha")
2023-07-06 16:24:45 +05:30
if engine == "" {
return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
2023-07-06 16:24:45 +05:30
}
data, err := libmozhi.LangList(engine, "tl")
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
2023-07-22 19:52:38 +05:30
}
2023-07-06 16:24:45 +05:30
return c.JSON(data)
}
2023-08-27 19:54:30 +05:30
2023-08-27 21:27:46 +05:30
// HandleTTS godoc
2023-08-29 14:50:56 +05:30
//
2023-08-27 21:27:46 +05:30
// @Summary Get Text-To-Speech for specified language using specified engine
// @Param engine query string true "Engine name"
// @Param lang query string true "Language being TTS'd"
// @Param text query string true "Text being TTS'd"
// @Router /api/tts [get]
2023-07-06 16:24:45 +05:30
func HandleTTS(c *fiber.Ctx) error {
2023-08-15 21:41:02 +05:30
engine := utils.Sanitize(c.Query("engine"), "alpha")
lang := utils.Sanitize(c.Query("lang"), "alpha")
2023-07-22 19:54:15 +05:30
text := c.Query("text")
if engine == "" || text == "" || lang == "" {
return fiber.NewError(fiber.StatusBadRequest, "engine, lang, text are required query strings.")
2023-07-06 16:24:45 +05:30
}
data, err := libmozhi.TTS(engine, lang, text)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
2023-07-06 16:24:45 +05:30
}
2023-07-22 19:54:15 +05:30
c.Set("Content-Type", "audio/mpeg")
2023-09-10 18:44:17 +05:30
return c.Status(fiber.StatusOK).Send(data)
2023-07-06 16:24:45 +05:30
}
2023-08-27 19:54:30 +05:30
2023-08-27 21:27:46 +05:30
// HandleTranslate godoc
2023-08-29 14:50:56 +05:30
//
// @Summary Translate text
// @Description When engine is set to all, it will return an array of libmozhi.LangOut.
// @Param engine query string true "Engine name"
// @Param from query string true "Source language"
// @Param to query string true "Target language"
// @Param text query string true "Text being translated"
2023-09-10 18:04:08 +05:30
// @Success 200 {object} libmozhi.LangOut
// @Router /api/translate [get]
2023-08-15 21:41:02 +05:30
func HandleTranslate(c *fiber.Ctx) error {
engine := utils.Sanitize(getQueryOrFormValue(c, "engine"), "alpha")
from := utils.Sanitize(getQueryOrFormValue(c, "from"), "alpha")
to := utils.Sanitize(getQueryOrFormValue(c, "to"), "alpha")
text := getQueryOrFormValue(c, "text")
if engine == "" || from == "" || to == "" || text == "" {
2023-08-15 21:41:02 +05:30
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
}
var dataarr []libmozhi.LangOut
var data libmozhi.LangOut
2023-08-27 19:54:30 +05:30
var err error
if engine == "all" {
dataarr = libmozhi.TranslateAll(to, from, text)
} else {
data, err = libmozhi.Translate(engine, to, from, text)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
2023-08-15 21:41:02 +05:30
}
2023-08-16 11:58:03 +05:30
if engine == "all" {
return c.JSON(dataarr)
} else {
return c.JSON(data)
}
2023-08-15 21:41:02 +05:30
}