-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
141 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
collyresponsible "github.com/tb0hdan/colly-responsible" | ||
) | ||
|
||
func main() { | ||
httpCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
err := collyresponsible.Crawl(httpCtx, "https://en.wikipedia.org", "Mozilla/5.0 (compatible; Colly Responsible; +https://github.com/tb0hdan/colly-responsible)") | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package collyresponsible | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type RequestLimiter struct { | ||
SleepDelay int | ||
sleepMin int | ||
lock *sync.RWMutex | ||
} | ||
|
||
func (r *RequestLimiter) Increase() { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
r.SleepDelay++ | ||
} | ||
|
||
func (r *RequestLimiter) Decrease() { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
if r.SleepDelay > r.sleepMin { | ||
r.SleepDelay-- | ||
} | ||
} | ||
|
||
func (r *RequestLimiter) Sleep() { | ||
r.lock.RLock() | ||
defer r.lock.RUnlock() | ||
time.Sleep(time.Duration(r.SleepDelay) * time.Second) | ||
} | ||
|
||
func NewLimiter(sleepDelay int) *RequestLimiter { | ||
return &RequestLimiter{ | ||
SleepDelay: sleepDelay, | ||
sleepMin: sleepDelay, | ||
lock: &sync.RWMutex{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package collyresponsible | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/temoto/robotstxt" | ||
) | ||
|
||
func GetRobots(ctx context.Context, website, userAgent string, limiter *RequestLimiter) (*robotstxt.RobotsData, error) { | ||
if strings.HasSuffix(website, "/") { | ||
website = website[:len(website)-1] | ||
} | ||
head, err := http.NewRequestWithContext(ctx, http.MethodHead, website+"/robots.txt", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
head.Header.Add("User-Agent", userAgent) | ||
// | ||
resp, err := http.DefaultClient.Do(head) | ||
if err != nil || resp.StatusCode != http.StatusOK { | ||
return nil, err | ||
} | ||
// | ||
limiter.Sleep() | ||
// | ||
get, err := http.NewRequestWithContext(ctx, http.MethodGet, website+"/robots.txt", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
get.Header.Add("User-Agent", userAgent) | ||
// | ||
getResp, err := http.DefaultClient.Do(get) | ||
if err != nil || resp.StatusCode != http.StatusOK { | ||
return nil, err | ||
} | ||
robots, err := robotstxt.FromResponse(getResp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return robots, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package collyresponsible | ||
|
||
import "sync" | ||
|
||
type VisitMap struct { | ||
visited map[string]bool | ||
lock *sync.RWMutex | ||
} | ||
|
||
func (v *VisitMap) Add(url string) { | ||
v.lock.Lock() | ||
defer v.lock.Unlock() | ||
v.visited[url] = true | ||
} | ||
|
||
func (v *VisitMap) IsVisited(url string) bool { | ||
v.lock.RLock() | ||
defer v.lock.RUnlock() | ||
return v.visited[url] | ||
} | ||
|
||
func NewVisitMap() *VisitMap { | ||
return &VisitMap{ | ||
visited: make(map[string]bool), | ||
lock: &sync.RWMutex{}, | ||
} | ||
} |