Skip to content

Commit 46425a2

Browse files
STOYAN KIROVctoyan
authored andcommitted
Add logging and small fixes
1 parent 4ae4641 commit 46425a2

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

main.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func main() {
3131
estimateTime := flag.Bool("time", false, "Show how much time it would take to make all requests for the current query")
3232
printUrls := flag.Bool("print-urls", false, "Print to stdout only a list of historic URLs, which you can request later")
3333
unique := flag.Bool("unique", false, "Print to stdout only unique reponses")
34-
output := flag.String("output", "", "Path to a folder where the tool will safe all unique responses in uniquely named files per response (meg style output)")
34+
output := flag.String("output", "", "Path to a folder where the tool will safe all unique responses in uniquely named files per response")
35+
logFile := flag.String("log-file", "", "Log every wayback history request url, but not the response")
3536

3637
flag.Parse()
3738

@@ -63,6 +64,8 @@ func main() {
6364
requestUrl += fmt.Sprintf("&filter=%v", *filter)
6465
}
6566

67+
addToLog("Main request url: "+requestUrl, *logFile)
68+
6669
historyItems := getHistoryItems(requestUrl)
6770

6871
if *estimateTime {
@@ -91,11 +94,20 @@ func main() {
9194

9295
wg.Add(1)
9396
go func() {
94-
response := get(historyUrl)
97+
addToLog("Hisotry item request url: "+historyUrl, *logFile)
98+
99+
response, err := get(historyUrl)
100+
if err != nil {
101+
fmt.Printf("error making history item request: %v", err)
102+
addToLog(fmt.Sprintf("ERROR fetching %v: %v", historyUrl, err), *logFile)
103+
}
104+
95105
if !*printUrls && !*unique && *output == "" {
96106
fmt.Println(string(response))
97107
}
108+
98109
historicResponses = append(historicResponses, response)
110+
99111
wg.Done()
100112
}()
101113
}
@@ -137,26 +149,44 @@ func main() {
137149
}
138150
}
139151

140-
func get(url string) []byte {
152+
func addToLog(logRow string, logFile string) {
153+
if logFile != "" {
154+
f, err := os.OpenFile(logFile,
155+
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
156+
if err != nil {
157+
log.Fatalf("error writing to log file: %v", err)
158+
}
159+
defer f.Close()
160+
if _, err := f.WriteString(logRow + "\n"); err != nil {
161+
log.Fatalf("error writing to log file: %v", err)
162+
}
163+
}
164+
}
165+
166+
func get(url string) (body []byte, err error) {
141167
resp, err := http.Get(url)
142168
if err != nil {
143-
log.Fatalf("error making get request: %v", err)
169+
return nil, err
144170
}
171+
145172
defer resp.Body.Close()
146173

147-
body, err := ioutil.ReadAll(resp.Body)
174+
body, err = ioutil.ReadAll(resp.Body)
148175
if err != nil {
149-
log.Fatalf("error reading response body: %v", err)
176+
return nil, err
150177
}
151178

152-
return body
179+
return body, nil
153180
}
154181

155182
func getHistoryItems(requestUrl string) []HistoryItem {
156-
body := get(requestUrl)
183+
body, err := get(requestUrl)
184+
if err != nil {
185+
log.Fatalf("error getting history items: %v", err)
186+
}
157187

158188
var timestamps2d [][]string
159-
err := json.Unmarshal(body, &timestamps2d)
189+
err = json.Unmarshal(body, &timestamps2d)
160190
if err != nil {
161191
log.Fatalf("error parsing timestamps: %v", err)
162192
}

0 commit comments

Comments
 (0)