Merge pull request #10 from rocketlaunchr/revert-9-revert-8-master
Revert "Revert "feat. colly execute add proxy""
This commit is contained in:
		
							
								
								
									
										13
									
								
								search.go
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								search.go
									
									
									
									
									
								
							@@ -8,6 +8,7 @@ import (
 | 
				
			|||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gocolly/colly/v2"
 | 
						"github.com/gocolly/colly/v2"
 | 
				
			||||||
 | 
						"github.com/gocolly/colly/v2/proxy"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Result represents a single result from Google Search.
 | 
					// Result represents a single result from Google Search.
 | 
				
			||||||
@@ -254,6 +255,9 @@ type SearchOptions struct {
 | 
				
			|||||||
	// OverLimit searches for more results than that specified by Limit.
 | 
						// OverLimit searches for more results than that specified by Limit.
 | 
				
			||||||
	// It then reduces the returned results to match Limit.
 | 
						// It then reduces the returned results to match Limit.
 | 
				
			||||||
	OverLimit bool
 | 
						OverLimit bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// ProxyAddr sets a proxy address to avoid IP blocking.
 | 
				
			||||||
 | 
						ProxyAddr string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Search returns a list of search results from Google.
 | 
					// Search returns a list of search results from Google.
 | 
				
			||||||
@@ -329,6 +333,15 @@ func Search(ctx context.Context, searchTerm string, opts ...SearchOptions) ([]Re
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	url := url(searchTerm, opts[0].CountryCode, lc, limit, opts[0].Start)
 | 
						url := url(searchTerm, opts[0].CountryCode, lc, limit, opts[0].Start)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if opts[0].ProxyAddr != "" {
 | 
				
			||||||
 | 
							rp, err := proxy.RoundRobinProxySwitcher(opts[0].ProxyAddr)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							c.SetProxyFunc(rp)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c.Visit(url)
 | 
						c.Visit(url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if rErr != nil {
 | 
						if rErr != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,7 @@ func TestSearch(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	opts := googlesearch.SearchOptions{
 | 
						opts := googlesearch.SearchOptions{
 | 
				
			||||||
		Limit:     20,
 | 
							Limit:     20,
 | 
				
			||||||
 | 
							ProxyAddr: "socks://127.0.0.1:7890",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	returnLinks, err := googlesearch.Search(ctx, q, opts)
 | 
						returnLinks, err := googlesearch.Search(ctx, q, opts)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user