This commit is contained in:
Arya 2023-06-25 00:11:11 +05:30
parent a4eb13cdac
commit a1c229e107
Signed by: arya
GPG Key ID: 842D12BDA50DF120
3 changed files with 66 additions and 65 deletions

View File

@ -1,9 +1,9 @@
package cmd package cmd
import ( import (
"github.com/spf13/cobra"
"codeberg.org/aryak/simplytranslate/utils" "codeberg.org/aryak/simplytranslate/utils"
"fmt" "fmt"
"github.com/spf13/cobra"
) )
var engine string var engine string

View File

@ -1,11 +1,12 @@
package utils package utils
import ( import (
"fmt"
"github.com/gocolly/colly" "github.com/gocolly/colly"
"os" "os"
"fmt"
"strings" "strings"
) )
type List struct { type List struct {
Name string Name string
Id string Id string
@ -26,7 +27,7 @@ func TranslateGoogle(to string, from string, query string) string {
return answer return answer
} }
func TranslateReverso(to string, from string, query string) string { 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} }`) 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 := GetRequest("https://api.reverso.net/translate/v1/translation", json)
gjsonArr := reversoOut.Get("translation").Array() gjsonArr := reversoOut.Get("translation").Array()
answer := gjsonArr[0].String() answer := gjsonArr[0].String()
@ -35,9 +36,9 @@ func TranslateReverso(to string, from string, query string) string {
func TranslateAll(to string, from string, query string) string { func TranslateAll(to string, from string, query string) string {
reverso := TranslateReverso(to, from, query) reverso := TranslateReverso(to, from, query)
google := TranslateGoogle(to, from, query) google := TranslateGoogle(to, from, query)
fmt.Println("Google: "+google) fmt.Println("Google: " + google)
fmt.Println("Reverso: "+reverso) fmt.Println("Reverso: " + reverso)
return "Google: "+google + "\nReverso: "+reverso return "Google: " + google + "\nReverso: " + reverso
} }
func LangListGoogle(listType string) []List { func LangListGoogle(listType string) []List {
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT") UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
@ -51,122 +52,122 @@ func LangListGoogle(listType string) []List {
var ListInfo List var ListInfo List
ListInfo.Name = el.ChildText("a") ListInfo.Name = el.ChildText("a")
if listType == "tl" { if listType == "tl" {
ListInfo.Id = strings.TrimPrefix(strings.TrimSuffix(el.ChildAttr("a", "href"), "&hl=en-US"), "./m?sl=en&tl=") ListInfo.Id = strings.TrimPrefix(strings.TrimSuffix(el.ChildAttr("a", "href"), "&hl=en-US"), "./m?sl=en&tl=")
} else { } else {
ListInfo.Id = strings.TrimPrefix(strings.TrimSuffix(el.ChildAttr("a", "href"), "&tl=en&hl=en-US"), "./m?sl=") ListInfo.Id = strings.TrimPrefix(strings.TrimSuffix(el.ChildAttr("a", "href"), "&tl=en&hl=en-US"), "./m?sl=")
} }
ListData = append(ListData, ListInfo) ListData = append(ListData, ListInfo)
}) })
}) })
url := "https://translate.google.com/m?sl=en&tl=en&hl=en-US&mui="+listType url := "https://translate.google.com/m?sl=en&tl=en&hl=en-US&mui=" + listType
sc.Visit(url) sc.Visit(url)
return ListData return ListData
} }
func LangListReverso(listType string) []List { func LangListReverso(listType string) []List {
// IDs got from original simplytranslate-web and trial and error. Usually first three letters of language. // IDs got from original simplytranslate-web and trial and error. Usually first three letters of language.
var ListData = []List { var ListData = []List{
List { List{
Id: "ara", Id: "ara",
Name: "Arabic", Name: "Arabic",
}, },
List { List{
Id: "chi", Id: "chi",
Name: "Chinese (Simplified)", Name: "Chinese (Simplified)",
}, },
List { List{
Id: "cze", Id: "cze",
Name: "Czech", Name: "Czech",
}, },
List { List{
Id: "dan", Id: "dan",
Name: "Danish", Name: "Danish",
}, },
List { List{
Id: "dut", Id: "dut",
Name: "Dutch", Name: "Dutch",
}, },
List { List{
Id: "eng", Id: "eng",
Name: "English", Name: "English",
}, },
List { List{
Id: "fra", Id: "fra",
Name: "French", Name: "French",
}, },
List { List{
Id: "ger", Id: "ger",
Name: "German", Name: "German",
}, },
List { List{
Id: "gre", Id: "gre",
Name: "Greek", Name: "Greek",
}, },
List { List{
Id: "Hebrew", Id: "Hebrew",
Name: "heb", Name: "heb",
}, },
List { List{
Id: "hin", Id: "hin",
Name: "Hindi", Name: "Hindi",
}, },
List { List{
Id: "hun", Id: "hun",
Name: "Hungarian", Name: "Hungarian",
}, },
List { List{
Id: "ita", Id: "ita",
Name: "Italian", Name: "Italian",
}, },
List { List{
Id: "jpn", Id: "jpn",
Name: "Japanese", Name: "Japanese",
}, },
List { List{
Id: "kor", Id: "kor",
Name: "Korean", Name: "Korean",
}, },
List { List{
Id: "per", Id: "per",
Name: "Persian", Name: "Persian",
}, },
List { List{
Id: "pol", Id: "pol",
Name: "Polish", Name: "Polish",
}, },
List { List{
Id: "por", Id: "por",
Name: "Portugese", Name: "Portugese",
}, },
List { List{
Id: "rum", Id: "rum",
Name: "Romanian", Name: "Romanian",
}, },
List { List{
Id: "rus", Id: "rus",
Name: "Russian", Name: "Russian",
}, },
List { List{
Id: "slo", Id: "slo",
Name: "Slovakian", Name: "Slovakian",
}, },
List { List{
Id: "spa", Id: "spa",
Name: "Spanish", Name: "Spanish",
}, },
List { List{
Id: "swe", Id: "swe",
Name: "Swedish", Name: "Swedish",
}, },
List { List{
Id: "tha", Id: "tha",
Name: "Thai", Name: "Thai",
}, },
List { List{
Id: "tur", Id: "tur",
Name: "Turkish", Name: "Turkish",
}, },
List { List{
Id: "ukr", Id: "ukr",
Name: "Ukrainian", Name: "Ukrainian",
}, },
} }

View File

@ -1,8 +1,8 @@
package utils package utils
import ( import (
"github.com/tidwall/gjson"
"bytes" "bytes"
"github.com/tidwall/gjson"
"io" "io"
"net/http" "net/http"
"os" "os"