added image search (website style wip)

This commit is contained in:
partisan 2024-04-05 12:44:11 +02:00
parent 6f25a28d5c
commit 7ae56f4e21
5 changed files with 207 additions and 114 deletions

49
text.go
View file

@ -1,22 +1,25 @@
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
type SearchResult struct {
type TextSearchResult struct {
URL string
Header string
Description string
}
func PerformTextSearch(query, safe, lang string) ([]SearchResult, error) {
var results []SearchResult
func PerformTextSearch(query, safe, lang string) ([]TextSearchResult, error) {
var results []TextSearchResult
client := &http.Client{}
safeParam := "&safe=off"
@ -61,7 +64,7 @@ func PerformTextSearch(query, safe, lang string) ([]SearchResult, error) {
description = descSelection.Text()
}
results = append(results, SearchResult{
results = append(results, TextSearchResult{
URL: href,
Header: header,
Description: description,
@ -70,3 +73,41 @@ func PerformTextSearch(query, safe, lang string) ([]SearchResult, error) {
return results, nil
}
func handleTextSearch(w http.ResponseWriter, r *http.Request, query, safe, lang string) {
// Perform the text search
results, err := PerformTextSearch(query, safe, lang)
if err != nil {
log.Printf("Error performing text search: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Assuming you have a separate template for text search results
tmpl, err := template.ParseFiles("templates/results.html") // Ensure this path matches your templates' location
if err != nil {
log.Printf("Error parsing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
data := struct {
Results []TextSearchResult // Ensure this type matches the structure expected by your template
Query string
Fetched string
LanguageOptions []LanguageOption
CurrentLang string
}{
Results: results,
Query: query,
Fetched: fmt.Sprintf("%.2f seconds", time.Since(time.Now()).Seconds()), // Example fetched time, adjust as necessary
LanguageOptions: languageOptions, // Assuming this is defined globally or elsewhere
CurrentLang: lang,
}
err = tmpl.Execute(w, data)
if err != nil {
log.Printf("Error executing template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}