This commit is contained in:
admin 2024-03-27 21:30:19 +01:00
parent 19a9d07e61
commit 35ab82393c
7 changed files with 597 additions and 0 deletions

9
go.mod Normal file
View file

@ -0,0 +1,9 @@
module searchengine
go 1.18
require (
github.com/PuerkitoBio/goquery v1.9.1 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
golang.org/x/net v0.21.0 // indirect
)

40
go.sum Normal file
View file

@ -0,0 +1,40 @@
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

145
main.go Normal file
View file

@ -0,0 +1,145 @@
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"html/template"
"github.com/PuerkitoBio/goquery"
)
var funcs = template.FuncMap{
"title": func(s string) string {
return strings.Title(s)
},
"url_for": func(filename string) string {
return "/" + filename
},
}
var templates = template.Must(template.New("").Funcs(funcs).ParseFiles("templates/results.html"))
func main() {
http.HandleFunc("/", handleSearch)
http.HandleFunc("/search", handleSearch)
fmt.Println("Server is listening on port 5000...")
log.Fatal(http.ListenAndServe(":5000", nil))
}
func handleSearch(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
http.ServeFile(w, r, "static/search.html")
return
}
query := r.FormValue("q")
if query == "" {
http.ServeFile(w, r, "static/search.html")
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]
// 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 {
http.Error(w, "Failed to fetch search results", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// Parse HTML
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
http.Error(w, "Failed to parse HTML", http.StatusInternalServerError)
return
}
// Retrieve links
var results [][]string
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})
})
// Retrieve description
var descriptions []string
doc.Find(".VwiC3b").Each(func(i int, s *goquery.Selection) {
desc := s.Text()
descriptions = append(descriptions, desc)
})
// 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)
// Render template
data := struct {
Results [][]string
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),
}
err = templates.ExecuteTemplate(w, "results.html", data)
if err != nil {
http.Error(w, "Failed to render template", http.StatusInternalServerError)
return
}
}

24
static/search.html Normal file
View file

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TailsX</title>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body>
<form action="/search" class="search-container" method="post" autocomplete="off">
<h1>Tails<span class="X">Go</span></h1>
<input type="text" name="q" autofocus />
<input type="submit" class="hide" />
<div class="search-button-wrapper">
<button name="t" type="submit">Search with TailsGo</button>
<button name="t" type="submit">Search images with TailsGo</button>
</div>
</form>
</body>
</html>

BIN
static/searchicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

341
static/style.css Normal file
View file

@ -0,0 +1,341 @@
body {
margin: 0;
}
html {
background-color: #1c1c1c;
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
}
.hide {
display: none;
}
input,
button {
outline: none;
}
.search-container {
text-align: center;
margin-top: 10%;
}
.search-container h1 {
font-size: 70px;
color: #F1F3F4;
}
.X {
color: #8ab4f8;
}
.search-container input {
width: 500px;
color: #F1F3F4;
background-color: #333333;
padding: 10px;
font-size: inherit;
font-family: sans-serif;
border: 1px solid #3C4043;
border-radius: 25px;
}
h3 {
color: #8ab4f8;
text-decoration: none;
}
h3:hover {
text-decoration: underline;
}
p {
color: #F1F3F4;
}
.results-search-container {
background-color: #161616;
width: 100%;
border-bottom: 1px solid #303134;
margin-bottom: 24px;
}
.results-search-container input {
width: 580px;
color: #fff;
position: relative;
left: 170px;
background-color: #333333;
padding: 10px;
font-size: inherit;
font-family: sans-serif;
border: 1px solid #3C4043;
border-radius: 25px;
margin-top: 14px;
margin-bottom: 16px;
}
.results {
position: relative;
left: 175px;
margin-top: 18px;
max-width: 581px;
word-wrap: break-word;
margin-bottom: 35px;
}
.search-button-wrapper button:hover {
border: 1px solid #5f6368;
cursor: pointer;
}
.search-button-wrapper button {
color: #F1F3F4;
background-color: #333333;
font-size: 14px;
border: 1px solid #333333;
border-radius: 4px;
padding: 13px 10px 13px 10px;
margin: 30px 60px 0px 60px;
}
.no-decoration {
text-decoration: none;
color: #F1F3F4;
}
.no-decoration:hover {
text-decoration: none;
}
.logomobile {
position: absolute;
margin-top: 0px;
top: 20px;
left: 38px;
}
.sub-search-button-wrapper button {
border: none;
background-color: inherit;
font-size: 15px;
margin-right: 14px;
color: #8ab4f8;
}
.sub-search-button-wrapper img {
vertical-align: middle;
margin-right: 5px;
width: 20px;
}
.sub-search-button-wrapper {
margin-left: 175px;
margin-bottom: 10px;
}
:root {
color-scheme: dark;
}
.fetched {
color: #999da2;
left: 175px;
position: relative;
font-size: 18px;
max-width: 580px;
}
.results a:visited h3 {
color: #bd93f9;
}
.results h3 {
font-size: 20px;
margin-top: 4px;
}
.results a,
.results a:hover,
.results a:visited {
text-decoration: none;
color: #999da2;
font-size: 14px;
}
.snip {
padding: 10px;
border: opacity 0;
width: 500px;
border-radius: 8px;
background: #282828;
color: #F1F3F4;
margin-left: 840px;
margin-top: 0px;
position: absolute;
font-size: 18px;
}
.snip a {
display: flex;
margin-top: 10px;
font-size: 14px;
color: #8ab4f8;
text-decoration: none;
}
.snip a:hover {
text-decoration: underline;
}
.snipp {
padding: 10px;
border-bottom: 1px solid #3C4043;
width: 500px;
background: inherit;
color: #F1F3F4;
margin-left: 170px;
margin-top: 0px;
position: relative;
font-size: 18px;
float: none;
}
.snipp a {
display: flex;
margin-top: 10px;
font-size: 14px;
color: #8ab4f8;
text-decoration: none;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 500px;
}
.snipp a:hover {
text-decoration: underline;
}
@media only screen and (max-width: 1320px) {
.snip {
position: relative;
float: none;
margin-left: 170px;
}
}
@media only screen and (max-width: 750px) {
.snip {
position: relative;
float: none;
max-width: 90%;
margin-left: 10px;
width: auto;
font-size: 16px;
}
.snipp {
position: relative;
float: none;
max-width: 90%;
margin-left: 10px;
width: auto;
font-size: 16px;
}
p {
font-size: 14px;
}
html {
font-size: 14px;
}
h3 {
font-size: 18px;
}
.results-search-container {
margin-left: auto;
margin-right: auto;
text-align: center;
}
.results-search-container input {
width: 80%;
position: relative;
left: 0px;
margin-top: 0px;
height: 20px;
}
.logomobile {
position: relative;
float: none;
margin-top: 0px;
margin-bottom: 0px;
margin-left: auto;
margin-right: auto;
padding: 10px;
font-size: 28px;
display: block;
margin-top: 0px;
top: 0px;
left: 0px;
}
.sub-search-button-wrapper {
margin: 0;
padding: 0;
display: flex;
align-items: baseline;
margin-bottom: 10px;
}
.sub-search-button-wrapper img {
margin: 0;
padding: 0;
}
.sub-search-button-wrapper button {
margin-left: 30px;
margin-right: auto;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
}
.results {
left: 20px;
max-width: 90%;
}
.fetched {
left: 20px;
max-width: 90%;
font-size: 16px;
}
.search-container h1 {
font-size: 55px;
}
.search-container {
margin-top: 10%;
text-align: center;
}
.search-container input {
width: 80%;
}
.search-button-wrapper button {
display: table-row;
margin: 30px 0px 0px 0px;
width: 80%;
}
}

38
templates/results.html Normal file
View file

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
</head>
<body>
<form action="/search" class="results-search-container" method="post" autocomplete="off">
<h1 class="logomobile"><a class="no-decoration" href="./">Tails<span class="X">Go</span></a></h1>
<input type="text" name="q" value="{{ .Query }}" />
<input type="submit" class="hide" />
<div class="sub-search-button-wrapper">
<button name="t"><img src="/static/searchicon.png" alt="text result" />General</button>
</div>
</form>
<p class="fetched">{{ .Fetched }}</p>
{{ if ne .KnoRdesc "" }}
<p class="snip">{{ .KnoRdesc }} <a target="_blank" href="{{ .RdescLink }}">{{ .RdescLink }}</a></p>
{{ end }}
{{ if ne .Snippet "" }}
<p class="snipp">{{ .Snippet }} <a target="_blank" href="{{ index .Results 0 0 }}">{{ index .Results 0 0 }}</a></p>
{{ end }}
{{ if .Results }}
{{ range .Results }}
<div class="results">
<a href="{{ . }}">{{ . }}<h3>{{ . }}</h3></a>
<p>{{ . }}</p>
</div>
{{ end }}
{{ end }}
</body>
</html>