typo fix lol
This commit is contained in:
parent
84a88d6fe6
commit
96dae70aa9
4 changed files with 132 additions and 70 deletions
72
main.go
72
main.go
|
@ -10,10 +10,16 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
"html/template"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
// Define a struct to represent search result
|
||||
type SearchResult struct {
|
||||
URL string
|
||||
Header string
|
||||
Description string
|
||||
}
|
||||
|
||||
var funcs = template.FuncMap{
|
||||
"title": func(s string) string {
|
||||
return strings.Title(s)
|
||||
|
@ -81,20 +87,28 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
// Retrieve links
|
||||
var results [][]string
|
||||
// Retrieve links, headers, and descriptions
|
||||
var results []SearchResult
|
||||
doc.Find(".yuRUbf").Each(func(i int, s *goquery.Selection) {
|
||||
link := s.Find("a")
|
||||
href, _ := link.Attr("href")
|
||||
title := link.Text()
|
||||
results = append(results, []string{href, title})
|
||||
})
|
||||
// Extract header/title
|
||||
header := link.Find("h3").Text()
|
||||
// Remove the unwanted text from the header/title
|
||||
header = strings.TrimSpace(strings.TrimSuffix(header, "›"))
|
||||
|
||||
// Retrieve description
|
||||
var descriptions []string
|
||||
doc.Find(".VwiC3b").Each(func(i int, s *goquery.Selection) {
|
||||
desc := s.Text()
|
||||
descriptions = append(descriptions, desc)
|
||||
// Retrieve corresponding description
|
||||
descSelection := doc.Find(".VwiC3b").Eq(i)
|
||||
description := ""
|
||||
if descSelection.Length() > 0 {
|
||||
description = descSelection.Text()
|
||||
}
|
||||
|
||||
results = append(results, SearchResult{
|
||||
URL: href,
|
||||
Header: header,
|
||||
Description: description,
|
||||
})
|
||||
})
|
||||
|
||||
// Retrieve kno-rdesc
|
||||
|
@ -117,26 +131,28 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
elapsed := time.Since(start)
|
||||
|
||||
// Render template
|
||||
// Prepare data for rendering template
|
||||
data := struct {
|
||||
Results [][]string
|
||||
Title string
|
||||
Query string
|
||||
Fetched string
|
||||
Snippet string
|
||||
KnoRdesc string
|
||||
RdescLink string
|
||||
ElapsedTime string
|
||||
Results []SearchResult
|
||||
Title string
|
||||
Query string
|
||||
Fetched string
|
||||
Snippet string
|
||||
KnoRdesc string
|
||||
RdescLink string
|
||||
ElapsedTime string
|
||||
}{
|
||||
Results: results,
|
||||
Title: query + " - TailsGo",
|
||||
Query: query,
|
||||
Fetched: fmt.Sprintf("Fetched the results in %.2f seconds", elapsed.Seconds()),
|
||||
Snippet: snip,
|
||||
KnoRdesc: kno,
|
||||
RdescLink: knoLink,
|
||||
ElapsedTime: strconv.FormatFloat(elapsed.Seconds(), 'f', 2, 64),
|
||||
Results: results,
|
||||
Title: query + " - TailsGo",
|
||||
Query: query,
|
||||
Fetched: fmt.Sprintf("Fetched the results in %.2f seconds", elapsed.Seconds()),
|
||||
Snippet: snip,
|
||||
KnoRdesc: kno,
|
||||
RdescLink: knoLink,
|
||||
ElapsedTime: strconv.FormatFloat(elapsed.Seconds(), 'f', 2, 64),
|
||||
}
|
||||
|
||||
// Render template
|
||||
err = templates.ExecuteTemplate(w, "results.html", data)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to render template", http.StatusInternalServerError)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue