From 0409b309609e5c6a39b88de7b173e9c224252489 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Wed, 30 Aug 2023 14:41:20 +0530 Subject: [PATCH] fix ddg vqd fetching --- go.mod | 3 +-- go.sum | 1 + utils/engines.go | 41 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 4e43a39..af8c8f0 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/google/go-querystring v1.1.0 github.com/google/uuid v1.3.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/swaggo/swag v1.16.1 github.com/tidwall/gjson v1.14.4 @@ -37,13 +38,11 @@ require ( github.com/gobwas/glob v0.2.3 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // 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/josharian/intern v1.0.0 // indirect github.com/kennygrant/sanitize v1.2.4 // indirect github.com/klauspost/compress v1.16.7 // 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/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index 09cb99a..d6772d1 100644 --- a/go.sum +++ b/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/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= 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/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= diff --git a/utils/engines.go b/utils/engines.go index 17d14cc..44ce3e7 100644 --- a/utils/engines.go +++ b/utils/engines.go @@ -2,7 +2,10 @@ package utils import ( "errors" + "io" + "net/http" "os" + "regexp" "strings" "github.com/OwO-Network/gdeeplx" @@ -11,6 +14,8 @@ import ( "github.com/google/uuid" ) +var ddgVqd string + func TranslateGoogle(to string, from string, text string) (LangOut, error) { ToOrig := to FromOrig := from @@ -294,6 +299,35 @@ func TranslateDeepl(to string, from string, text string) (LangOut, error) { 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) { FromOrig := from ToOrig := to @@ -317,14 +351,15 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error) return LangOut{}, errors.New("Source language code invalid") } var url string + var langout LangOut + ddgVqdUpdate() 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 { - 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)) gjsonArr := duckDuckGoOut.Get("translated").Array() - var langout LangOut langout.OutputText = gjsonArr[0].String() langout.Engine = "duckduckgo" langout.SourceLang = FromOrig