add all engine suppor to translate api
This commit is contained in:
parent
dae465a210
commit
75f94266d7
4 changed files with 62 additions and 15 deletions
3
TODO.md
3
TODO.md
|
@ -1,11 +1,10 @@
|
||||||
# TODO
|
# TODO
|
||||||
- Create a web interface
|
- Create a web interface
|
||||||
- Proper Error handling for requests.go
|
- Proper Error handling for requests.go
|
||||||
- Use struct instead of string in many places
|
|
||||||
- Tell which language Detect Language chose
|
- Tell which language Detect Language chose
|
||||||
- Add speech to text with Vosk perhaps
|
|
||||||
- Finish simplytranslate-py compatible API for translations
|
- Finish simplytranslate-py compatible API for translations
|
||||||
- Alternative words
|
- Alternative words
|
||||||
- Dictionary
|
- Dictionary
|
||||||
- Use the TranslateServerWebui API instead of scraping Google Translate Legacy
|
- Use the TranslateServerWebui API instead of scraping Google Translate Legacy
|
||||||
- ICIBA
|
- ICIBA
|
||||||
|
- Add speech to text with Vosk perhaps
|
||||||
|
|
17
pages/api.go
17
pages/api.go
|
@ -67,14 +67,14 @@ func HandleTTS(c *fiber.Ctx) error {
|
||||||
} else if lang == "" {
|
} else if lang == "" {
|
||||||
return c.SendStatus(fiber.StatusBadRequest)
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
}
|
}
|
||||||
var data string
|
var data []byte
|
||||||
if engine == "google" {
|
if engine == "google" {
|
||||||
data = utils.TTSGoogle(lang, text)
|
data = utils.TTSGoogle(lang, text)
|
||||||
} else if engine == "reverso" {
|
} else if engine == "reverso" {
|
||||||
data = utils.TTSReverso(lang, text)
|
data = utils.TTSReverso(lang, text)
|
||||||
}
|
}
|
||||||
c.Set("Content-Type", "audio/mpeg")
|
c.Set("Content-Type", "audio/mpeg")
|
||||||
return c.Send([]byte(data))
|
return c.Send(data)
|
||||||
}
|
}
|
||||||
func HandleTranslate(c *fiber.Ctx) error {
|
func HandleTranslate(c *fiber.Ctx) error {
|
||||||
engine := utils.Sanitize(c.Query("engine"), "alpha")
|
engine := utils.Sanitize(c.Query("engine"), "alpha")
|
||||||
|
@ -84,8 +84,9 @@ func HandleTranslate(c *fiber.Ctx) error {
|
||||||
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 data utils.LangOut
|
|
||||||
var err error
|
var err error
|
||||||
|
var data utils.LangOut
|
||||||
|
var dataarr []utils.LangOut
|
||||||
if engine == "google" {
|
if engine == "google" {
|
||||||
data, err = utils.TranslateGoogle(to, from, text)
|
data, err = utils.TranslateGoogle(to, from, text)
|
||||||
} else if engine == "libre" {
|
} else if engine == "libre" {
|
||||||
|
@ -102,11 +103,15 @@ func HandleTranslate(c *fiber.Ctx) error {
|
||||||
data, err = utils.TranslateMyMemory(to, from, text)
|
data, err = utils.TranslateMyMemory(to, from, text)
|
||||||
} else if engine == "duckduckgo" {
|
} else if engine == "duckduckgo" {
|
||||||
data, err = utils.TranslateDuckDuckGo(to, from, text)
|
data, err = utils.TranslateDuckDuckGo(to, from, text)
|
||||||
|
} else if engine == "all" {
|
||||||
|
dataarr = utils.TranslateAll(to, from, text)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
data.SourceLang = from
|
if engine == "all" {
|
||||||
data.TargetLang = to
|
return c.JSON(dataarr)
|
||||||
return c.JSON(data)
|
} else {
|
||||||
|
return c.JSON(data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,13 +11,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type LangOut struct {
|
type LangOut struct {
|
||||||
OutputText string `json:"translated-text"`
|
Engine string `json:"engine"`
|
||||||
AutoDetect string `json:"detected"`
|
AutoDetect string `json:"detected"`
|
||||||
|
OutputText string `json:"translated-text"`
|
||||||
SourceLang string `json:"source_language"`
|
SourceLang string `json:"source_language"`
|
||||||
TargetLang string `json:"target_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
|
||||||
|
FromOrig := from
|
||||||
// For some reason google uses no for norwegian instead of nb like the rest of the translators. This is for the All function primarily
|
// For some reason google uses no for norwegian instead of nb like the rest of the translators. This is for the All function primarily
|
||||||
if to == "nb" {
|
if to == "nb" {
|
||||||
to = "no"
|
to = "no"
|
||||||
|
@ -65,9 +68,14 @@ func TranslateGoogle(to string, from string, text string) (LangOut, error) {
|
||||||
sc.Visit(url)
|
sc.Visit(url)
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = answer
|
langout.OutputText = answer
|
||||||
|
langout.Engine = "google"
|
||||||
|
langout.SourceLang = FromOrig
|
||||||
|
langout.TargetLang = ToOrig
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateReverso(to string, from string, query string) (LangOut, error) {
|
func TranslateReverso(to string, from string, query string) (LangOut, error) {
|
||||||
|
ToOrig := to
|
||||||
|
FromOrig := from
|
||||||
var ToValid bool
|
var ToValid bool
|
||||||
var FromValid bool
|
var FromValid bool
|
||||||
for _, v := range LangListReverso("sl") {
|
for _, v := range LangListReverso("sl") {
|
||||||
|
@ -92,9 +100,14 @@ func TranslateReverso(to string, from string, query string) (LangOut, error) {
|
||||||
gjsonArr := reversoOut.Get("translation").Array()
|
gjsonArr := reversoOut.Get("translation").Array()
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
|
langout.Engine = "reverso"
|
||||||
|
langout.SourceLang = FromOrig
|
||||||
|
langout.TargetLang = ToOrig
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateLibreTranslate(to string, from string, query string) (LangOut, error) {
|
func TranslateLibreTranslate(to string, from string, query string) (LangOut, error) {
|
||||||
|
ToOrig := to
|
||||||
|
FromOrig := from
|
||||||
var ToValid bool
|
var ToValid bool
|
||||||
var FromValid bool
|
var FromValid bool
|
||||||
for _, v := range LangListLibreTranslate("sl") {
|
for _, v := range LangListLibreTranslate("sl") {
|
||||||
|
@ -120,9 +133,14 @@ func TranslateLibreTranslate(to string, from string, query string) (LangOut, err
|
||||||
gjsonArr := libreTranslateOut.Get("translatedText").Array()
|
gjsonArr := libreTranslateOut.Get("translatedText").Array()
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
|
langout.Engine = "libretranslate"
|
||||||
|
langout.SourceLang = FromOrig
|
||||||
|
langout.TargetLang = ToOrig
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateWatson(to string, from string, query string) (LangOut, error) {
|
func TranslateWatson(to string, from string, query string) (LangOut, error) {
|
||||||
|
FromOrig := from
|
||||||
|
ToOrig := to
|
||||||
var ToValid bool
|
var ToValid bool
|
||||||
var FromValid bool
|
var FromValid bool
|
||||||
for _, v := range LangListWatson("sl") {
|
for _, v := range LangListWatson("sl") {
|
||||||
|
@ -147,9 +165,14 @@ func TranslateWatson(to string, from string, query string) (LangOut, error) {
|
||||||
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
|
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
|
langout.Engine = "watson"
|
||||||
|
langout.SourceLang = FromOrig
|
||||||
|
langout.TargetLang = ToOrig
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateMyMemory(to string, from string, text string) (LangOut, error) {
|
func TranslateMyMemory(to string, from string, text string) (LangOut, error) {
|
||||||
|
FromOrig := from
|
||||||
|
ToOrig := to
|
||||||
var ToValid bool
|
var ToValid bool
|
||||||
var FromValid bool
|
var FromValid bool
|
||||||
for _, v := range LangListMyMemory("sl") {
|
for _, v := range LangListMyMemory("sl") {
|
||||||
|
@ -179,9 +202,14 @@ func TranslateMyMemory(to string, from string, text string) (LangOut, error) {
|
||||||
gjsonArr := myMemoryOut.Get("responseData.translatedText").Array()
|
gjsonArr := myMemoryOut.Get("responseData.translatedText").Array()
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
|
langout.Engine = "mymemory"
|
||||||
|
langout.SourceLang = FromOrig
|
||||||
|
langout.TargetLang = ToOrig
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateYandex(to string, from string, text string) (LangOut, error) {
|
func TranslateYandex(to string, from string, text string) (LangOut, error) {
|
||||||
|
FromOrig := from
|
||||||
|
ToOrig := to
|
||||||
var ToValid bool
|
var ToValid bool
|
||||||
var FromValid bool
|
var FromValid bool
|
||||||
for _, v := range LangListYandex("sl") {
|
for _, v := range LangListYandex("sl") {
|
||||||
|
@ -216,9 +244,14 @@ func TranslateYandex(to string, from string, text string) (LangOut, error) {
|
||||||
gjsonArr := yandexOut.Get("text.0").Array()
|
gjsonArr := yandexOut.Get("text.0").Array()
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
|
langout.Engine = "yandex"
|
||||||
|
langout.SourceLang = FromOrig
|
||||||
|
langout.TargetLang = ToOrig
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateDeepl(to string, from string, text string) (LangOut, error) {
|
func TranslateDeepl(to string, from string, text string) (LangOut, error) {
|
||||||
|
FromOrig := from
|
||||||
|
ToOrig := to
|
||||||
var ToValid bool
|
var ToValid bool
|
||||||
var FromValid bool
|
var FromValid bool
|
||||||
for _, v := range LangListDeepl("sl") {
|
for _, v := range LangListDeepl("sl") {
|
||||||
|
@ -246,9 +279,14 @@ func TranslateDeepl(to string, from string, text string) (LangOut, error) {
|
||||||
ans := answer1["data"].(string)
|
ans := answer1["data"].(string)
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = ans
|
langout.OutputText = ans
|
||||||
|
langout.Engine = "deepl"
|
||||||
|
langout.SourceLang = FromOrig
|
||||||
|
langout.TargetLang = ToOrig
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error) {
|
func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error) {
|
||||||
|
FromOrig := from
|
||||||
|
ToOrig := to
|
||||||
var ToValid bool
|
var ToValid bool
|
||||||
var FromValid bool
|
var FromValid bool
|
||||||
for _, v := range LangListDuckDuckGo("sl") {
|
for _, v := range LangListDuckDuckGo("sl") {
|
||||||
|
@ -272,9 +310,12 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error)
|
||||||
gjsonArr := duckDuckGoOut.Get("translated").Array()
|
gjsonArr := duckDuckGoOut.Get("translated").Array()
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
|
langout.Engine = "duckduckgo"
|
||||||
|
langout.SourceLang = FromOrig
|
||||||
|
langout.TargetLang = ToOrig
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateAll(to string, from string, query string) (string, string, string, string, string, string, string, string) {
|
func TranslateAll(to string, from string, query string) ([]LangOut) {
|
||||||
reverso, _ := TranslateReverso(to, from, query)
|
reverso, _ := TranslateReverso(to, from, query)
|
||||||
google, _ := TranslateGoogle(to, from, query)
|
google, _ := TranslateGoogle(to, from, query)
|
||||||
libretranslate, _ := TranslateLibreTranslate(to, from, query)
|
libretranslate, _ := TranslateLibreTranslate(to, from, query)
|
||||||
|
@ -283,5 +324,7 @@ func TranslateAll(to string, from string, query string) (string, string, string,
|
||||||
yandex, _ := TranslateYandex(to, from, query)
|
yandex, _ := TranslateYandex(to, from, query)
|
||||||
deepl, _ := TranslateDeepl(to, from, query)
|
deepl, _ := TranslateDeepl(to, from, query)
|
||||||
duckduckgo, _ := TranslateDuckDuckGo(to, from, query)
|
duckduckgo, _ := TranslateDuckDuckGo(to, from, query)
|
||||||
return google.OutputText, reverso.OutputText, libretranslate.OutputText, watson.OutputText, mymemory.OutputText, yandex.OutputText, deepl.OutputText, duckduckgo.OutputText
|
|
||||||
|
langout := []LangOut{reverso, google, libretranslate, watson, mymemory, yandex, deepl, duckduckgo}
|
||||||
|
return langout
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ type ReversoTTS struct {
|
||||||
Voice string
|
Voice string
|
||||||
}
|
}
|
||||||
|
|
||||||
func TTSGoogle(lang string, text string) string {
|
func TTSGoogle(lang string, text string) []byte {
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Lang string `url:"tl"`
|
Lang string `url:"tl"`
|
||||||
Text string `url:"q"`
|
Text string `url:"q"`
|
||||||
|
@ -31,9 +31,9 @@ func TTSGoogle(lang string, text string) string {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
file = ""
|
file = ""
|
||||||
}
|
}
|
||||||
return file
|
return []byte(file)
|
||||||
}
|
}
|
||||||
func TTSReverso(lang string, text string) string {
|
func TTSReverso(lang string, text string) []byte {
|
||||||
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{
|
||||||
|
@ -149,5 +149,5 @@ func TTSReverso(lang string, text string) string {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
file = ""
|
file = ""
|
||||||
}
|
}
|
||||||
return file
|
return []byte(file)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue