add duckduckgo

This commit is contained in:
Arya 2023-08-15 22:09:38 +05:30
parent f7ec6b556d
commit dae465a210
Signed by: arya
GPG Key ID: 842D12BDA50DF120
5 changed files with 45 additions and 4 deletions

View File

@ -8,5 +8,4 @@
- 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
- Bing Translate
- ICIBA - ICIBA

View File

@ -57,6 +57,14 @@ var translateCmd = &cobra.Command{
} else { } else {
fmt.Println(utils.TranslateYandex(dest, source, query)) 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" { } else if engine == "mymemory" {
if langlist == "sl" { if langlist == "sl" {
fmt.Println(utils.LangListMyMemory(langlist)) fmt.Println(utils.LangListMyMemory(langlist))

View File

@ -25,6 +25,8 @@ func HandleSourceLanguages(c *fiber.Ctx) error {
data = utils.LangListYandex("sl") data = utils.LangListYandex("sl")
} else if engine == "mymemory" { } else if engine == "mymemory" {
data = utils.LangListMyMemory("sl") data = utils.LangListMyMemory("sl")
} else if engine == "duckduckgo" {
data = utils.LangListDuckDuckGo("sl")
} }
return c.JSON(data) return c.JSON(data)
} }
@ -48,6 +50,8 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
data = utils.LangListYandex("tl") data = utils.LangListYandex("tl")
} else if engine == "mymemory" { } else if engine == "mymemory" {
data = utils.LangListMyMemory("tl") data = utils.LangListMyMemory("tl")
} else if engine == "duckduckgo" {
data = utils.LangListDuckDuckGo("tl")
} }
return c.JSON(data) return c.JSON(data)
} }
@ -96,6 +100,8 @@ func HandleTranslate(c *fiber.Ctx) error {
data, err = utils.TranslateYandex(to, from, text) data, err = utils.TranslateYandex(to, from, text)
} else if engine == "mymemory" { } else if engine == "mymemory" {
data, err = utils.TranslateMyMemory(to, from, text) data, err = utils.TranslateMyMemory(to, from, text)
} else if engine == "duckduckgo" {
data, err = utils.TranslateDuckDuckGo(to, from, text)
} }
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())

View File

@ -248,7 +248,33 @@ func TranslateDeepl(to string, from string, text string) (LangOut, error) {
langout.OutputText = ans langout.OutputText = ans
return langout, nil return langout, nil
} }
func TranslateAll(to string, from string, query string) (string, string, string, string, string, string, string) { func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error) {
var ToValid bool
var FromValid bool
for _, v := range LangListDuckDuckGo("sl") {
if v.Id == to {
ToValid = true
}
if v.Id == from {
FromValid = true
}
if FromValid == true && ToValid == true {
break
}
}
if ToValid != true {
return LangOut{}, errors.New("Target language code invalid")
}
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
}
duckDuckGoOut := PostRequest("https://duckduckgo.com/translation.js?vqd=4-80922924764394623683473042291214994119&query=translate&to="+to+"&from="+from, []byte(query))
gjsonArr := duckDuckGoOut.Get("translated").Array()
var langout LangOut
langout.OutputText = gjsonArr[0].String()
return langout, nil
}
func TranslateAll(to string, from string, query string) (string, string, string, string, string, string, string, string) {
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)
@ -256,5 +282,6 @@ func TranslateAll(to string, from string, query string) (string, string, string,
mymemory, _ := TranslateMyMemory(to, from, query) mymemory, _ := TranslateMyMemory(to, from, query)
yandex, _ := TranslateYandex(to, from, query) yandex, _ := TranslateYandex(to, from, query)
deepl, _ := TranslateDeepl(to, from, query) deepl, _ := TranslateDeepl(to, from, query)
return google.OutputText, reverso.OutputText, libretranslate.OutputText, watson.OutputText, mymemory.OutputText, yandex.OutputText, deepl.OutputText duckduckgo, _ := TranslateDuckDuckGo(to, from, query)
return google.OutputText, reverso.OutputText, libretranslate.OutputText, watson.OutputText, mymemory.OutputText, yandex.OutputText, deepl.OutputText, duckduckgo.OutputText
} }

View File

@ -1,6 +1,7 @@
package utils package utils
func LangListBing(listType string) []List { func LangListDuckDuckGo(listType string) []List {
// DuckDuckGo is just Bing translate but easier to scrape :)
// Converted json of https://github.com/plainheart/bing-translate-api/blob/master/src/lang.json to this format // Converted json of https://github.com/plainheart/bing-translate-api/blob/master/src/lang.json to this format
var ListData = []List{ var ListData = []List{
List{ List{