Head API added. Opt renamed to Options. Tests updated. More documentation added.
This commit is contained in:
parent
95d97436bf
commit
b90908066b
@ -10,6 +10,7 @@ import (
|
|||||||
var file *os.File
|
var file *os.File
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
|
|
||||||
|
// Export exports response data as JSON streaming file
|
||||||
func Export(response *Response) {
|
func Export(response *Response) {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
newFile, err := os.OpenFile("out.json", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
newFile, err := os.OpenFile("out.json", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
|
18
geziyor.go
18
geziyor.go
@ -18,13 +18,13 @@ import (
|
|||||||
type Geziyor struct {
|
type Geziyor struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
opt Opt
|
opt Options
|
||||||
|
|
||||||
visitedURLS []string
|
visitedURLS []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opt is custom options type for Geziyor
|
// Options is custom options type for Geziyor
|
||||||
type Opt struct {
|
type Options struct {
|
||||||
AllowedDomains []string
|
AllowedDomains []string
|
||||||
StartURLs []string
|
StartURLs []string
|
||||||
ParseFunc func(response *Response)
|
ParseFunc func(response *Response)
|
||||||
@ -37,7 +37,7 @@ func init() {
|
|||||||
|
|
||||||
// NewGeziyor creates new Geziyor with default values.
|
// NewGeziyor creates new Geziyor with default values.
|
||||||
// If options provided, options
|
// If options provided, options
|
||||||
func NewGeziyor(opt Opt) *Geziyor {
|
func NewGeziyor(opt Options) *Geziyor {
|
||||||
geziyor := &Geziyor{
|
geziyor := &Geziyor{
|
||||||
client: &http.Client{
|
client: &http.Client{
|
||||||
Timeout: time.Second * 10,
|
Timeout: time.Second * 10,
|
||||||
@ -72,6 +72,16 @@ func (g *Geziyor) Get(url string) {
|
|||||||
g.Do(req)
|
g.Do(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Head issues a HEAD to the specified URL
|
||||||
|
func (g *Geziyor) Head(url string) {
|
||||||
|
req, err := http.NewRequest("HEAD", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Request creating error %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
g.Do(req)
|
||||||
|
}
|
||||||
|
|
||||||
// Do sends an HTTP request
|
// Do sends an HTTP request
|
||||||
func (g *Geziyor) Do(req *http.Request) {
|
func (g *Geziyor) Do(req *http.Request) {
|
||||||
g.wg.Add(1)
|
g.wg.Add(1)
|
||||||
|
@ -1,37 +1,38 @@
|
|||||||
package geziyor
|
package geziyor_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
"github.com/fpfeng/httpcache"
|
"github.com/fpfeng/httpcache"
|
||||||
|
"github.com/geziyor/geziyor"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGeziyor_Simple(t *testing.T) {
|
func TestGeziyor_Simple(t *testing.T) {
|
||||||
NewGeziyor(Opt{
|
geziyor.NewGeziyor(geziyor.Options{
|
||||||
StartURLs: []string{"http://api.ipify.org"},
|
StartURLs: []string{"http://api.ipify.org"},
|
||||||
ParseFunc: func(r *Response) {
|
ParseFunc: func(r *geziyor.Response) {
|
||||||
fmt.Println(string(r.Body))
|
fmt.Println(string(r.Body))
|
||||||
},
|
},
|
||||||
}).Start()
|
}).Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGeziyor_IP(t *testing.T) {
|
func TestGeziyor_IP(t *testing.T) {
|
||||||
geziyor := NewGeziyor(Opt{
|
gez := geziyor.NewGeziyor(geziyor.Options{
|
||||||
StartURLs: []string{"http://api.ipify.org"},
|
StartURLs: []string{"http://api.ipify.org"},
|
||||||
Cache: httpcache.NewMemoryCache(),
|
Cache: httpcache.NewMemoryCache(),
|
||||||
ParseFunc: func(r *Response) {
|
ParseFunc: func(r *geziyor.Response) {
|
||||||
fmt.Println(string(r.Body))
|
fmt.Println(string(r.Body))
|
||||||
r.Geziyor.Get("http://api.ipify.org")
|
r.Geziyor.Get("http://api.ipify.org")
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
geziyor.Start()
|
gez.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGeziyor_HTML(t *testing.T) {
|
func TestGeziyor_HTML(t *testing.T) {
|
||||||
geziyor := NewGeziyor(Opt{
|
gez := geziyor.NewGeziyor(geziyor.Options{
|
||||||
StartURLs: []string{"http://quotes.toscrape.com/"},
|
StartURLs: []string{"http://quotes.toscrape.com/"},
|
||||||
ParseFunc: func(r *Response) {
|
ParseFunc: func(r *geziyor.Response) {
|
||||||
r.Doc.Find("div.quote").Each(func(i int, s *goquery.Selection) {
|
r.Doc.Find("div.quote").Each(func(i int, s *goquery.Selection) {
|
||||||
// Export Data
|
// Export Data
|
||||||
r.Exports <- map[string]interface{}{
|
r.Exports <- map[string]interface{}{
|
||||||
@ -49,14 +50,14 @@ func TestGeziyor_HTML(t *testing.T) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
geziyor.Start()
|
gez.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGeziyor_Concurrent_Requests(t *testing.T) {
|
func TestGeziyor_Concurrent_Requests(t *testing.T) {
|
||||||
geziyor := NewGeziyor(Opt{
|
gez := geziyor.NewGeziyor(geziyor.Options{
|
||||||
AllowedDomains: []string{"quotes.toscrape.com"},
|
AllowedDomains: []string{"quotes.toscrape.com"},
|
||||||
StartURLs: []string{"http://quotes.toscrape.com/"},
|
StartURLs: []string{"http://quotes.toscrape.com/"},
|
||||||
ParseFunc: func(r *Response) {
|
ParseFunc: func(r *geziyor.Response) {
|
||||||
//r.Exports <- map[string]interface{}{"href": r.Request.URL.String()}
|
//r.Exports <- map[string]interface{}{"href": r.Request.URL.String()}
|
||||||
r.Doc.Find("a").Each(func(i int, s *goquery.Selection) {
|
r.Doc.Find("a").Each(func(i int, s *goquery.Selection) {
|
||||||
if href, ok := s.Attr("href"); ok {
|
if href, ok := s.Attr("href"); ok {
|
||||||
@ -65,5 +66,5 @@ func TestGeziyor_Concurrent_Requests(t *testing.T) {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
geziyor.Start()
|
gez.Start()
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Response type wraps http.Response
|
||||||
|
// Contains parsed response data and Geziyor functions.
|
||||||
type Response struct {
|
type Response struct {
|
||||||
*http.Response
|
*http.Response
|
||||||
Body []byte
|
Body []byte
|
||||||
@ -15,6 +17,7 @@ type Response struct {
|
|||||||
Exports chan map[string]interface{}
|
Exports chan map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JoinURL joins base response URL and provided relative URL.
|
||||||
func (r *Response) JoinURL(relativeURL string) string {
|
func (r *Response) JoinURL(relativeURL string) string {
|
||||||
parsedRelativeURL, err := url.Parse(relativeURL)
|
parsedRelativeURL, err := url.Parse(relativeURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user