fix code formatting
All checks were successful
Build and publish the docker image / build (push) Successful in 1m2s

Signed-off-by: ngn <ngn@ngn.tf>
This commit is contained in:
ngn 2025-04-05 23:51:03 +03:00
parent 06fa48351b
commit 7755d6c2fe
Signed by: ngn
GPG Key ID: A3654DF5AD9F641D
7 changed files with 207 additions and 207 deletions

View File

@ -3,29 +3,29 @@ package extract
import "net/url" import "net/url"
type dictionary struct { type dictionary struct {
ShortName string ShortName string
Name string Name string
Url string Url string
} }
var Dicts []dictionary = []dictionary{ var Dicts []dictionary = []dictionary{
{ShortName: "EN-TR", Name: "Turkish - English", Url: "/turkish-english"}, {ShortName: "EN-TR", Name: "Turkish - English", Url: "/turkish-english"},
{ShortName: "EN-DE", Name: "German - English", Url: "/german-english"}, {ShortName: "EN-DE", Name: "German - English", Url: "/german-english"},
{ShortName: "EN-ES", Name: "Spanish - English", Url: "/spanish-english"}, {ShortName: "EN-ES", Name: "Spanish - English", Url: "/spanish-english"},
{ShortName: "EN-FR", Name: "French - English", Url: "/french-english"}, {ShortName: "EN-FR", Name: "French - English", Url: "/french-english"},
} }
func find_dict(sn string) (*dictionary) { func find_dict(sn string) *dictionary {
for i := range Dicts { for i := range Dicts {
if Dicts[i].ShortName == sn { if Dicts[i].ShortName == sn {
return &Dicts[i] return &Dicts[i]
} }
} }
return nil return nil
} }
func (d *dictionary) Path(term string) string{ func (d *dictionary) Path(term string) string {
term_escaped := url.PathEscape(term) term_escaped := url.PathEscape(term)
return d.Url + "/" + term_escaped return d.Url + "/" + term_escaped
} }

View File

@ -9,79 +9,79 @@ import (
) )
const ( const (
BASE_URL = "https://tureng.com/en/" BASE_URL = "https://tureng.com/en/"
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.3" USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.3"
REFERER = "https://tureng.com/en/turkish-english" REFERER = "https://tureng.com/en/turkish-english"
) )
type Extractor struct { type Extractor struct {
client *http.Client client *http.Client
} }
func (e *Extractor) get(path string) (*http.Response, error) { func (e *Extractor) get(path string) (*http.Response, error) {
var ( var (
err error err error
full_url string full_url string
req *http.Request req *http.Request
res *http.Response res *http.Response
) )
if full_url, err = url.JoinPath(BASE_URL, path); err != nil { if full_url, err = url.JoinPath(BASE_URL, path); err != nil {
return nil, err return nil, err
} }
if req, err = http.NewRequest("GET", full_url, nil); err != nil { if req, err = http.NewRequest("GET", full_url, nil); err != nil {
return nil, err return nil, err
} }
// headers // headers
req.Header.Set("User-Agent", USER_AGENT) req.Header.Set("User-Agent", USER_AGENT)
req.Header.Set("Referer", REFERER) req.Header.Set("Referer", REFERER)
if res, err = e.client.Do(req); err != nil { if res, err = e.client.Do(req); err != nil {
return nil, err return nil, err
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
return nil, fmt.Errorf("bad response code: %d", res.StatusCode) return nil, fmt.Errorf("bad response code: %d", res.StatusCode)
} }
return res, err return res, err
} }
func (e *Extractor) Translate(dict_name string, term string) (*Translation, error) { func (e *Extractor) Translate(dict_name string, term string) (*Translation, error) {
var ( var (
dict *dictionary dict *dictionary
res *http.Response res *http.Response
doc *goquery.Document doc *goquery.Document
err error err error
) )
if dict = find_dict(dict_name); dict == nil { if dict = find_dict(dict_name); dict == nil {
return nil, nil return nil, nil
} }
term_path := dict.Path(term) term_path := dict.Path(term)
if res, err = e.get(term_path); err != nil { if res, err = e.get(term_path); err != nil {
return nil, fmt.Errorf("failed to get %s: %s", term_path, err.Error()) return nil, fmt.Errorf("failed to get %s: %s", term_path, err.Error())
} }
defer res.Body.Close() defer res.Body.Close()
if doc, err = goquery.NewDocumentFromReader(res.Body); err != nil { if doc, err = goquery.NewDocumentFromReader(res.Body); err != nil {
return nil, err return nil, err
} }
trans := Translation{} trans := Translation{}
err = trans.GetResults(doc) err = trans.GetResults(doc)
return &trans, err return &trans, err
} }
func New() (*Extractor, error) { func New() (*Extractor, error) {
var extractor Extractor var extractor Extractor
extractor.client = &http.Client{} extractor.client = &http.Client{}
return &extractor, nil return &extractor, nil
} }

View File

@ -8,106 +8,106 @@ import (
) )
type Entry struct { type Entry struct {
Text string Text string
Detail string Detail string
} }
type Translation struct { type Translation struct {
Suggestions []string Suggestions []string
Fields []string Fields []string
Entries [][]Entry Entries [][]Entry
} }
func (t *Translation) getSuggestions(doc *goquery.Document) error { func (t *Translation) getSuggestions(doc *goquery.Document) error {
doc.Find(".suggestion-list li").Each(func(_ int, sel *goquery.Selection){ doc.Find(".suggestion-list li").Each(func(_ int, sel *goquery.Selection) {
if link := sel.Find("a"); link != nil { if link := sel.Find("a"); link != nil {
t.Suggestions = append(t.Suggestions, link.Text()) t.Suggestions = append(t.Suggestions, link.Text())
} }
}) })
return nil return nil
} }
func (t *Translation) getEntry(tds *goquery.Selection) { func (t *Translation) getEntry(tds *goquery.Selection) {
if tds == nil || tds.Length() < 4{ if tds == nil || tds.Length() < 4 {
return return
} }
var ( var (
entries []Entry entries []Entry
index int = 0 index int = 0
) )
tds.Each(func(_ int,sel *goquery.Selection){ tds.Each(func(_ int, sel *goquery.Selection) {
if index < 1 || index > 3 { if index < 1 || index > 3 {
index++ index++
return return
} }
link := sel.Find("a") link := sel.Find("a")
detail := sel.Find("i") detail := sel.Find("i")
text := "" text := ""
if link.Length() == 0 { if link.Length() == 0 {
text = sel.Text() text = sel.Text()
} else { } else {
text = link.Text() text = link.Text()
} }
if detail == nil { if detail == nil {
entries = append(entries, Entry{ entries = append(entries, Entry{
Text: text, Text: text,
}) })
}else { } else {
entries = append(entries, Entry{ entries = append(entries, Entry{
Text: text, Text: text,
Detail: detail.Text(), Detail: detail.Text(),
}) })
} }
index++ index++
}) })
t.Entries = append(t.Entries, entries) t.Entries = append(t.Entries, entries)
} }
func (t *Translation) getEntries(trs *goquery.Selection) { func (t *Translation) getEntries(trs *goquery.Selection) {
trs.Each(func(_ int, sel *goquery.Selection){ trs.Each(func(_ int, sel *goquery.Selection) {
tds := sel.Find("td") tds := sel.Find("td")
if tds == nil { if tds == nil {
return return
} }
if class, ok := tds.Last().Attr("class"); !ok { if class, ok := tds.Last().Attr("class"); !ok {
return return
}else if !strings.Contains(class, "rc4 hidden-xs"){ } else if !strings.Contains(class, "rc4 hidden-xs") {
return return
} }
t.getEntry(tds) t.getEntry(tds)
}) })
} }
func (t *Translation) GetResults(doc *goquery.Document) error { func (t *Translation) GetResults(doc *goquery.Document) error {
table := doc.Find(".searchResultsTable") table := doc.Find(".searchResultsTable")
if table.Length() == 0 { if table.Length() == 0 {
return t.getSuggestions(doc) return t.getSuggestions(doc)
} }
trs := table.Find("tbody tr") trs := table.Find("tbody tr")
if trs.Length() == 0 { if trs.Length() == 0 {
return fmt.Errorf("failed to get table body") return fmt.Errorf("failed to get table body")
} }
trs.First().Find("th").Each(func(_ int, sel *goquery.Selection){ trs.First().Find("th").Each(func(_ int, sel *goquery.Selection) {
if sel.Text() != "" { if sel.Text() != "" {
t.Fields = append(t.Fields, sel.Text()) t.Fields = append(t.Fields, sel.Text())
} }
}) })
t.getEntries(trs) t.getEntries(trs)
return nil return nil
} }

62
main.go
View File

@ -12,42 +12,42 @@ import (
) )
func main() { func main() {
var ( var (
extractor *extract.Extractor extractor *extract.Extractor
err error err error
) )
// configure logger // configure logger
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
if extractor, err = extract.New(); err != nil { if extractor, err = extract.New(); err != nil {
log.Printf("failed to create an extractor: %s", err.Error()) log.Printf("failed to create an extractor: %s", err.Error())
os.Exit(1) os.Exit(1)
} }
engine := html.New("./views", ".html") engine := html.New("./views", ".html")
app := fiber.New(fiber.Config{ app := fiber.New(fiber.Config{
AppName: "tren", AppName: "tren",
DisableStartupMessage: true, DisableStartupMessage: true,
ServerHeader: "", ServerHeader: "",
Views: engine, Views: engine,
}) })
app.Static("/", "./static") app.Static("/", "./static")
app.Use("*", func(c *fiber.Ctx) error { app.Use("*", func(c *fiber.Ctx) error {
c.Locals("extractor", extractor) c.Locals("extractor", extractor)
return c.Next() return c.Next()
}) })
// routes // routes
app.Get("/", routes.GET_index) app.Get("/", routes.GET_index)
app.Post("/translate", routes.POST_translate) app.Post("/translate", routes.POST_translate)
// all the other routes redirect to index // all the other routes redirect to index
app.All("*", func(c *fiber.Ctx) error { app.All("*", func(c *fiber.Ctx) error {
return util.RenderError(c, 404) return util.RenderError(c, 404)
}) })
log.Printf("starting the web application on port 8080") log.Printf("starting the web application on port 8080")
log.Fatal(app.Listen(":8080")) log.Fatal(app.Listen(":8080"))
} }

View File

@ -6,5 +6,5 @@ import (
) )
func GET_index(c *fiber.Ctx) error { func GET_index(c *fiber.Ctx) error {
return util.Render(c, "index", nil) return util.Render(c, "index", nil)
} }

View File

@ -7,41 +7,41 @@ import (
) )
type translate_form struct { type translate_form struct {
Dictionary string Dictionary string
Term string Term string
} }
func POST_translate(c *fiber.Ctx) error { func POST_translate(c *fiber.Ctx) error {
var ( var (
extractor *extract.Extractor extractor *extract.Extractor
trans *extract.Translation trans *extract.Translation
form translate_form form translate_form
err error err error
) )
extractor = c.Locals("extractor").(*extract.Extractor) extractor = c.Locals("extractor").(*extract.Extractor)
if err = c.BodyParser(&form); err != nil { if err = c.BodyParser(&form); err != nil {
return util.RenderError(c, 400) return util.RenderError(c, 400)
} }
if form.Dictionary == "" || form.Term == "" { if form.Dictionary == "" || form.Term == "" {
return util.RenderError(c, 400) return util.RenderError(c, 400)
} }
if trans, err = extractor.Translate(form.Dictionary, form.Term); err != nil { if trans, err = extractor.Translate(form.Dictionary, form.Term); err != nil {
return util.RenderError(c, 500, err) return util.RenderError(c, 500, err)
} }
if trans == nil { if trans == nil {
return util.RenderError(c, 400) return util.RenderError(c, 400)
} }
return util.Render(c, "translate", fiber.Map{ return util.Render(c, "translate", fiber.Map{
"term": form.Term, "term": form.Term,
"dictionary": form.Dictionary, "dictionary": form.Dictionary,
"fields": trans.Fields, "fields": trans.Fields,
"entries": trans.Entries, "entries": trans.Entries,
"suggestions": trans.Suggestions, "suggestions": trans.Suggestions,
}) })
} }

View File

@ -8,18 +8,18 @@ import (
) )
func Render(c *fiber.Ctx, template string, data fiber.Map) error { func Render(c *fiber.Ctx, template string, data fiber.Map) error {
if data == nil { if data == nil {
data = make(fiber.Map) data = make(fiber.Map)
} }
data["dictionaries"] = extract.Dicts data["dictionaries"] = extract.Dicts
return c.Render(template, data) return c.Render(template, data)
} }
func RenderError(c *fiber.Ctx, code int, err... error) error { func RenderError(c *fiber.Ctx, code int, err ...error) error {
if code == 500 && len(err) > 0 && err[0] != nil{ if code == 500 && len(err) > 0 && err[0] != nil {
log.Printf("server error: %s", err[0].Error()) log.Printf("server error: %s", err[0].Error())
} }
return c.Redirect("/") return c.Redirect("/")
} }