added safe search
This commit is contained in:
parent
7bf2b8e609
commit
dfc3600664
3 changed files with 124 additions and 52 deletions
84
main.go
84
main.go
|
@ -42,14 +42,53 @@ func main() {
|
|||
}
|
||||
|
||||
func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
var query, safe, lang string
|
||||
|
||||
// Differentiate between GET and POST requests to correctly extract query, safe, and lang.
|
||||
if r.Method == "GET" {
|
||||
// Serve the search page if no query is provided for a GET request
|
||||
// Or extract the query parameters directly from the URL if present
|
||||
query = r.URL.Query().Get("q")
|
||||
if query == "" {
|
||||
http.ServeFile(w, r, "static/search.html")
|
||||
return
|
||||
}
|
||||
safe = r.URL.Query().Get("safe")
|
||||
lang = r.URL.Query().Get("lang")
|
||||
} else if r.Method == "POST" {
|
||||
// For a POST request, extract form values
|
||||
query = r.FormValue("q")
|
||||
safe = r.FormValue("safe")
|
||||
lang = r.FormValue("lang")
|
||||
}
|
||||
|
||||
// Early return if query is empty
|
||||
if query == "" {
|
||||
http.ServeFile(w, r, "static/search.html")
|
||||
return
|
||||
}
|
||||
|
||||
query := r.FormValue("q")
|
||||
if query == "" {
|
||||
http.ServeFile(w, r, "static/search.html")
|
||||
// Time
|
||||
start := time.Now()
|
||||
|
||||
// Adjust the search URL based on safe search and language settings
|
||||
var safeParam string
|
||||
if safe == "active" {
|
||||
safeParam = "&safe=active"
|
||||
} else {
|
||||
safeParam = "&safe=off"
|
||||
}
|
||||
|
||||
var langParam string
|
||||
if lang != "" {
|
||||
langParam = "&lr=" + lang
|
||||
}
|
||||
|
||||
searchURL := "https://www.google.com/search?q=" + url.QueryEscape(query) + safeParam + langParam
|
||||
print(searchURL+"\n")
|
||||
req, err := http.NewRequest("GET", searchURL, nil)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to create request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -65,16 +104,6 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
|||
randIndex := rand.Intn(len(userAgents))
|
||||
userAgent := userAgents[randIndex]
|
||||
|
||||
// Time
|
||||
start := time.Now()
|
||||
|
||||
// Search query
|
||||
searchURL := "https://www.google.com/search?q=" + url.QueryEscape(query)
|
||||
req, err := http.NewRequest("GET", searchURL, nil)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to create request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
|
@ -95,12 +124,9 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
|||
doc.Find(".yuRUbf").Each(func(i int, s *goquery.Selection) {
|
||||
link := s.Find("a")
|
||||
href, _ := link.Attr("href")
|
||||
// Extract header/title
|
||||
header := link.Find("h3").Text()
|
||||
// Remove the unwanted text from the header/title
|
||||
header = strings.TrimSpace(strings.TrimSuffix(header, "›"))
|
||||
|
||||
// Retrieve corresponding description
|
||||
descSelection := doc.Find(".VwiC3b").Eq(i)
|
||||
description := ""
|
||||
if descSelection.Length() > 0 {
|
||||
|
@ -114,44 +140,18 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
|||
})
|
||||
})
|
||||
|
||||
// Retrieve kno-rdesc
|
||||
kno := ""
|
||||
knoLink := ""
|
||||
rdesc := doc.Find(".kno-rdesc")
|
||||
if rdesc.Length() > 0 {
|
||||
span := rdesc.Find("span")
|
||||
kno = span.Text()
|
||||
descLink := rdesc.Find("a")
|
||||
knoLink, _ = descLink.Attr("href")
|
||||
}
|
||||
|
||||
// Retrieve featured snippet
|
||||
snip := ""
|
||||
snipSpan := doc.Find(".hgKElc")
|
||||
if snipSpan.Length() > 0 {
|
||||
snip = snipSpan.Text()
|
||||
}
|
||||
|
||||
elapsed := time.Since(start)
|
||||
|
||||
// Prepare data for rendering template
|
||||
data := struct {
|
||||
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),
|
||||
}
|
||||
|
||||
|
|
|
@ -1336,6 +1336,69 @@ p {
|
|||
letter-spacing: normal;
|
||||
}
|
||||
|
||||
/* Variables for light theme */
|
||||
:root {
|
||||
--background-color: #ffffff;
|
||||
--text-color: #000000;
|
||||
--highlight: #007bff;
|
||||
--border-color: #dddddd;
|
||||
--search-bg: #f1f3f4;
|
||||
--search-bg-input: #ffffff;
|
||||
--search-bg-input-border: #dfe1e5;
|
||||
--button: #f8f9fa;
|
||||
--link: #1a0dab;
|
||||
--fg: #202124;
|
||||
--html-bg: #ffffff;
|
||||
--snip-border: #dfe1e5;
|
||||
--snip-background: #ffffff;
|
||||
--image-view: #ffffff;
|
||||
--image-view-titlebar: #f1f3f4;
|
||||
--search-button: #5f6368;
|
||||
--image-select: #e8f0fe;
|
||||
--view-image-color: #f8f9fa;
|
||||
--footer-bg: #f2f2f2;
|
||||
--footer-font: #70757a;
|
||||
--border: #e0e0e0;
|
||||
--link-visited: #660099;
|
||||
--publish-info: #70757a;
|
||||
--green: #3c802c;
|
||||
}
|
||||
|
||||
/* Styles for dark theme */
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--background-color: #202124;
|
||||
--text-color: #e8eaed;
|
||||
--highlight: #8ab4f8;
|
||||
--border-color: #5f6368;
|
||||
--search-bg: #303134;
|
||||
--search-bg-input: #202124;
|
||||
--search-bg-input-border: #5f6368;
|
||||
--button: #3c4043;
|
||||
--link: #8ab4f8;
|
||||
--fg: #e8eaed;
|
||||
--html-bg: #202124;
|
||||
--snip-border: #5f6368;
|
||||
--snip-background: #303134;
|
||||
--image-view: #202124;
|
||||
--image-view-titlebar: #303134;
|
||||
--search-button: #e8eaed;
|
||||
--image-select: #5f6368;
|
||||
--view-image-color: #202124;
|
||||
--footer-bg: #303134;
|
||||
--footer-font: #e8eaed;
|
||||
--border: #5f6368;
|
||||
--link-visited: #c79ff;
|
||||
--publish-info: #e8eaed;
|
||||
--green: #8ab4f8;
|
||||
}
|
||||
}
|
||||
|
||||
body, h1, p, a, input, button {
|
||||
color: var(--text-color); /* Applies the text color based on theme */
|
||||
background-color: var(--background-color); /* Applies the background color based on theme */
|
||||
}
|
||||
|
||||
@media only screen and (max-width: 1320px) {
|
||||
|
||||
.snip {
|
||||
|
|
|
@ -7,20 +7,29 @@
|
|||
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="search-container">
|
||||
<form action="/search" method="post" autocomplete="off">
|
||||
<h1>TailsGo</h1>
|
||||
<div class="wrapper">
|
||||
<div class="header">
|
||||
<h1>TailsGo</h1>
|
||||
<form action="/search" method="post" class="search-form" autocomplete="off">
|
||||
<div class="search-bar">
|
||||
<input type="text" name="q" value="{{ .Query }}" autofocus id="search-input" placeholder="Type to search..." />
|
||||
<button id="search-wrapper-ico" class="material-icons-round" name="t" type="submit">search</button>
|
||||
<!-- Include clear search icon if functionality is supported -->
|
||||
</div>
|
||||
<div class="search-button-wrapper">
|
||||
<!-- Additional search options buttons if needed -->
|
||||
<button type="submit" id="search-btn">Search</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<form class="results_settings" action="/search" method="get">
|
||||
<input type="hidden" name="q" value="{{ .Query }}">
|
||||
<select class="results-settings" name="safe" id="safeSearchSelect">
|
||||
<option value="active">Safe Search On</option>
|
||||
<option value="">Safe Search Off</option>
|
||||
</select>
|
||||
<select class="results-settings" name="lang" id="languageSelect">
|
||||
<option value="">Any Language</option>
|
||||
<!-- Populate with actual language options as needed -->
|
||||
</select>
|
||||
<button class="results-save" type="submit">Apply settings</button>
|
||||
</form>
|
||||
<div class="results">
|
||||
<!-- Results go here -->
|
||||
{{range .Results}}
|
||||
<div class="result_item">
|
||||
<a href="{{.URL}}"><h3>{{.Header}}</h3></a>
|
||||
|
@ -28,6 +37,6 @@
|
|||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
<!-- Additional content and structured elements go here -->
|
||||
<!-- Additional features and formatting based on inspiration.html -->
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Add table
Reference in a new issue