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 once sync.Once
|
||||
|
||||
// Export exports response data as JSON streaming file
|
||||
func Export(response *Response) {
|
||||
once.Do(func() {
|
||||
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 {
|
||||
client *http.Client
|
||||
wg sync.WaitGroup
|
||||
opt Opt
|
||||
opt Options
|
||||
|
||||
visitedURLS []string
|
||||
}
|
||||
|
||||
// Opt is custom options type for Geziyor
|
||||
type Opt struct {
|
||||
// Options is custom options type for Geziyor
|
||||
type Options struct {
|
||||
AllowedDomains []string
|
||||
StartURLs []string
|
||||
ParseFunc func(response *Response)
|
||||
@ -37,7 +37,7 @@ func init() {
|
||||
|
||||
// NewGeziyor creates new Geziyor with default values.
|
||||
// If options provided, options
|
||||
func NewGeziyor(opt Opt) *Geziyor {
|
||||
func NewGeziyor(opt Options) *Geziyor {
|
||||
geziyor := &Geziyor{
|
||||
client: &http.Client{
|
||||
Timeout: time.Second * 10,
|
||||
@ -72,6 +72,16 @@ func (g *Geziyor) Get(url string) {
|
||||
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
|
||||
func (g *Geziyor) Do(req *http.Request) {
|
||||
g.wg.Add(1)
|
||||
|
@ -1,37 +1,38 @@
|
||||
package geziyor
|
||||
package geziyor_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/fpfeng/httpcache"
|
||||
"github.com/geziyor/geziyor"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGeziyor_Simple(t *testing.T) {
|
||||
NewGeziyor(Opt{
|
||||
geziyor.NewGeziyor(geziyor.Options{
|
||||
StartURLs: []string{"http://api.ipify.org"},
|
||||
ParseFunc: func(r *Response) {
|
||||
ParseFunc: func(r *geziyor.Response) {
|
||||
fmt.Println(string(r.Body))
|
||||
},
|
||||
}).Start()
|
||||
}
|
||||
|
||||
func TestGeziyor_IP(t *testing.T) {
|
||||
geziyor := NewGeziyor(Opt{
|
||||
gez := geziyor.NewGeziyor(geziyor.Options{
|
||||
StartURLs: []string{"http://api.ipify.org"},
|
||||
Cache: httpcache.NewMemoryCache(),
|
||||
ParseFunc: func(r *Response) {
|
||||
ParseFunc: func(r *geziyor.Response) {
|
||||
fmt.Println(string(r.Body))
|
||||
r.Geziyor.Get("http://api.ipify.org")
|
||||
},
|
||||
})
|
||||
geziyor.Start()
|
||||
gez.Start()
|
||||
}
|
||||
|
||||
func TestGeziyor_HTML(t *testing.T) {
|
||||
geziyor := NewGeziyor(Opt{
|
||||
gez := geziyor.NewGeziyor(geziyor.Options{
|
||||
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) {
|
||||
// Export Data
|
||||
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) {
|
||||
geziyor := NewGeziyor(Opt{
|
||||
gez := geziyor.NewGeziyor(geziyor.Options{
|
||||
AllowedDomains: []string{"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.Doc.Find("a").Each(func(i int, s *goquery.Selection) {
|
||||
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"
|
||||
)
|
||||
|
||||
// Response type wraps http.Response
|
||||
// Contains parsed response data and Geziyor functions.
|
||||
type Response struct {
|
||||
*http.Response
|
||||
Body []byte
|
||||
@ -15,6 +17,7 @@ type Response struct {
|
||||
Exports chan map[string]interface{}
|
||||
}
|
||||
|
||||
// JoinURL joins base response URL and provided relative URL.
|
||||
func (r *Response) JoinURL(relativeURL string) string {
|
||||
parsedRelativeURL, err := url.Parse(relativeURL)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user