-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-scraper.go
228 lines (203 loc) · 5.31 KB
/
google-scraper.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
const apiKey = "key"
const cx = "key"
const queriesPerMinute = 100
const intervalBetweenQueries = time.Minute / queriesPerMinute
// domains which will be blacklisted in results
var blacklist = []string{
"github.com",
"reddit.com",
"stackexchange.com",
"stackoverflow.com",
"quora.com",
"medium.com",
"facebook.com",
"x.com",
"twitter.com",
"linkedin.com",
"pinterest.com",
"tumblr.com",
"instagram.com",
"flickr.com",
"wikipedia.org",
"youtube.com",
"reddit.com",
"pastebin.com",
"mozilla.org",
"duckduckgo.com",
"sitepoint.com",
"codecademy.com",
"bytes.com",
"programmingforums.org",
"dev.to",
"codenewbie.org",
"slashdot.org",
"daniweb.com",
"coderanch.com",
"gamedev.net",
"replit.com",
"community.sap.com",
"community.spiceworks.com",
"techguy.org",
"techsupportforum.com",
"bleepingcomputer.com/forums",
"linustechtips.com/main",
"tomshardware.com/forum",
"hardforum.com",
"arstechnica.com/civis",
"neowin.net/forum",
"forums.anandtech.com",
"php.net",
"microsoft.com",
"vulnweb.com",
"intel.com",
}
type SearchResult struct {
Items []struct {
Link string `json:"link"`
} `json:"items"`
Queries struct {
NextPage []struct {
StartIndex int `json:"startIndex"`
} `json:"nextPage"`
} `json:"queries"`
}
func isBlacklisted(url string) bool {
for _, domain := range blacklist {
if strings.Contains(url, domain) {
return true
}
}
return false
}
func googleSearch(query, outputFile string) error {
query = strings.ReplaceAll(query, " ", "+")
startIndex := 1
file, err := os.OpenFile(outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open output file: %w", err)
}
defer file.Close()
retryCount := 0
const maxRetries = 3
const maxResults = 100
const queriesPerMinuteLimit = 100
const waitTime = time.Minute
queryCount := 0
fetchedResults := 0
queryStartTime := time.Now()
for fetchedResults < maxResults {
if queryCount >= queriesPerMinuteLimit {
timeSinceStart := time.Since(queryStartTime)
if timeSinceStart < time.Minute {
sleepTime := time.Minute - timeSinceStart
fmt.Printf("Reached query limit (%d queries/minute). Waiting %s before next query...\n", queriesPerMinuteLimit, sleepTime)
time.Sleep(sleepTime)
}
queryStartTime = time.Now()
queryCount = 0
}
url := fmt.Sprintf("https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=%s&start=%d&num=10", apiKey, cx, query, startIndex)
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("failed to fetch search results: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
retryCount = 0
} else if resp.StatusCode == http.StatusBadRequest {
retryCount++
if retryCount > maxRetries {
fmt.Printf("Max retries reached for query: %s\n", query)
break
}
fmt.Printf("Retrying query: %s (attempt %d)\n", query, retryCount)
time.Sleep(2 * time.Second)
continue
} else if resp.StatusCode == http.StatusTooManyRequests {
fmt.Println("Received 429 Too Many Requests, waiting 1 minute...")
time.Sleep(1 * time.Minute)
continue
} else {
return fmt.Errorf("unexpected response status: %s", resp.Status)
}
queryCount++
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
var result SearchResult
if err := json.Unmarshal(body, &result); err != nil {
return fmt.Errorf("failed to parse search results: %w", err)
}
if len(result.Items) == 0 {
break
}
for _, item := range result.Items {
if !isBlacklisted(item.Link) {
if _, err := file.WriteString(item.Link + "\n"); err != nil {
return fmt.Errorf("failed to write to output file: %w", err)
}
fetchedResults++
if fetchedResults >= maxResults {
fmt.Printf("Reached the maximum limit of %d results.\n", maxResults)
return nil
}
}
}
if len(result.Queries.NextPage) > 0 {
startIndex = result.Queries.NextPage[0].StartIndex
} else {
break
}
time.Sleep(intervalBetweenQueries)
}
fmt.Printf("Fetched %d results in total.\n", fetchedResults)
return nil
}
func main() {
query := flag.String("q", "", "Search query")
outputFile := flag.String("o", "output.txt", "Output file")
wordlist := flag.String("w", "", "Wordlist file with multiple search queries")
flag.Parse()
if *wordlist != "" {
file, err := os.Open(*wordlist)
if err != nil {
fmt.Printf("Error opening wordlist file: %s\n", err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
query := scanner.Text()
if err := googleSearch(query, *outputFile); err != nil {
fmt.Printf("Error processing query '%s': %s\n", query, err)
}
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading wordlist file: %s\n", err)
os.Exit(1)
}
} else if *query != "" {
if err := googleSearch(*query, *outputFile); err != nil {
fmt.Printf("Error processing query '%s': %s\n", *query, err)
os.Exit(1)
}
} else {
fmt.Println("Usage: go run search.go -q 'search-term' -o output.txt")
fmt.Println(" go run search.go -w wordlist.txt -o output.txt")
os.Exit(1)
}
fmt.Printf("Search results saved to %s\n", *outputFile)
}