From 9fbc656c6cd2ad610986a265c6b346bc234bb881 Mon Sep 17 00:00:00 2001 From: ale Date: Mon, 29 Jun 2015 10:07:40 +0100 Subject: improve queue code; golint fixes The queuing code now performs proper lease accounting, and it will not return a URL twice if the page load is slow. --- scope.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'scope.go') diff --git a/scope.go b/scope.go index ccba5f5..6a63018 100644 --- a/scope.go +++ b/scope.go @@ -7,7 +7,9 @@ import ( "strings" ) +// Scope defines the crawling scope. type Scope interface { + // Check a URL to see if it's in scope for crawling. Check(*url.URL, int) bool } @@ -48,14 +50,16 @@ func NewSchemeScope(schemes []string) Scope { // eventual "www." prefix. type URLPrefixMap map[string]struct{} -func normalizeUrlPrefix(uri *url.URL) string { +func normalizeURLPrefix(uri *url.URL) string { return strings.TrimPrefix(uri.Host, "www.") + strings.TrimSuffix(uri.Path, "/") } +// Add an URL to the prefix map. func (m URLPrefixMap) Add(uri *url.URL) { - m[normalizeUrlPrefix(uri)] = struct{}{} + m[normalizeURLPrefix(uri)] = struct{}{} } +// Contains returns true if the given URL matches the prefix map. func (m URLPrefixMap) Contains(uri *url.URL) bool { s := strings.TrimPrefix(uri.Host, "www.") if _, ok := m[s]; ok { @@ -111,6 +115,8 @@ func (s *regexpIgnoreScope) Check(uri *url.URL, depth int) bool { return true } +// NewRegexpIgnoreScope returns a Scope that filters out URLs +// according to a list of regular expressions. func NewRegexpIgnoreScope(ignores []string) Scope { if ignores == nil { ignores = defaultIgnorePatterns -- cgit v1.2.3-54-g00ecf