From dae465a2107bcc9d93d6b050a44554d8609b1973 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Tue, 15 Aug 2023 22:09:38 +0530 Subject: [PATCH] add duckduckgo --- TODO.md | 1 - cmd/translate.go | 8 +++++ pages/api.go | 6 ++++ utils/engines.go | 31 +++++++++++++++++-- ...anglist-bing.go => langlist-duckduckgo.go} | 3 +- 5 files changed, 45 insertions(+), 4 deletions(-) rename utils/{langlist-bing.go => langlist-duckduckgo.go} (98%) diff --git a/TODO.md b/TODO.md index 50ae089..fe7ca72 100644 --- a/TODO.md +++ b/TODO.md @@ -8,5 +8,4 @@ - Alternative words - Dictionary - Use the TranslateServerWebui API instead of scraping Google Translate Legacy -- Bing Translate - ICIBA diff --git a/cmd/translate.go b/cmd/translate.go index bc6733e..d2f60a1 100644 --- a/cmd/translate.go +++ b/cmd/translate.go @@ -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)) diff --git a/pages/api.go b/pages/api.go index bbe4d8d..17757d7 100644 --- a/pages/api.go +++ b/pages/api.go @@ -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()) diff --git a/utils/engines.go b/utils/engines.go index 6b4ad65..2b36ccf 100644 --- a/utils/engines.go +++ b/utils/engines.go @@ -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 } diff --git a/utils/langlist-bing.go b/utils/langlist-duckduckgo.go similarity index 98% rename from utils/langlist-bing.go rename to utils/langlist-duckduckgo.go index ff3482b..f90552d 100644 --- a/utils/langlist-bing.go +++ b/utils/langlist-duckduckgo.go @@ -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{