use io.Reader when parsing urls

This commit is contained in:
sm
2016-11-26 00:15:36 -08:00
parent b2df3da864
commit ccc08a5da1
3 changed files with 66 additions and 19 deletions
+7
View File
@@ -2,6 +2,8 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
@@ -20,9 +22,14 @@ func main() {
for p := range spider.Pages(os.Args[1]) {
resp, err := http.Get(p.To)
if err != nil {
p.Err = err
failures = append(failures, p)
continue
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
p.Err = fmt.Errorf("http status; got %s, want %s", http.StatusText(resp.StatusCode), http.StatusText(http.StatusOK))
failures = append(failures, p)
}
}
+10 -1
View File
@@ -2,6 +2,7 @@ package main
import (
"fmt"
"net/http"
"os"
"mcquay.me/spider"
@@ -14,7 +15,15 @@ func main() {
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
for _, l := range spider.URLs(os.Args[1]) {
resp, err := http.Get(os.Args[1])
if err != nil {
panic(err)
}
links, err := spider.URLs(resp.Body)
if err != nil {
panic(err)
}
for _, l := range links {
fmt.Println(l)
}
}