mozhi/pages/api.go

107 lines
3.2 KiB
Go
Raw Normal View History

2023-07-06 16:24:45 +05:30
package pages
import (
"codeberg.org/aryak/simplytranslate/utils"
"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 c.SendStatus(fiber.StatusBadRequest)
}
var data []utils.List
if engine == "google" {
data = utils.LangListGoogle("sl")
} else if engine == "libre" {
data = utils.LangListLibreTranslate("sl")
} else if engine == "reverso" {
data = utils.LangListReverso("sl")
} else if engine == "deepl" {
data = utils.LangListDeepl("sl")
2023-07-22 11:12:26 +05:30
} else if engine == "watson" {
data = utils.LangListWatson("sl")
2023-07-22 19:52:38 +05:30
} else if engine == "yandex" {
data = utils.LangListYandex("sl")
2023-07-23 11:32:48 +05:30
} else if engine == "mymemory" {
data = utils.LangListMyMemory("sl")
2023-07-22 19:52:38 +05:30
}
2023-07-06 16:24:45 +05:30
return c.JSON(data)
}
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 c.SendStatus(fiber.StatusBadRequest)
}
var data []utils.List
if engine == "google" {
data = utils.LangListGoogle("tl")
} else if engine == "libre" {
2023-08-15 21:41:02 +05:30
data = utils.LangListLibreTranslate("tl")
2023-07-06 16:24:45 +05:30
} else if engine == "reverso" {
data = utils.LangListReverso("tl")
} else if engine == "deepl" {
data = utils.LangListDeepl("tl")
2023-07-22 11:12:26 +05:30
} else if engine == "watson" {
data = utils.LangListWatson("tl")
2023-07-22 19:52:38 +05:30
} else if engine == "yandex" {
data = utils.LangListYandex("tl")
2023-07-23 11:32:48 +05:30
} else if engine == "mymemory" {
data = utils.LangListMyMemory("tl")
2023-07-22 19:52:38 +05:30
}
2023-07-06 16:24:45 +05:30
return c.JSON(data)
}
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")
2023-07-06 16:24:45 +05:30
// Why does go not have an andor statement :(
if engine == "" {
return c.SendStatus(fiber.StatusBadRequest)
} else if text == "" {
return c.SendStatus(fiber.StatusBadRequest)
} else if lang == "" {
return c.SendStatus(fiber.StatusBadRequest)
}
var data string
if engine == "google" {
data = utils.TTSGoogle(lang, text)
} else if engine == "reverso" {
data = utils.TTSReverso(lang, text)
}
2023-07-22 19:54:15 +05:30
c.Set("Content-Type", "audio/mpeg")
2023-07-06 16:24:45 +05:30
return c.Send([]byte(data))
}
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 == ""{
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
}
var data utils.LangOut
var err error
if engine == "google" {
data, err = utils.TranslateGoogle(to, from, text)
} else if engine == "libre" {
data, err = utils.TranslateLibreTranslate(to, from, text)
} else if engine == "reverso" {
data, err = utils.TranslateReverso(to, from, text)
} else if engine == "deepl" {
data, err = utils.TranslateDeepl(to, from, text)
} else if engine == "watson" {
data, err = utils.TranslateWatson(to, from, text)
} else if engine == "yandex" {
data, err = utils.TranslateYandex(to, from, text)
} else if engine == "mymemory" {
data, err = utils.TranslateMyMemory(to, from, text)
}
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
data.SourceLang = from
data.TargetLang = to
return c.JSON(data)
}