add duckduckgo

This commit is contained in:
Arya Kiran 2023-08-15 22:09:38 +05:30
parent f7ec6b556d
commit dae465a210
No known key found for this signature in database
GPG key ID: 842D12BDA50DF120
5 changed files with 45 additions and 4 deletions

View file

@ -8,5 +8,4 @@
- Alternative words
- Dictionary
- Use the TranslateServerWebui API instead of scraping Google Translate Legacy
- Bing Translate
- ICIBA

View file

@ -57,6 +57,14 @@ var translateCmd = &cobra.Command{
} 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))

View file

@ -25,6 +25,8 @@ func HandleSourceLanguages(c *fiber.Ctx) error {
data = utils.LangListYandex("sl")
} else if engine == "mymemory" {
data = utils.LangListMyMemory("sl")
} else if engine == "duckduckgo" {
data = utils.LangListDuckDuckGo("sl")
}
return c.JSON(data)
}
@ -48,6 +50,8 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
data = utils.LangListYandex("tl")
} else if engine == "mymemory" {
data = utils.LangListMyMemory("tl")
} else if engine == "duckduckgo" {
data = utils.LangListDuckDuckGo("tl")
}
return c.JSON(data)
}
@ -96,6 +100,8 @@ func HandleTranslate(c *fiber.Ctx) error {
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)
}
if err != nil {
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
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)
google, _ := TranslateGoogle(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)
yandex, _ := TranslateYandex(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
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
var ListData = []List{
List{