deepl autodetect + gofmt

This commit is contained in:
Arya 2023-08-17 19:39:18 +05:30
parent 6b7a9b3f0d
commit e666fc63cc
Signed by: arya
GPG Key ID: 842D12BDA50DF120
5 changed files with 15 additions and 9 deletions

6
go.mod
View File

@ -2,12 +2,16 @@ module codeberg.org/aryak/simplytranslate
go 1.20 go 1.20
replace github.com/OwO-Network/gdeeplx => github.com/gi-yt/gdeeplx v0.0.0-20230817133036-0eb71706cd51
require ( require (
github.com/OwO-Network/gdeeplx v0.0.1
github.com/carlmjohnson/requests v0.23.4 github.com/carlmjohnson/requests v0.23.4
github.com/gocolly/colly v1.2.0 github.com/gocolly/colly v1.2.0
github.com/gofiber/fiber/v2 v2.47.0 github.com/gofiber/fiber/v2 v2.47.0
github.com/gofiber/template v1.8.0 github.com/gofiber/template v1.8.0
github.com/google/go-querystring v1.1.0 github.com/google/go-querystring v1.1.0
github.com/google/uuid v1.3.0
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/otiai10/gosseract/v2 v2.4.0 github.com/otiai10/gosseract/v2 v2.4.0
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0
@ -15,7 +19,6 @@ require (
) )
require ( require (
github.com/OwO-Network/gdeeplx v0.0.1 // indirect
github.com/PuerkitoBio/goquery v1.8.1 // indirect github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/abadojack/whatlanggo v1.0.1 // indirect github.com/abadojack/whatlanggo v1.0.1 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect github.com/andybalholm/brotli v1.0.5 // indirect
@ -27,7 +30,6 @@ require (
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/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.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

4
go.sum
View File

@ -54,8 +54,6 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
github.com/Joker/jade v1.1.3/go.mod h1:T+2WLyt7VH6Lp0TRxQrUYEs64nRc83wkMQrfeIQKduM= github.com/Joker/jade v1.1.3/go.mod h1:T+2WLyt7VH6Lp0TRxQrUYEs64nRc83wkMQrfeIQKduM=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/OwO-Network/gdeeplx v0.0.1 h1:uWkXp1AiD3jAgsg9Gw5ak8TH6ZZ/RjgfFXgYA8kyE3g=
github.com/OwO-Network/gdeeplx v0.0.1/go.mod h1:xLqaj0n6E2VmiI1RXBOFy8wLoiOurvO3O6QxUvepyVo=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM= github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ= github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/abadojack/whatlanggo v1.0.1 h1:19N6YogDnf71CTHm3Mp2qhYfkRdyvbgwWdd2EPxJRG4= github.com/abadojack/whatlanggo v1.0.1 h1:19N6YogDnf71CTHm3Mp2qhYfkRdyvbgwWdd2EPxJRG4=
@ -137,6 +135,8 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF
github.com/flosch/pongo2/v4 v4.0.2/go.mod h1:B5ObFANs/36VwxxlgKpdchIJHMvHB562PW+BWPhwZD8= github.com/flosch/pongo2/v4 v4.0.2/go.mod h1:B5ObFANs/36VwxxlgKpdchIJHMvHB562PW+BWPhwZD8=
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gi-yt/gdeeplx v0.0.0-20230817133036-0eb71706cd51 h1:UsBBzLOcgAHzumecyEwnwlvF/cfxizNJvVilj9zbQMc=
github.com/gi-yt/gdeeplx v0.0.0-20230817133036-0eb71706cd51/go.mod h1:xLqaj0n6E2VmiI1RXBOFy8wLoiOurvO3O6QxUvepyVo=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=

View File

@ -81,7 +81,7 @@ func HandleTranslate(c *fiber.Ctx) error {
from := utils.Sanitize(c.Query("from"), "alpha") from := utils.Sanitize(c.Query("from"), "alpha")
to := utils.Sanitize(c.Query("to"), "alpha") to := utils.Sanitize(c.Query("to"), "alpha")
text := c.Query("text") text := c.Query("text")
if engine == "" && from == "" && to == "" && text == ""{ if engine == "" && from == "" && to == "" && text == "" {
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.") return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
} }
var err error var err error

View File

@ -1,4 +1,5 @@
package utils package utils
func AutoDetectWatson(query string) (string, error) { func AutoDetectWatson(query string) (string, error) {
json := []byte(`{"text":"` + query + `"}`) json := []byte(`{"text":"` + query + `"}`)
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/detect", json) watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/detect", json)

View File

@ -11,7 +11,7 @@ import (
) )
type LangOut struct { type LangOut struct {
Engine string `json:"engine"` Engine string `json:"engine"`
AutoDetect string `json:"detected"` AutoDetect string `json:"detected"`
OutputText string `json:"translated-text"` OutputText string `json:"translated-text"`
SourceLang string `json:"source_language"` SourceLang string `json:"source_language"`
@ -287,6 +287,9 @@ func TranslateDeepl(to string, from string, text string) (LangOut, error) {
var langout LangOut var langout LangOut
langout.OutputText = ans langout.OutputText = ans
langout.Engine = "deepl" langout.Engine = "deepl"
if from == "auto" {
langout.AutoDetect = strings.ToLower(answer1["detected_lang"].(string))
}
langout.SourceLang = FromOrig langout.SourceLang = FromOrig
langout.TargetLang = ToOrig langout.TargetLang = ToOrig
return langout, nil return langout, nil
@ -315,9 +318,9 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error)
} }
var url string var url string
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=4-80922924764394623683473042291214994119&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=4-80922924764394623683473042291214994119&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()
@ -331,7 +334,7 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error)
} }
return langout, nil return langout, nil
} }
func TranslateAll(to string, from string, query string) ([]LangOut) { func TranslateAll(to string, from string, query string) []LangOut {
reverso, _ := TranslateReverso(to, from, query) reverso, _ := TranslateReverso(to, from, query)
google, _ := TranslateGoogle(to, from, query) google, _ := TranslateGoogle(to, from, query)
libretranslate, _ := TranslateLibreTranslate(to, from, query) libretranslate, _ := TranslateLibreTranslate(to, from, query)