mozhi/utils/engines.go

50 lines
2.2 KiB
Go
Raw Normal View History

2023-06-25 00:10:57 +05:30
package utils
import (
"github.com/gocolly/colly"
"os"
)
2023-06-25 00:11:11 +05:30
2023-06-25 00:10:57 +05:30
func TranslateGoogle(to string, from string, query string) string {
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
if !ok {
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
sc := colly.NewCollector(colly.AllowedDomains("translate.google.com"), colly.UserAgent(UserAgent))
var answer string
sc.OnHTML("div.result-container", func(e *colly.HTMLElement) {
answer = e.Text
})
url := "https://translate.google.com/m?sl=" + from + "&tl=" + to + "&hl=" + to + "&q=" + query
sc.Visit(url)
return answer
}
func TranslateReverso(to string, from string, query string) string {
2023-06-25 00:11:11 +05:30
json := []byte(`{ "format": "text", "from": "` + from + `", "to": "` + to + `", "input":"` + query + `", "options": {"sentenceSplitter": false, "origin":"translation.web", contextResults: false, languageDetection: true} }`)
2023-06-25 00:10:57 +05:30
reversoOut := GetRequest("https://api.reverso.net/translate/v1/translation", json)
gjsonArr := reversoOut.Get("translation").Array()
answer := gjsonArr[0].String()
return answer
}
2023-07-03 17:29:46 +05:30
func TranslateLibreTranslate(to string, from string, query string) string {
json := []byte(`{"q":"`+query+`","source":"`+from+`","target":"`+to+`"}`)
// TODO: Make it configurable
libreTranslateOut := GetRequest("https://translate.argosopentech.com/translate", json)
gjsonArr := libreTranslateOut.Get("translatedText").Array()
answer := gjsonArr[0].String()
return answer
}
2023-07-22 11:12:26 +05:30
func TranslateWatson(to string, from string, query string) string {
json := []byte(`{"text":"`+query+`","source":"`+from+`","target":"`+to+`"}`)
watsonOut:= GetRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
answer := gjsonArr[0].String()
return answer
}
2023-06-25 00:10:57 +05:30
func TranslateAll(to string, from string, query string) string {
reverso := TranslateReverso(to, from, query)
google := TranslateGoogle(to, from, query)
2023-07-03 17:29:46 +05:30
libretranslate := TranslateLibreTranslate(to, from, query)
return "Google: " + google + "\nReverso: " + reverso + "\nLibreTranslate: " + libretranslate
2023-06-25 00:10:57 +05:30
}