Merge pull request #12 from sundowndev/feat/google

Improve Google methods
pull/13/head
Raphaël 2020-09-05 17:04:14 +02:00 committed by GitHub
commit 0c9a09cceb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 17 deletions

View File

@ -16,6 +16,8 @@ const (
excludeTag = "-"
intitleTag = "intitle:"
intextTag = "intext:"
operatorOr = "|"
operatorAnd = "+"
)
// GoogleSearch is the Google search implementation for Dorkgen
@ -32,16 +34,21 @@ func (e *GoogleSearch) String() string {
return strings.Join(e.tags, " ")
}
// ToURL converts tags to an encoded Google Search URL
func (e *GoogleSearch) ToURL() string {
baseURL, _ := url.Parse(searchURL)
// QueryValues returns search request as URL values
func (e *GoogleSearch) QueryValues() url.Values {
tags := strings.Join(e.tags, " ")
params := url.Values{}
params.Add("q", tags)
baseURL.RawQuery = params.Encode()
return params
}
// URL converts tags to an encoded Google Search URL
func (e *GoogleSearch) URL() string {
baseURL, _ := url.Parse(searchURL)
baseURL.RawQuery = e.QueryValues().Encode()
return baseURL.String()
}
@ -54,7 +61,13 @@ func (e *GoogleSearch) Site(site string) *GoogleSearch {
// Or puts an OR operator in the request
func (e *GoogleSearch) Or() *GoogleSearch {
e.tags = append(e.tags, "OR")
e.tags = append(e.tags, operatorOr)
return e
}
// And puts an AND operator in the request
func (e *GoogleSearch) And() *GoogleSearch {
e.tags = append(e.tags, operatorAnd)
return e
}
@ -95,8 +108,8 @@ func (e *GoogleSearch) Ext(ext string) *GoogleSearch {
}
// Exclude excludes some results.
func (e *GoogleSearch) Exclude(value string) *GoogleSearch {
e.tags = append(e.tags, e.concat(excludeTag, value, false))
func (e *GoogleSearch) Exclude(tags *GoogleSearch) *GoogleSearch {
e.tags = append(e.tags, e.concat(excludeTag, tags.String(), false))
return e
}
@ -111,3 +124,9 @@ func (e *GoogleSearch) Intitle(value string) *GoogleSearch {
e.tags = append(e.tags, e.concat(intitleTag, value, true))
return e
}
// Plain allows you to add additional values as string without any kind of formatting.
func (e *GoogleSearch) Plain(value string) *GoogleSearch {
e.tags = append(e.tags, value)
return e
}

View File

@ -2,6 +2,7 @@ package dorkgen
import (
"fmt"
"net/url"
"testing"
assertion "github.com/stretchr/testify/assert"
@ -17,7 +18,7 @@ func TestInit(t *testing.T) {
result := dork.
Site("example.com").
ToURL()
URL()
assert.Equal(result, "https://www.google.com/search?q=site%3Aexample.com", "they should be equal")
})
@ -104,16 +105,16 @@ func TestInit(t *testing.T) {
dork = NewGoogleSearch()
result := dork.
Exclude("html").
Exclude("htm").
Exclude("php").
Exclude("md5sums").
Exclude(NewGoogleSearch().Plain("html")).
Exclude(NewGoogleSearch().Plain("htm")).
Exclude(NewGoogleSearch().Plain("php")).
Exclude(NewGoogleSearch().Plain("md5sums")).
String()
assert.Equal(result, "-html -htm -php -md5sums", "they should be equal")
})
t.Run("should handle or tag correctly", func(t *testing.T) {
t.Run("should handle 'OR' tag correctly", func(t *testing.T) {
dork = NewGoogleSearch()
result := dork.
@ -122,7 +123,19 @@ func TestInit(t *testing.T) {
Site("twitter.com").
String()
assert.Equal(result, "site:facebook.com OR site:twitter.com", "they should be equal")
assert.Equal(result, "site:facebook.com | site:twitter.com", "they should be equal")
})
t.Run("should handle 'AND' tag correctly", func(t *testing.T) {
dork = NewGoogleSearch()
result := dork.
Intitle("facebook").
And().
Intitle("twitter").
String()
assert.Equal(result, "intitle:\"facebook\" + intitle:\"twitter\"", "they should be equal")
})
t.Run("should handle group tag correctly", func(t *testing.T) {
@ -133,7 +146,7 @@ func TestInit(t *testing.T) {
Group((NewGoogleSearch()).Intext("1").Or().Intext("2")).
String()
assert.Equal(result, "site:linkedin.com (intext:\"1\" OR intext:\"2\")", "they should be equal")
assert.Equal(result, "site:linkedin.com (intext:\"1\" | intext:\"2\")", "they should be equal")
})
t.Run("should handle group tag correctly", func(t *testing.T) {
@ -145,6 +158,20 @@ func TestInit(t *testing.T) {
Intitle("jordan").
String()
assert.Equal(result, "site:linkedin.com (intext:\"1\" OR intext:\"2\") intitle:\"jordan\"", "they should be equal")
assert.Equal(result, "site:linkedin.com (intext:\"1\" | intext:\"2\") intitle:\"jordan\"", "they should be equal")
})
t.Run("should return URL values", func(t *testing.T) {
dork = NewGoogleSearch()
result := dork.
Site("linkedin.com").
Group((NewGoogleSearch()).Intext("1").Or().Intext("2")).
Intitle("jordan").
QueryValues()
assert.Equal(url.Values{
"q": []string{"site:linkedin.com (intext:\"1\" | intext:\"2\") intitle:\"jordan\""},
}, result, "they should be equal")
})
}