filesplit

This commit is contained in:
admin 2024-04-02 20:56:45 +02:00
parent 114a32668c
commit 123e9ce477
4 changed files with 180 additions and 197 deletions

111
main.go
View file

@ -2,24 +2,15 @@ package main
import ( import (
"fmt" "fmt"
"html/template"
"log" "log"
"math/rand"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"html/template"
"github.com/PuerkitoBio/goquery"
) )
// Define a struct to represent search result // LanguageOption represents a language option for search
type SearchResult struct {
URL string
Header string
Description string
}
type LanguageOption struct { type LanguageOption struct {
Code string Code string
Name string Name string
@ -76,20 +67,14 @@ var languageOptions = []LanguageOption{
} }
var funcs = template.FuncMap{ var funcs = template.FuncMap{
"title": func(s string) string { "title": func(s string) string { return strings.Title(s) },
return strings.Title(s) "url_for": func(filename string) string { return "/" + filename },
},
"url_for": func(filename string) string {
return "/" + filename
},
} }
var templates = template.Must(template.New("").Funcs(funcs).ParseFiles("templates/results.html")) var templates = template.Must(template.New("").Funcs(funcs).ParseFiles("templates/results.html"))
func main() { func main() {
// Serve static files from the 'static' directory
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", handleSearch) http.HandleFunc("/", handleSearch)
http.HandleFunc("/search", handleSearch) http.HandleFunc("/search", handleSearch)
fmt.Println("Server is listening on http://localhost:5000") fmt.Println("Server is listening on http://localhost:5000")
@ -99,105 +84,30 @@ func main() {
func handleSearch(w http.ResponseWriter, r *http.Request) { func handleSearch(w http.ResponseWriter, r *http.Request) {
var query, safe, lang string var query, safe, lang string
// Differentiate between GET and POST requests to correctly extract query, safe, and lang.
if r.Method == "GET" { 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") query = r.URL.Query().Get("q")
if query == "" {
http.ServeFile(w, r, "static/search.html")
return
}
safe = r.URL.Query().Get("safe") safe = r.URL.Query().Get("safe")
lang = r.URL.Query().Get("lang") lang = r.URL.Query().Get("lang")
} else if r.Method == "POST" { } else if r.Method == "POST" {
// For a POST request, extract form values
query = r.FormValue("q") query = r.FormValue("q")
safe = r.FormValue("safe") safe = r.FormValue("safe")
lang = r.FormValue("lang") lang = r.FormValue("lang")
} }
// Early return if query is empty
if query == "" { if query == "" {
http.ServeFile(w, r, "static/search.html") http.ServeFile(w, r, "static/search.html")
return return
} }
// Time start := time.Now() // This line is correctly placed
start := time.Now() results, err := PerformTextSearch(query, safe, lang)
// 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
}
// Random user agent
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",
}
randIndex := rand.Intn(len(userAgents))
userAgent := userAgents[randIndex]
req.Header.Set("User-Agent", userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
http.Error(w, "Failed to fetch search results", http.StatusInternalServerError) http.Error(w, "Failed to fetch search results", http.StatusInternalServerError)
return return
} }
defer resp.Body.Close() elapsed := time.Since(start) // Correctly captures the elapsed time after search
fetched := fmt.Sprintf("Fetched the results in %.2f seconds", elapsed.Seconds())
// Parse HTML
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
http.Error(w, "Failed to parse HTML", http.StatusInternalServerError)
return
}
// 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")
header := link.Find("h3").Text()
header = strings.TrimSpace(strings.TrimSuffix(header, ""))
descSelection := doc.Find(".VwiC3b").Eq(i)
description := ""
if descSelection.Length() > 0 {
description = descSelection.Text()
}
results = append(results, SearchResult{
URL: href,
Header: header,
Description: description,
})
})
elapsed := time.Since(start)
// Prepare data for rendering template
data := struct { data := struct {
Results []SearchResult Results []SearchResult
Query string Query string
@ -208,13 +118,12 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
}{ }{
Results: results, Results: results,
Query: query, Query: query,
Fetched: fmt.Sprintf("Fetched the results in %.2f seconds", elapsed.Seconds()), Fetched: fetched,
ElapsedTime: strconv.FormatFloat(elapsed.Seconds(), 'f', 2, 64), ElapsedTime: strconv.FormatFloat(time.Since(start).Seconds(), 'f', 2, 64),
LanguageOptions: languageOptions, LanguageOptions: languageOptions,
CurrentLang: lang, CurrentLang: lang,
} }
// Render template
err = templates.ExecuteTemplate(w, "results.html", data) err = templates.ExecuteTemplate(w, "results.html", data)
if err != nil { if err != nil {
http.Error(w, "Failed to render template", http.StatusInternalServerError) http.Error(w, "Failed to render template", http.StatusInternalServerError)

View file

@ -1388,7 +1388,7 @@ p {
--footer-bg: #303134; --footer-bg: #303134;
--footer-font: #e8eaed; --footer-font: #e8eaed;
--border: #5f6368; --border: #5f6368;
--link-visited: #c79ff; --link-visited: #c79fff;
--publish-info: #e8eaed; --publish-info: #e8eaed;
--green: #8ab4f8; --green: #8ab4f8;
} }

View file

@ -33,9 +33,11 @@
<!-- Results go here --> <!-- Results go here -->
{{range .Results}} {{range .Results}}
<div class="result_item"> <div class="result_item">
<a href="{{.URL}}">{{.URL}}</a>
<a href="{{.URL}}"><h3>{{.Header}}</h3></a> <a href="{{.URL}}"><h3>{{.Header}}</h3></a>
<p>{{.Description}}</p> <p>{{.Description}}</p>
</div> </div>
<br>
{{end}} {{end}}
</div> </div>
<!-- Additional features and formatting based on inspiration.html --> <!-- Additional features and formatting based on inspiration.html -->

72
text.go Normal file
View file

@ -0,0 +1,72 @@
package main
import (
"log"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
type SearchResult struct {
URL string
Header string
Description string
}
func PerformTextSearch(query, safe, lang string) ([]SearchResult, error) {
var results []SearchResult
client := &http.Client{}
safeParam := "&safe=off"
if safe == "active" {
safeParam = "&safe=active"
}
langParam := ""
if lang != "" {
langParam = "&lr=" + lang
}
searchURL := "https://www.google.com/search?q=" + url.QueryEscape(query) + safeParam + langParam
req, err := http.NewRequest("GET", searchURL, nil)
if err != nil {
log.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
doc.Find(".yuRUbf").Each(func(i int, s *goquery.Selection) {
link := s.Find("a")
href, _ := link.Attr("href")
header := link.Find("h3").Text()
header = strings.TrimSpace(strings.TrimSuffix(header, ""))
descSelection := doc.Find(".VwiC3b").Eq(i)
description := ""
if descSelection.Length() > 0 {
description = descSelection.Text()
}
results = append(results, SearchResult{
URL: href,
Header: header,
Description: description,
})
})
return results, nil
}