-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartpage.go
144 lines (133 loc) · 3.61 KB
/
startpage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package googleit
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
log "github.com/schollz/logger"
)
func StartPage(query string, ops ...Options) (urls []string, err error) {
if httpClient == nil {
if len(ops) > 0 {
httpClient, err = GetClient(ops[0].UseTor)
} else {
httpClient, err = GetClient(false)
}
if err != nil {
return
}
}
pageLimit := 10
mustInclude := []string{}
if len(ops) > 0 {
pageLimit = ops[0].NumPages
mustInclude = ops[0].MustInclude
if ops[0].Site != "" {
query = "site:" + ops[0].Site + " " + query
}
}
if pageLimit < 1 {
pageLimit = 10
}
currentCount := 1
urls = []string{}
escapedQuery, _ := url.QueryUnescape(query)
var code string
for i := 0; i < pageLimit; i++ {
log.Tracef("[startpage] working on page %d with code %s", i, code)
var req *http.Request
if i == 0 {
body := strings.NewReader(`query=` + escapedQuery + `&cat=web&cmd=process_search&language=english&engine0=v1all&abp=1`)
req, err = http.NewRequest("POST", "https://www.startpage.com/do/search", body)
if err != nil {
return
}
req.Header.Set("Referer", "https://www.startpage.com/")
} else {
if code == "" {
break
}
body := strings.NewReader(fmt.Sprintf("language=english&abp=1&lui=english&sc=%s&cat=web&query=%s&page=%d",
code,
escapedQuery,
i+1,
))
req, err = http.NewRequest("POST", "https://www.startpage.com/sp/search", body)
if err != nil {
return
}
req.Header.Set("Referer", "https://www.startpage.com/sp/search")
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Upgrade-Insecure-Requests", "1")
resp, err2 := httpClient.Client.Do(req)
if err2 != nil {
err = err2
return
}
var newResults []Result
newResults, code, err2 = captureStartPage(resp)
if err2 != nil {
err = err2
return
}
if len(newResults) == 0 {
break
}
for _, r := range newResults {
doesntHave := ""
for _, word := range mustInclude {
if !strings.Contains(r.Title, word) && !strings.Contains(r.URL, word) {
doesntHave = word
break
}
}
if doesntHave != "" {
log.Tracef("[startpage] skipping '%s' as it doesn't have '%s'", r.Title, doesntHave)
continue
}
urls = append(urls, r.URL)
currentCount++
}
}
urls = ListToSet(urls)
return
}
func captureStartPage(res *http.Response) (results []Result, code string, err error) {
defer res.Body.Close()
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return
}
// Find the urls
results = []Result{}
doc.Find(".w-gl__result-title").Each(func(i int, s *goquery.Selection) {
href, ok := s.Attr("href")
if !ok {
return
}
href, _ = url.QueryUnescape(href)
if strings.Contains(href, "=http") {
href = "http" + strings.Split(href, "=http")[1]
}
if !strings.Contains(href, "http") || strings.Contains(href, "bing") || strings.Contains(href, "bing.co") || strings.Contains(href, "clickserve") {
return
}
title := strings.TrimSpace(strings.ToLower(s.Text()))
results = append(results, Result{title, href})
log.Tracef("[startpage] '%s' %s", title, href)
})
doc.Find("input[name='sc']").Each(func(i int, s *goquery.Selection) {
id := s.AttrOr("name", "")
if id == "sc" {
code, _ = s.Attr("value")
}
})
return
}