links are now uniq'd before marshaling

* crawled is now a map to httpItem's (instead of struct{}) so we can connect pages mid-crawl
* added duplicate link to html site, test updated to match
* now any httpItem with linkType == tUnknown is listed as "Broken" in the output
This commit is contained in:
swaits committed 2015-02-16 22:47:56 -08:00
1 parent 7e18005473
commit 2c2cfc126c
4 files changed
+21 -11

No files matched your search

+13 -8
View File
@@ -10,7 +10,7 @@ import (
// docrawl begins crawling the site at "homeurl" // docrawl begins crawling the site at "homeurl"
func docrawl(homeurl string) itemSlice { func docrawl(homeurl string) itemSlice {
// set of what we have already crawled // set of what we have already crawled
crawled := make(map[string]struct{}) crawled := make(itemMap)
// map (of urlString -> httpItem) of our results // map (of urlString -> httpItem) of our results
results := make(itemMap) results := make(itemMap)
@@ -31,7 +31,7 @@ func docrawl(homeurl string) itemSlice {
crawlingCount := 1 crawlingCount := 1
// start the home page crawl // start the home page crawl
crawled[homeitem.url.String()] = struct{}{} crawled[homeitem.url.String()] = homeitem
go crawlItem(homeitem, rchan) go crawlItem(homeitem, rchan)
// wait for results // wait for results
@@ -46,19 +46,24 @@ func docrawl(homeurl string) itemSlice {
// start crawly any new child pages we haven't yet crawled // start crawly any new child pages we haven't yet crawled
for i, c := range r.children { for i, c := range r.children {
// see if we already have a result for this page, if so, point to that result // see if we already have a result for this page
// if so, point to that result
if existing, ok := results[c.url.String()]; ok { if existing, ok := results[c.url.String()]; ok {
r.children[i] = existing r.children[i] = existing
continue continue
} }
// see if we're already crawling this page (but maybe don't have results yet) // see if we're already crawling this page (but maybe don't have results yet)
if _, ok := crawled[c.url.String()]; !ok { // if so, point to that item (we will have it as a result later!)
// haven't crawled this one yet, do so now if existing, ok := crawled[c.url.String()]; ok {
crawlingCount++ r.children[i] = existing
crawled[c.url.String()] = struct{}{} continue
go crawlItem(c, rchan)
} }
// haven't crawled this one yet, do so now
crawlingCount++
crawled[c.url.String()] = c
go crawlItem(c, rchan)
} }
case <-ticker: // our regular ticker. for status output and checking for completion. case <-ticker: // our regular ticker. for status output and checking for completion.
+2 -2
View File
@@ -102,7 +102,7 @@ func TestSimpleMap(t *testing.T) {
t.Logf("got %v, wanted %v\n", len(pages), 6) t.Logf("got %v, wanted %v\n", len(pages), 6)
t.Fatal("got wrong number of pages") t.Fatal("got wrong number of pages")
} }
if children != 8 { if children != 9 {
t.Logf("got %v, wanted %v\n", children, 8) t.Logf("got %v, wanted %v\n", children, 8)
t.Fatal("got wrong number of total children") t.Fatal("got wrong number of total children")
} }
@@ -117,7 +117,7 @@ func TestSimpleMap(t *testing.T) {
} }
// specific page number of children // specific page number of children
if len(homepage.children) != 4 { if len(homepage.children) != 5 {
t.Logf("got %v, wanted %v\n", len(homepage.children), 4) t.Logf("got %v, wanted %v\n", len(homepage.children), 4)
t.Fatal("got wrong number of links") t.Fatal("got wrong number of links")
} }
+5 -1
View File
@@ -48,9 +48,13 @@ func sitemapToLocations(pages itemSlice) []*Location {
l.Links = append(l.Links, c.url.String()) l.Links = append(l.Links, c.url.String())
} else if c.linkType == tBroken { } else if c.linkType == tBroken {
l.Broken = append(l.Broken, c.url.String()) l.Broken = append(l.Broken, c.url.String())
} else { } else if c.linkType == tAsset {
l.Assets = append(l.Assets, c.url.String()) l.Assets = append(l.Assets, c.url.String())
} else {
// unknown link here, which means it failed to crawl, let's call it "broken"
l.Broken = append(l.Broken, c.url.String())
} }
} }
// now uniq & sort the children slices // now uniq & sort the children slices
+1
View File
@@ -5,6 +5,7 @@
<body> <body>
<img src="/assets/image.png"/> <img src="/assets/image.png"/>
<a href="/about.html">fooey!</a> <a href="/about.html">fooey!</a>
<a href="/about.html">duplicate!</a>
<script src="scripts/blah.js"/> <script src="scripts/blah.js"/>
<a href="/zzzbroken.html">oops!</a> <a href="/zzzbroken.html">oops!</a>
</body> </body>