From 9550f76265805e9bfe0616a7834a9eed4d95e368 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Thu, 15 Oct 2020 18:38:21 +0200 Subject: Add multi language support --- grimtube.go | 7 +++++-- static/style-halloween.css | 8 ++++++++ static/style.css | 16 ++++++++++++++++ templates/search.html | 14 ++++++++++---- ytparser/ytparser.go | 23 ++++++++++++++--------- 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/grimtube.go b/grimtube.go index 9851c8d..416bc7f 100644 --- a/grimtube.go +++ b/grimtube.go @@ -40,6 +40,7 @@ func search(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() term := query.Get("term") sPage := query.Get("page") + lang := query.Get("lang") var page int if sPage == "" { page = 0 @@ -51,8 +52,8 @@ func search(w http.ResponseWriter, r *http.Request) { page = p } } - log.Printf("searching: %s, page: %d\n", term, page) - items, err := ytparser.Search(term, page) + log.Printf("searching: %s, page: %d, lang: %s\n", term, page, lang) + items, err := ytparser.Search(term, page, lang) if err != nil { log.Println(err) data := struct { @@ -66,10 +67,12 @@ func search(w http.ResponseWriter, r *http.Request) { Items []ytparser.Item Term string Page int + Lang string }{ items, term, page, + lang, } serve(w, "templates/search.html", data) } diff --git a/static/style-halloween.css b/static/style-halloween.css index def4dac..0598e83 100644 --- a/static/style-halloween.css +++ b/static/style-halloween.css @@ -54,3 +54,11 @@ iframe { .comments { font-size: small; } + +input#term { + width: 90%; +} + +select#lang { + width: calc(10% - 10px); +} diff --git a/static/style.css b/static/style.css index 62ec755..a4f71c5 100644 --- a/static/style.css +++ b/static/style.css @@ -44,3 +44,19 @@ iframe { .comments { font-size: small; } + +input#term { + width: 90%; + height: 22px; + border: 1px solid black; + padding: 0px; + margin: 0px; +} + +select#lang { + width: calc(10% - 8px); + height: 22px; + border: 1px solid black; + padding: 0px; + margin: 0px; +} diff --git a/templates/search.html b/templates/search.html index 517e5d0..e5a8e34 100644 --- a/templates/search.html +++ b/templates/search.html @@ -2,6 +2,11 @@ {{define "content"}}
+
{{range .Items}} @@ -10,8 +15,9 @@ {{end}}
- {{.Title}}
- Comments +

{{.Title}}

+

Comments

+

{{.Published}}

{{.ChannelTitle}} @@ -19,6 +25,6 @@
- Prev Page | - Next Page + Prev Page | + Next Page {{end}} diff --git a/ytparser/ytparser.go b/ytparser/ytparser.go index 57c2a31..fe9ae66 100644 --- a/ytparser/ytparser.go +++ b/ytparser/ytparser.go @@ -22,6 +22,7 @@ type Item struct { Thumb string ChannelTitle string ChannelUrl string + Published string } func (item Item) Format(t *template.Template) string { @@ -38,10 +39,8 @@ func (item Item) String() string { } func parsejson(data string) ([]Item, error) { - //fmt.Println(data) dec := json.NewDecoder(strings.NewReader(data)) - depth := 0 isArray := false isValue := false @@ -116,6 +115,12 @@ func parsejson(data string) ([]Item, error) { names[depth-1] == "url" { item.Thumb = t } + if depth >= 3 && + names[depth-3] == "videoRenderer" && + names[depth-2] == "publishedTimeText" && + names[depth-1] == "simpleText" { + item.Published = t + } isValue = false } } @@ -123,20 +128,18 @@ func parsejson(data string) ([]Item, error) { } } return items, nil - //fmt.Println(names) } func PrintItems(items []Item, format string) { t := template.Must(template.New("items").Parse(format)) for _, i := range items { fmt.Println(i.Format(t)) - //fmt.Println(i) } } -func request(query string, page int) (string, error) { +func request(query string, page int, lang string) (string, error) { q := url.QueryEscape(query) - url := fmt.Sprintf("https://www.youtube.com/results?search_query=%s&page=%d", q, page) + url := fmt.Sprintf("https://www.youtube.com/results?search_query=%s&page=%d&hl=%s", q, page, lang) res, err := http.DefaultClient.Get(url) if err != nil { return "", err @@ -150,12 +153,14 @@ func request(query string, page int) (string, error) { startData := body[idx:] idx = bytes.Index(startData, []byte(";\n")) startData = startData[:idx] - //fmt.Println(string(startData)) return string(startData), nil } -func Search(query string, page int) ([]Item, error) { - data, err := request(query, page) +func Search(query string, page int, lang string) ([]Item, error) { + if lang == "" { + lang = "en" + } + data, err := request(query, page, lang) if err != nil { return nil, err } -- cgit v1.2.3