make mozhi not use 100 billion if statements + add TTS err handling

This commit is contained in:
Arya 2023-08-27 19:48:56 +05:30
parent 2efcf283b7
commit 67fb87d524
Signed by: arya
GPG Key ID: 842D12BDA50DF120
5 changed files with 116 additions and 149 deletions

View File

@ -17,72 +17,12 @@ var translateCmd = &cobra.Command{
Short: "Translate.",
Long: `Translate.`,
Run: func(cmd *cobra.Command, args []string) {
if engine == "reverso" {
if langlist == "sl" {
fmt.Println(utils.LangListReverso(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListReverso(langlist))
} else {
fmt.Println(utils.TranslateReverso(dest, source, query))
}
} else if engine == "deepl" {
if langlist == "sl" {
fmt.Println(utils.LangListDeepl(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListDeepl(langlist))
} else {
fmt.Println(utils.TranslateDeepl(dest, source, query))
}
} else if engine == "libretranslate" {
if langlist == "sl" {
fmt.Println(utils.LangListLibreTranslate(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListLibreTranslate(langlist))
} else {
fmt.Println(utils.TranslateLibreTranslate(dest, source, query))
}
} else if engine == "watson" {
if langlist == "sl" {
fmt.Println(utils.LangListWatson(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListWatson(langlist))
} else {
fmt.Println(utils.TranslateWatson(dest, source, query))
}
} else if engine == "yandex" {
if langlist == "sl" {
fmt.Println(utils.LangListYandex(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListYandex(langlist))
} else {
fmt.Println(utils.TranslateYandex(dest, source, query))
}
} else if engine == "duckduckgo" {
if langlist == "sl" {
fmt.Println(utils.LangListDuckDuckGo(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListDuckDuckGo(langlist))
} else {
fmt.Println(utils.TranslateDuckDuckGo(dest, source, query))
}
} else if engine == "mymemory" {
if langlist == "sl" {
fmt.Println(utils.LangListMyMemory(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListMyMemory(langlist))
} else {
fmt.Println(utils.TranslateMyMemory(dest, source, query))
}
} else if engine == "all" {
if langlist == "sl" || langlist == "tl" {
fmt.Println(utils.LangList(engine, langlist))
} else if engine == "all"{
fmt.Println(utils.TranslateAll(dest, source, query))
} else {
if langlist == "sl" {
fmt.Println(utils.LangListGoogle(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListGoogle(langlist))
} else {
fmt.Println(utils.TranslateGoogle(dest, source, query))
}
fmt.Println(utils.Translate(engine, dest, source, query))
}
},
}

View File

@ -8,50 +8,22 @@ import (
func HandleSourceLanguages(c *fiber.Ctx) error {
engine := utils.Sanitize(c.Query("engine"), "alpha")
if engine == "" {
return c.SendStatus(fiber.StatusBadRequest)
return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
}
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")
} else if engine == "watson" {
data = utils.LangListWatson("sl")
} else if engine == "yandex" {
data = utils.LangListYandex("sl")
} else if engine == "mymemory" {
data = utils.LangListMyMemory("sl")
} else if engine == "duckduckgo" {
data = utils.LangListDuckDuckGo("sl")
data, err := utils.LangList(engine, "sl")
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(data)
}
func HandleTargetLanguages(c *fiber.Ctx) error {
engine := utils.Sanitize(c.Query("engine"), "alpha")
if engine == "" {
return c.SendStatus(fiber.StatusBadRequest)
return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
}
var data []utils.List
if engine == "google" {
data = utils.LangListGoogle("tl")
} else if engine == "libre" {
data = utils.LangListLibreTranslate("tl")
} else if engine == "reverso" {
data = utils.LangListReverso("tl")
} else if engine == "deepl" {
data = utils.LangListDeepl("tl")
} else if engine == "watson" {
data = utils.LangListWatson("tl")
} else if engine == "yandex" {
data = utils.LangListYandex("tl")
} else if engine == "mymemory" {
data = utils.LangListMyMemory("tl")
} else if engine == "duckduckgo" {
data = utils.LangListDuckDuckGo("tl")
data, err := utils.LangList(engine, "tl")
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(data)
}
@ -59,19 +31,12 @@ func HandleTTS(c *fiber.Ctx) error {
engine := utils.Sanitize(c.Query("engine"), "alpha")
lang := utils.Sanitize(c.Query("lang"), "alpha")
text := c.Query("text")
// 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)
if engine == "" || text == "" || lang == "" {
return fiber.NewError(fiber.StatusBadRequest, "engine, lang, text are required query strings.")
}
var data []byte
if engine == "google" {
data = utils.TTSGoogle(lang, text)
} else if engine == "reverso" {
data = utils.TTSReverso(lang, text)
data, err := utils.TTS(engine, lang, text)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
c.Set("Content-Type", "audio/mpeg")
return c.Send(data)
@ -81,33 +46,19 @@ func HandleTranslate(c *fiber.Ctx) error {
from := utils.Sanitize(c.Query("from"), "alpha")
to := utils.Sanitize(c.Query("to"), "alpha")
text := c.Query("text")
if engine == "" && from == "" && to == "" && text == "" {
if engine == "" || from == "" || to == "" || text == "" {
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
}
var err error
var data utils.LangOut
var dataarr []utils.LangOut
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)
} else if engine == "duckduckgo" {
data, err = utils.TranslateDuckDuckGo(to, from, text)
} else if engine == "all" {
var data utils.LangOut
var err error
if engine == "all" {
dataarr = utils.TranslateAll(to, from, text)
}
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
data, err = utils.Translate(engine, to, from, text)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
}
if engine == "all" {
return c.JSON(dataarr)

View File

@ -10,14 +10,6 @@ import (
"strings"
)
type LangOut struct {
Engine string `json:"engine"`
AutoDetect string `json:"detected"`
OutputText string `json:"translated-text"`
SourceLang string `json:"source_language"`
TargetLang string `json:"target_language"`
}
func TranslateGoogle(to string, from string, text string) (LangOut, error) {
ToOrig := to
FromOrig := from

View File

@ -1,6 +1,89 @@
package utils
import (
"errors"
)
type List struct {
Name string
Id string
}
type LangOut struct {
Engine string `json:"engine"`
AutoDetect string `json:"detected"`
OutputText string `json:"translated-text"`
SourceLang string `json:"source_language"`
TargetLang string `json:"target_language"`
}
func LangList(engine string, listType string) ([]List, error) {
var data []List
if listType != "sl" && listType != "tl" {
return []List{}, errors.New("list type invalid: either give tl for target languages or sl for source languages.")
}
if engine == "google" {
data = LangListGoogle("sl")
} else if engine == "libre" {
data = LangListLibreTranslate("sl")
} else if engine == "reverso" {
data = LangListReverso("sl")
} else if engine == "deepl" {
data = LangListDeepl("sl")
} else if engine == "watson" {
data = LangListWatson("sl")
} else if engine == "yandex" {
data = LangListYandex("sl")
} else if engine == "mymemory" {
data = LangListMyMemory("sl")
} else if engine == "duckduckgo" {
data = LangListDuckDuckGo("sl")
} else {
return []List{} , errors.New("Engine does not exist.")
}
return data, nil
}
// General function to translate stuff so there is no need for a huge if-block everywhere
func Translate(engine string, to string, from string, text string) (LangOut, error) {
var err error
var data LangOut
if engine == "google" {
data, err = TranslateGoogle(to, from, text)
} else if engine == "libre" {
data, err = TranslateLibreTranslate(to, from, text)
} else if engine == "reverso" {
data, err = TranslateReverso(to, from, text)
} else if engine == "deepl" {
data, err = TranslateDeepl(to, from, text)
} else if engine == "watson" {
data, err = TranslateWatson(to, from, text)
} else if engine == "yandex" {
data, err = TranslateYandex(to, from, text)
} else if engine == "mymemory" {
data, err = TranslateMyMemory(to, from, text)
} else if engine == "duckduckgo" {
data, err = TranslateDuckDuckGo(to, from, text)
} else {
return LangOut{}, errors.New("Engine does not exist.")
}
if err != nil {
return LangOut{}, err
}
return data, nil
}
func TTS(engine string, lang string, text string) ([]byte, error) {
var err error
var data []byte
if engine == "google" {
data, err = TTSGoogle(lang, text)
} else if engine == "reverso" {
data, err = TTSReverso(lang, text)
} else {
return []byte(""), errors.New("Engine does not exist and/or doesn't support TTS.")
}
if err != nil {
return []byte(""), err
}
return data, nil
}

View File

@ -13,7 +13,7 @@ type ReversoTTS struct {
Voice string
}
func TTSGoogle(lang string, text string) []byte {
func TTSGoogle(lang string, text string) ([]byte, error) {
type Options struct {
Lang string `url:"tl"`
Text string `url:"q"`
@ -29,11 +29,12 @@ func TTSGoogle(lang string, text string) []byte {
ToString(&file).
Fetch(context.Background())
if err != nil {
file = ""
return []byte(""), err
}
return []byte(file)
return []byte(file), nil
}
func TTSReverso(lang string, text string) []byte {
func TTSReverso(lang string, text string) ([]byte, error) {
var TTSData = []ReversoTTS{
// http://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetAvailableVoices with randomized deduplication
ReversoTTS{
@ -147,7 +148,7 @@ func TTSReverso(lang string, text string) []byte {
UserAgent(UserAgent).
Fetch(context.Background())
if err != nil {
file = ""
return []byte(""), err
}
return []byte(file)
return []byte(file), nil
}