From 17643950b1a8c7fb602d5cf0054dbdf74c5bd4c6 Mon Sep 17 00:00:00 2001 From: dez Date: Thu, 28 Mar 2024 11:52:16 +0100 Subject: [PATCH] typo fix lol --- go.mod | 2 +- imageresults.go | 71 +++++++++++++++++++++++++++++++++++++++++ main.go | 72 ++++++++++++++++++++++++++---------------- templates/results.html | 57 ++++++++++----------------------- 4 files changed, 132 insertions(+), 70 deletions(-) create mode 100644 imageresults.go diff --git a/go.mod b/go.mod index 3e53ea7..366b3d4 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module searchengine -go 1.18 +go 1.21.6 require ( github.com/PuerkitoBio/goquery v1.9.1 // indirect diff --git a/imageresults.go b/imageresults.go new file mode 100644 index 0000000..88fcaa6 --- /dev/null +++ b/imageresults.go @@ -0,0 +1,71 @@ +package main + +import ( + "encoding/json" + "fmt" + "math/rand" + "net/http" + "net/url" + "time" +) + +var userAgents := []string{ + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15", +} + +type ImageResult struct { + ThumbnailURL string `json:"thumb_proxy"` + Source string `json:"source"` +} + +func FetchImageResults(query string) ([]ImageResult, error) { + var results []ImageResult + + // Random user agent + randIndex := rand.Intn(len(userAgents)) + userAgent := userAgents[randIndex] + + client := &http.Client{} + reqURL := fmt.Sprintf("https://api.qwant.com/v3/search/images?t=images&q=%s&count=50&locale=en_CA&offset=1&device=desktop&tgp=2&safesearch=1", url.QueryEscape(query)) + req, err := http.NewRequest("GET", reqURL, nil) + if err != nil { + return nil, err + } + req.Header.Set("User-Agent", userAgent) + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var respData struct { + Data struct { + Result struct { + Items []struct { + Thumbnail string `json:"thumbnail"` + URL string `json:"url"` + } `json:"items"` + } `json:"result"` + } `json:"data"` + } + + if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil { + return nil, err + } + + for _, item := range respData.Data.Result.Items { + // Process each image result here + results = append(results, ImageResult{ + ThumbnailURL: "/img_proxy?url=" + url.QueryEscape(item.Thumbnail), + Source: url.QueryEscape(urlparse(item.URL).Host), // Ensure you have a urlparse equivalent in Go or process URL differently + }) + } + + return results, nil +} diff --git a/main.go b/main.go index 16c5a77..5898a6c 100644 --- a/main.go +++ b/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) diff --git a/templates/results.html b/templates/results.html index 66afe71..54fcbdf 100644 --- a/templates/results.html +++ b/templates/results.html @@ -1,12 +1,10 @@ - - {{ .Title }} - + {{.Query}} - Search Results -

TailsGo

@@ -113,21 +90,19 @@
-

{{ .Fetched }}

- {{ if ne .KnoRdesc "" }} -

{{ .KnoRdesc }} {{ .RdescLink }}

- {{ end }} - {{ if ne .Snippet "" }} -

{{ .Snippet }} {{ index .Results 0 0 }}

- {{ end }} - {{ if .Results }} - {{ range .Results }} -
- {{ . }}

{{ . }}

-

{{ . }}

-
- {{ end }} - {{ end }} + + + {{range .Results}} +
+ {{.Header}}
+ {{.Description}} +
+
+ {{end}} + -