mirror of
https://codeberg.org/aryak/mozhi
synced 2024-12-22 12:59:46 +05:30
getrequest.go -> requests.go; query string [ENGINES]
This commit is contained in:
parent
5262f4357e
commit
e1c4c4c863
@ -2,10 +2,11 @@ package utils
|
||||
|
||||
import (
|
||||
"github.com/gocolly/colly"
|
||||
"github.com/google/go-querystring/query"
|
||||
"os"
|
||||
)
|
||||
|
||||
func TranslateGoogle(to string, from string, query string) string {
|
||||
func TranslateGoogle(to string, from string, text 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"
|
||||
@ -15,13 +16,21 @@ func TranslateGoogle(to string, from string, query string) 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
|
||||
type Options struct {
|
||||
To string `url:"tl"`
|
||||
UI string `url:"hl"`
|
||||
From string `url:"sl"`
|
||||
Text string `url:"q"`
|
||||
}
|
||||
opt := Options{ to, to, from, text }
|
||||
v, _ := query.Values(opt)
|
||||
url := "https://translate.google.com/m?"+v.Encode()
|
||||
sc.Visit(url)
|
||||
return answer
|
||||
}
|
||||
func TranslateReverso(to string, from string, query string) string {
|
||||
json := []byte(`{ "format": "text", "from": "` + from + `", "to": "` + to + `", "input":"` + query + `", "options": {"sentenceSplitter": false, "origin":"translation.web", contextResults: false, languageDetection: true} }`)
|
||||
reversoOut := GetRequest("https://api.reverso.net/translate/v1/translation", json)
|
||||
reversoOut := PostRequest("https://api.reverso.net/translate/v1/translation", json)
|
||||
gjsonArr := reversoOut.Get("translation").Array()
|
||||
answer := gjsonArr[0].String()
|
||||
return answer
|
||||
@ -29,18 +38,34 @@ func TranslateReverso(to string, from string, query string) string {
|
||||
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)
|
||||
libreTranslateOut := PostRequest("https://translate.argosopentech.com/translate", json)
|
||||
gjsonArr := libreTranslateOut.Get("translatedText").Array()
|
||||
answer := gjsonArr[0].String()
|
||||
return answer
|
||||
}
|
||||
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)
|
||||
watsonOut:= PostRequest("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
|
||||
}
|
||||
func TranslateYandex(to string, from string, text string) string {
|
||||
type Options struct {
|
||||
Translate string `url:"lang"`
|
||||
Text string `url:"text"`
|
||||
Srv string `url:"srv"`
|
||||
Id string `url:"id"`
|
||||
Reason string `url:"reason"`
|
||||
}
|
||||
opt := Options{ from+"-"+to, text, "tr-mobile", "c2317111.64bac36a.ab16ef22.74722d6d6f62696c65-0-0", "submit"}
|
||||
v, _ := query.Values(opt)
|
||||
|
||||
yandexOut := GetRequest("https://translate.yandex.net/api/v1/tr.json/translate?"+v.Encode())
|
||||
gjsonArr := yandexOut.Get("text.0").Array()
|
||||
answer := gjsonArr[0].String()
|
||||
return answer
|
||||
}
|
||||
func TranslateAll(to string, from string, query string) string {
|
||||
reverso := TranslateReverso(to, from, query)
|
||||
google := TranslateGoogle(to, from, query)
|
||||
|
@ -36,7 +36,7 @@ func LangListGoogle(listType string) []List {
|
||||
}
|
||||
func LangListLibreTranslate(listType string) []List {
|
||||
// TODO: Make it configurable
|
||||
libreTranslateOut := GetRequest("https://translate.argosopentech.com/languages", []byte(""))
|
||||
libreTranslateOut := PostRequest("https://translate.argosopentech.com/languages", []byte(""))
|
||||
gjsonArr := libreTranslateOut.Array()
|
||||
var ListData []List
|
||||
for _, r := range gjsonArr {
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetRequest(url string, data []byte) gjson.Result {
|
||||
func PostRequest(url string, data []byte) gjson.Result {
|
||||
bodyReader := bytes.NewReader(data)
|
||||
r, err := http.NewRequest(http.MethodPost, url, bodyReader)
|
||||
if err != nil {
|
||||
@ -39,3 +39,32 @@ func GetRequest(url string, data []byte) gjson.Result {
|
||||
|
||||
return jsonified
|
||||
}
|
||||
func GetRequest(url string) gjson.Result {
|
||||
r, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
//r.Header.Set("Content-Type", "application/json")
|
||||
r.Header.Set("User-Agent", UserAgent)
|
||||
|
||||
client := &http.Client{}
|
||||
res, err := client.Do(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
jsonified := gjson.Parse(string(body))
|
||||
|
||||
return jsonified
|
||||
}
|
Loading…
Reference in New Issue
Block a user