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

This commit is contained in:
Arya Kiran 2023-08-27 19:48:56 +05:30
parent 2efcf283b7
commit 67fb87d524
No known key found for this signature in database
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.", Short: "Translate.",
Long: `Translate.`, Long: `Translate.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if engine == "reverso" { if langlist == "sl" || langlist == "tl" {
if langlist == "sl" { fmt.Println(utils.LangList(engine, langlist))
fmt.Println(utils.LangListReverso(langlist)) } else if engine == "all"{
} 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" {
fmt.Println(utils.TranslateAll(dest, source, query)) fmt.Println(utils.TranslateAll(dest, source, query))
} else { } else {
if langlist == "sl" { fmt.Println(utils.Translate(engine, dest, source, query))
fmt.Println(utils.LangListGoogle(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListGoogle(langlist))
} else {
fmt.Println(utils.TranslateGoogle(dest, source, query))
}
} }
}, },
} }

View file

@ -8,50 +8,22 @@ import (
func HandleSourceLanguages(c *fiber.Ctx) error { func HandleSourceLanguages(c *fiber.Ctx) error {
engine := utils.Sanitize(c.Query("engine"), "alpha") engine := utils.Sanitize(c.Query("engine"), "alpha")
if engine == "" { if engine == "" {
return c.SendStatus(fiber.StatusBadRequest) return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
} }
var data []utils.List data, err := utils.LangList(engine, "sl")
if engine == "google" { if err != nil {
data = utils.LangListGoogle("sl") return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} 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")
} }
return c.JSON(data) return c.JSON(data)
} }
func HandleTargetLanguages(c *fiber.Ctx) error { func HandleTargetLanguages(c *fiber.Ctx) error {
engine := utils.Sanitize(c.Query("engine"), "alpha") engine := utils.Sanitize(c.Query("engine"), "alpha")
if engine == "" { if engine == "" {
return c.SendStatus(fiber.StatusBadRequest) return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
} }
var data []utils.List data, err := utils.LangList(engine, "tl")
if engine == "google" { if err != nil {
data = utils.LangListGoogle("tl") return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} 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")
} }
return c.JSON(data) return c.JSON(data)
} }
@ -59,19 +31,12 @@ func HandleTTS(c *fiber.Ctx) error {
engine := utils.Sanitize(c.Query("engine"), "alpha") engine := utils.Sanitize(c.Query("engine"), "alpha")
lang := utils.Sanitize(c.Query("lang"), "alpha") lang := utils.Sanitize(c.Query("lang"), "alpha")
text := c.Query("text") text := c.Query("text")
// Why does go not have an andor statement :( if engine == "" || text == "" || lang == "" {
if engine == "" { return fiber.NewError(fiber.StatusBadRequest, "engine, lang, text are required query strings.")
return c.SendStatus(fiber.StatusBadRequest)
} else if text == "" {
return c.SendStatus(fiber.StatusBadRequest)
} else if lang == "" {
return c.SendStatus(fiber.StatusBadRequest)
} }
var data []byte data, err := utils.TTS(engine, lang, text)
if engine == "google" { if err != nil {
data = utils.TTSGoogle(lang, text) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else if engine == "reverso" {
data = utils.TTSReverso(lang, text)
} }
c.Set("Content-Type", "audio/mpeg") c.Set("Content-Type", "audio/mpeg")
return c.Send(data) return c.Send(data)
@ -81,34 +46,20 @@ func HandleTranslate(c *fiber.Ctx) error {
from := utils.Sanitize(c.Query("from"), "alpha") from := utils.Sanitize(c.Query("from"), "alpha")
to := utils.Sanitize(c.Query("to"), "alpha") to := utils.Sanitize(c.Query("to"), "alpha")
text := c.Query("text") 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.") return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
} }
var err error
var data utils.LangOut
var dataarr []utils.LangOut var dataarr []utils.LangOut
if engine == "google" { var data utils.LangOut
data, err = utils.TranslateGoogle(to, from, text) var err error
} else if engine == "libre" { if engine == "all" {
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" {
dataarr = utils.TranslateAll(to, from, text) dataarr = utils.TranslateAll(to, from, text)
} } else {
data, err = utils.Translate(engine, to, from, text)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }
}
if engine == "all" { if engine == "all" {
return c.JSON(dataarr) return c.JSON(dataarr)
} else { } else {

View file

@ -10,14 +10,6 @@ import (
"strings" "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) { func TranslateGoogle(to string, from string, text string) (LangOut, error) {
ToOrig := to ToOrig := to
FromOrig := from FromOrig := from

View file

@ -1,6 +1,89 @@
package utils package utils
import (
"errors"
)
type List struct { type List struct {
Name string Name string
Id 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 Voice string
} }
func TTSGoogle(lang string, text string) []byte { func TTSGoogle(lang string, text string) ([]byte, error) {
type Options struct { type Options struct {
Lang string `url:"tl"` Lang string `url:"tl"`
Text string `url:"q"` Text string `url:"q"`
@ -29,11 +29,12 @@ func TTSGoogle(lang string, text string) []byte {
ToString(&file). ToString(&file).
Fetch(context.Background()) Fetch(context.Background())
if err != nil { 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{ var TTSData = []ReversoTTS{
// http://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetAvailableVoices with randomized deduplication // http://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetAvailableVoices with randomized deduplication
ReversoTTS{ ReversoTTS{
@ -147,7 +148,7 @@ func TTSReverso(lang string, text string) []byte {
UserAgent(UserAgent). UserAgent(UserAgent).
Fetch(context.Background()) Fetch(context.Background())
if err != nil { if err != nil {
file = "" return []byte(""), err
} }
return []byte(file) return []byte(file), nil
} }