diff --git a/README.md b/README.md index 1073095..5fd378f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # Geziyor -Scraper and crawler framework for Golang. Geziyor uses go *channels* over *callbacks* +Geziyor is a fast web crawling and web scraping framework, used to crawl websites and extract structured data from their pages. It can be used for a wide range of purposes, from data mining to monitoring and automated testing. + +[![GoDoc](https://godoc.org/github.com/geziyor/geziyor?status.svg)](https://godoc.org/github.com/geziyor/geziyor) +[![report card](https://goreportcard.com/badge/github.com/geziyor/geziyor)](http://goreportcard.com/report/geziyor/geziyor) ## Features -- 1.000+ Requests/Sec +- 1.000+ Requests/Sec - Caching - Automatic Data Exporting @@ -35,4 +38,6 @@ geziyor.Start() ## Installation - go get github.com/geziyor/geziyor \ No newline at end of file + go get github.com/geziyor/geziyor + +We highly recommend you to use go modules. As this project is in **development stage** right now. diff --git a/geziyor.go b/geziyor.go index 3e72926..a439b05 100644 --- a/geziyor.go +++ b/geziyor.go @@ -14,6 +14,7 @@ import ( "time" ) +// Geziyor is our main scraper type type Geziyor struct { client *http.Client wg sync.WaitGroup @@ -22,6 +23,7 @@ type Geziyor struct { visitedURLS []string } +// Opt is custom options type for Geziyor type Opt struct { AllowedDomains []string StartURLs []string @@ -33,6 +35,8 @@ func init() { log.SetOutput(os.Stdout) } +// NewGeziyor creates new Geziyor with default values. +// If options provided, options func NewGeziyor(opt Opt) *Geziyor { geziyor := &Geziyor{ client: &http.Client{ @@ -48,6 +52,7 @@ func NewGeziyor(opt Opt) *Geziyor { return geziyor } +// Start starts scraping func (g *Geziyor) Start() { for _, startURL := range g.opt.StartURLs { go g.Get(startURL) @@ -57,19 +62,30 @@ func (g *Geziyor) Start() { g.wg.Wait() } -func (g *Geziyor) Get(rawURL string) { +// Get issues a GET to the specified URL. +func (g *Geziyor) Get(url string) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + log.Printf("Request creating error %v\n", err) + return + } + g.Do(req) +} + +// Do sends an HTTP request +func (g *Geziyor) Do(req *http.Request) { g.wg.Add(1) defer g.wg.Done() - if !checkURL(rawURL, g) { + if !checkURL(req.URL, g) { return } // Log - log.Println("Fetching: ", rawURL) + log.Println("Fetching: ", req.URL.String()) - // Get request - resp, err := g.client.Get(rawURL) + // Do request + resp, err := g.client.Do(req) if resp != nil { defer resp.Body.Close() } @@ -104,23 +120,17 @@ func (g *Geziyor) Get(rawURL string) { time.Sleep(time.Millisecond) } -func checkURL(rawURL string, g *Geziyor) bool { - - // Parse URL - parsedURL, err := url.Parse(rawURL) - if err != nil { - fmt.Fprintf(os.Stderr, "url parsing error: %v\n", err) - return false - } +func checkURL(parsedURL *url.URL, g *Geziyor) bool { + rawURL := parsedURL.String() // Check for allowed domains - if len(g.opt.AllowedDomains) != 0 && !Contains(g.opt.AllowedDomains, parsedURL.Host) { + if len(g.opt.AllowedDomains) != 0 && !contains(g.opt.AllowedDomains, parsedURL.Host) { log.Printf("Domain not allowed: %s\n", parsedURL.Host) return false } // Check for duplicate requests - if Contains(g.visitedURLS, rawURL) { + if contains(g.visitedURLS, rawURL) { log.Printf("URL already visited %s\n", rawURL) return false } @@ -129,8 +139,8 @@ func checkURL(rawURL string, g *Geziyor) bool { return true } -// Contains checks whether []string contains string -func Contains(s []string, e string) bool { +// contains checks whether []string contains string +func contains(s []string, e string) bool { for _, a := range s { if a == e { return true diff --git a/geziyor_test.go b/geziyor_test.go index 4087322..2d565f0 100644 --- a/geziyor_test.go +++ b/geziyor_test.go @@ -7,7 +7,16 @@ import ( "testing" ) -func TestGeziyor_StartURLs_Simple(t *testing.T) { +func TestGeziyor_Simple(t *testing.T) { + NewGeziyor(Opt{ + StartURLs: []string{"http://api.ipify.org"}, + ParseFunc: func(r *Response) { + fmt.Println(string(r.Body)) + }, + }).Start() +} + +func TestGeziyor_IP(t *testing.T) { geziyor := NewGeziyor(Opt{ StartURLs: []string{"http://api.ipify.org"}, Cache: httpcache.NewMemoryCache(), @@ -19,7 +28,7 @@ func TestGeziyor_StartURLs_Simple(t *testing.T) { geziyor.Start() } -func TestGeziyor_StartURLs_HTML(t *testing.T) { +func TestGeziyor_HTML(t *testing.T) { geziyor := NewGeziyor(Opt{ StartURLs: []string{"http://quotes.toscrape.com/"}, ParseFunc: func(r *Response) {