From 35ab82393cd10c2a46a4f16c3b0229bea69f2187 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 27 Mar 2024 21:30:19 +0100 Subject: [PATCH] Init --- go.mod | 9 ++ go.sum | 40 +++++ main.go | 145 ++++++++++++++++++ static/search.html | 24 +++ static/searchicon.png | Bin 0 -> 1067 bytes static/style.css | 341 +++++++++++++++++++++++++++++++++++++++++ templates/results.html | 38 +++++ 7 files changed, 597 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 static/search.html create mode 100644 static/searchicon.png create mode 100644 static/style.css create mode 100644 templates/results.html diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3e53ea7 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f988942 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..f7053c8 --- /dev/null +++ b/main.go @@ -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 + } +} diff --git a/static/search.html b/static/search.html new file mode 100644 index 0000000..235ce31 --- /dev/null +++ b/static/search.html @@ -0,0 +1,24 @@ + + + + + + + + TailsX + + + + +
+

TailsGo

+ + +
+ + +
+
+ + + \ No newline at end of file diff --git a/static/searchicon.png b/static/searchicon.png new file mode 100644 index 0000000000000000000000000000000000000000..c751913d76aecc2eaedbfcbb86986a65e022ecfc GIT binary patch literal 1067 zcmV+`1l0S9P)pF8FWQhbW?9;ba!ELWdL_~cP?peYja~^aAhuUa%Y?FJQ@H11Gq^< zK~!jg?U+Ao8+8=NKkt(GQaha~Nc<@bW$4l(2DUCpD0Z_@2RgQ@R4fERY3#CD9%}~_ zgrX1$DI*XIRMOgXVCV>yfGGo2MWIVliLL3?=i++|IUkOFcJ3To7bJhD-|zk2yWjhq z@BRK*RF(g;jHnYhs73%$YXlIrMgUQ31Q4}G08wjvm=7=9W-`80n-!6>3a5Z$?$EZt zZ>oC3nwnhCpP|<895SrKT&Y?=4|)yd`F&g;2y%5H8T-1Qc{%_}6?+l*5SZu^^Az|j zn>4e2gXJ<~_Y-zel=(1;sPO6W$7U{_rV%F2uLjePn7vTM$%B6ltN?R8AfBSE_zje*_493SzWl^8FXq$i?DdRo z0-J?0%Za$Xrf|+3xUyKS-&vTBtp^1dSPc|zGa0w+yDi^*Ih!=!0rs0fN;mB?!rN~D zFDqfb8nmznwv5JiYO~Gc6}BwPyvY8^1E@OY1>m9EfANS_O9!2IU;rX=)@!RQb$&M8 zm7C!aB1@h@Nd=7^7=Xg*X4_FJVdEYCi!qSXy?k3W$es9^W}_yt`(cB}#$$gp+X~MH zjT_2QA`wHv#yGY9av3B*vj}O}^_Rj1>-e6S?QVYU>evU0x9k%*%%I{AC+c9Zk$cPh zeUBM_Rd^Xd3{t>m&?ErFA?5vUsy_#f>j01jKLE$Ou7CAgta$AEpow(s z#Kl|o38NvipouOaj}6gp=BLfO?IVko`n!sL{Uuw8uc0Oc}c?y2(1 z0Ui?=VrUnd#cF+4RZGANJrjU?K*l|hUTzr*1Ti!K*gc^hv1)0Qlpv>o=XP^u3-l*d zBacN$IBjIl}v%MfuYD#k`!)tO?cCIqZ{7U5F-b$i**xt~jYQ1!h02T*5CD9LQfthPG!bEyxio|pds z(rnVqAbjEW9mV*&-N_GNh26JzP<6fhhmdBc%?wIX)p#S9nz+;MaM)I%&^nAo97-dA ls5JtJS|fm{H3EoQ{{n>nU!|S>?(YBq002ovPDHLkV1m0=`2hd` literal 0 HcmV?d00001 diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..a7d6c29 --- /dev/null +++ b/static/style.css @@ -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%; + } +} \ No newline at end of file diff --git a/templates/results.html b/templates/results.html new file mode 100644 index 0000000..dfcbc68 --- /dev/null +++ b/templates/results.html @@ -0,0 +1,38 @@ + + + + + + + + {{ .Title }} + + + + +
+

TailsGo

+ + +
+ +
+
+

{{ .Fetched }}

+ {{ if ne .KnoRdesc "" }} +

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

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

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

+ {{ end }} + {{ if .Results }} + {{ range .Results }} +
+ {{ . }}

{{ . }}

+

{{ . }}

+
+ {{ end }} + {{ end }} + + +