mozhi/pages/api.go

72 lines
2.1 KiB
Go
Raw Normal View History

2023-07-06 16:24:45 +05:30
package pages
import (
"codeberg.org/aryak/mozhi/utils"
2023-07-06 16:24:45 +05:30
"github.com/gofiber/fiber/v2"
)
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 := utils.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-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 := utils.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-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 := utils.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-08-16 11:58:03 +05:30
return c.Send(data)
2023-07-06 16:24:45 +05:30
}
2023-08-27 19:54:30 +05:30
2023-08-15 21:41:02 +05:30
func HandleTranslate(c *fiber.Ctx) error {
engine := utils.Sanitize(c.Query("engine"), "alpha")
from := utils.Sanitize(c.Query("from"), "alpha")
to := utils.Sanitize(c.Query("to"), "alpha")
text := c.Query("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.")
}
2023-08-16 11:58:03 +05:30
var dataarr []utils.LangOut
2023-08-27 19:54:30 +05:30
var data utils.LangOut
var err error
if engine == "all" {
2023-08-16 11:58:03 +05:30
dataarr = utils.TranslateAll(to, from, text)
} else {
data, err = utils.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
}