fix ddg vqd fetching
This commit is contained in:
parent
18ae6aaff6
commit
0409b30960
3 changed files with 40 additions and 5 deletions
3
go.mod
3
go.mod
|
@ -14,6 +14,7 @@ require (
|
||||||
github.com/google/go-querystring v1.1.0
|
github.com/google/go-querystring v1.1.0
|
||||||
github.com/google/uuid v1.3.1
|
github.com/google/uuid v1.3.1
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
|
github.com/ktr0731/go-fuzzyfinder v0.7.0
|
||||||
github.com/spf13/cobra v1.7.0
|
github.com/spf13/cobra v1.7.0
|
||||||
github.com/swaggo/swag v1.16.1
|
github.com/swaggo/swag v1.16.1
|
||||||
github.com/tidwall/gjson v1.14.4
|
github.com/tidwall/gjson v1.14.4
|
||||||
|
@ -37,13 +38,11 @@ require (
|
||||||
github.com/gobwas/glob v0.2.3 // indirect
|
github.com/gobwas/glob v0.2.3 // indirect
|
||||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||||
github.com/golang/protobuf v1.5.3 // indirect
|
github.com/golang/protobuf v1.5.3 // indirect
|
||||||
github.com/google/go-cmp v0.5.9 // indirect
|
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/josharian/intern v1.0.0 // indirect
|
github.com/josharian/intern v1.0.0 // indirect
|
||||||
github.com/kennygrant/sanitize v1.2.4 // indirect
|
github.com/kennygrant/sanitize v1.2.4 // indirect
|
||||||
github.com/klauspost/compress v1.16.7 // indirect
|
github.com/klauspost/compress v1.16.7 // indirect
|
||||||
github.com/ktr0731/go-ansisgr v0.1.0 // indirect
|
github.com/ktr0731/go-ansisgr v0.1.0 // indirect
|
||||||
github.com/ktr0731/go-fuzzyfinder v0.7.0 // indirect
|
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||||
github.com/mailru/easyjson v0.7.7 // indirect
|
github.com/mailru/easyjson v0.7.7 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
|
1
go.sum
1
go.sum
|
@ -240,6 +240,7 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
|
||||||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
||||||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||||
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||||
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||||
|
|
|
@ -2,7 +2,10 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/OwO-Network/gdeeplx"
|
"github.com/OwO-Network/gdeeplx"
|
||||||
|
@ -11,6 +14,8 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ddgVqd string
|
||||||
|
|
||||||
func TranslateGoogle(to string, from string, text string) (LangOut, error) {
|
func TranslateGoogle(to string, from string, text string) (LangOut, error) {
|
||||||
ToOrig := to
|
ToOrig := to
|
||||||
FromOrig := from
|
FromOrig := from
|
||||||
|
@ -294,6 +299,35 @@ func TranslateDeepl(to string, from string, text string) (LangOut, error) {
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ddgVqdUpdate() {
|
||||||
|
r, err := http.NewRequest("GET", "https://duckduckgo.com/?q=translate", nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
UserAgent, ok := os.LookupEnv("MOZHI_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("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)
|
||||||
|
}
|
||||||
|
re := regexp.MustCompile(`vqd="([^"]*)"`)
|
||||||
|
match := re.FindStringSubmatch(string(body))
|
||||||
|
ddgVqd = match[1]
|
||||||
|
}
|
||||||
|
|
||||||
func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error) {
|
func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error) {
|
||||||
FromOrig := from
|
FromOrig := from
|
||||||
ToOrig := to
|
ToOrig := to
|
||||||
|
@ -317,14 +351,15 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error)
|
||||||
return LangOut{}, errors.New("Source language code invalid")
|
return LangOut{}, errors.New("Source language code invalid")
|
||||||
}
|
}
|
||||||
var url string
|
var url string
|
||||||
|
var langout LangOut
|
||||||
|
ddgVqdUpdate()
|
||||||
if from == "auto" {
|
if from == "auto" {
|
||||||
url = "https://duckduckgo.com/translation.js?vqd=4-80922924764394623683473042291214994119&query=translate&to=" + to
|
url = "https://duckduckgo.com/translation.js?vqd=" + ddgVqd + "&query=translate&to=" + to
|
||||||
} else {
|
} else {
|
||||||
url = "https://duckduckgo.com/translation.js?vqd=4-80922924764394623683473042291214994119&query=translate&to=" + to + "&from=" + from
|
url = "https://duckduckgo.com/translation.js?vqd=" + ddgVqd + "&query=translate&to=" + to + "&from=" + from
|
||||||
}
|
}
|
||||||
duckDuckGoOut := PostRequest(url, []byte(query))
|
duckDuckGoOut := PostRequest(url, []byte(query))
|
||||||
gjsonArr := duckDuckGoOut.Get("translated").Array()
|
gjsonArr := duckDuckGoOut.Get("translated").Array()
|
||||||
var langout LangOut
|
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
langout.Engine = "duckduckgo"
|
langout.Engine = "duckduckgo"
|
||||||
langout.SourceLang = FromOrig
|
langout.SourceLang = FromOrig
|
||||||
|
|
Loading…
Reference in a new issue