Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial SDK #2

Merged
merged 27 commits into from
Jan 3, 2024
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6bb1b50
init commit
maaz-munir Nov 23, 2023
62e861a
some improvements + added bing and baidu
maaz-munir Nov 24, 2023
7ada257
Apply suggestions from code review
maaz-munir Nov 28, 2023
37300b9
added google_search + adjusted or multiple return types
maaz-munir Dec 1, 2023
6e66508
added google source and check for empty url
maaz-munir Dec 1, 2023
fb3c840
Apply suggestions from code review v2
maaz-munir Dec 7, 2023
e72bf48
added remaining google serp sources
maaz-munir Dec 7, 2023
2ffd8e4
comments + some more checks
maaz-munir Dec 8, 2023
f2f2749
check for async runtime models
maaz-munir Dec 11, 2023
14e3d24
Apply suggestions from code review v3 + yandex
maaz-munir Dec 12, 2023
f7dd301
bing and baidu async models + some improvements
maaz-munir Dec 12, 2023
905ed7c
2 google funcs + better error handling with channels
maaz-munir Dec 12, 2023
1420df5
rest of google sources for async polling model
maaz-munir Dec 12, 2023
d14237b
parse checks in google_async + some comment fixes
maaz-munir Dec 16, 2023
d3c3c64
proxy endpoint integration method
maaz-munir Dec 16, 2023
ace829f
send custom headers with proxy endpoint
maaz-munir Dec 16, 2023
0d54a22
make GeoLocation param a ptr
maaz-munir Dec 18, 2023
1a9a06b
refactor async functions
maaz-munir Dec 18, 2023
179164d
update creating payload in google_search funcs
maaz-munir Dec 19, 2023
ce1b8ce
update public func comments
maaz-munir Dec 19, 2023
8fda0b6
Apply suggestions from code review v4
maaz-munir Dec 20, 2023
608084f
update readme
maaz-munir Dec 20, 2023
c689f01
comment
maaz-munir Dec 20, 2023
9270fd9
comments + spelling fixes
maaz-munir Dec 20, 2023
8a4b9c8
update readme
maaz-munir Dec 20, 2023
fe7dda5
update readme
maaz-munir Dec 21, 2023
c250f36
fmt
maaz-munir Dec 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
comments + some more checks
maaz-munir committed Dec 8, 2023

Verified

This commit was signed with the committer’s verified signature.
andrewlock Andrew Lock
commit 2ffd8e4d8ddb07ce8c44e744fde182d0e8441d3c
4 changes: 2 additions & 2 deletions serp/baidu.go
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ type BaiduSearchOpts struct {
CallbackUrl string
}

// Scrapes Baidu via its search engine.
// ScrapeBaiduSearch scrapes baidu with baidu_search as source.
func (c *SerpClient) ScrapeBaiduSearch(
query string,
opts ...*BaiduSearchOpts,
@@ -96,7 +96,7 @@ type BaiduUrlOpts struct {
CallbackUrl string
}

// Scrapes Baidu via its url.
// ScrapeBaiduUrl scrapes baidu with baidu as source.
func (c *SerpClient) ScrapeBaiduUrl(
url string,
opts ...*BaiduUrlOpts,
4 changes: 2 additions & 2 deletions serp/bing.go
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ type BingSearchOpts struct {
Render oxylabs.Render
}

// Scrapes Bing via its search engine.
// ScraperBingSearch scrapes bing with bing_search as source.
func (c *SerpClient) ScrapeBingSearch(
query string,
opts ...*BingSearchOpts,
@@ -116,7 +116,7 @@ type BingUrlOpts struct {
CallbackUrl string
}

// Scrapes Bing via provided url.
// ScrapeBingUrl scrapes bing with bing as source.
func (c *SerpClient) ScrapeBingUrl(
url string,
opts ...*BingUrlOpts,
59 changes: 46 additions & 13 deletions serp/google.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"github.com/mslmio/oxylabs-sdk-go/oxylabs"
)

// Accepted Parameters for context options in google.
var AcceptedTbmParameters = []string{
"app",
"bks",
@@ -19,7 +20,6 @@ var AcceptedTbmParameters = []string{
"rcp",
"lcl",
}

var AcceptedSearchTypeParameters = []string{
"web_search",
"image_search",
@@ -109,11 +109,18 @@ func (opt *GoogleHotelsOpts) checkParameterValidity(ctx ContextOption) error {
return fmt.Errorf("limit, pages and start_page parameters must be greater than 0")
}

if ctx["hotel_occupancy"] != nil && ctx["hotel_occupancy"].(int) < 0 {
return fmt.Errorf("invalid hotel_occupancy parameter: %v", ctx["hotel_occupancy"])
}

return nil
}

// checkParameterValidity checks validity of google travel hotels parameters.
func (opt *GoogleTravelHotelsOpts) checkParameterValidity(ctx ContextOption) error {
if !oxylabs.IsUserAgentValid(opt.UserAgent) {
return fmt.Errorf("invalid user agent parameter: %v", opt.UserAgent)
}

if opt.Render != "" && !oxylabs.IsRenderValid(opt.Render) {
return fmt.Errorf("invalid render parameter: %v", opt.Render)
@@ -123,12 +130,23 @@ func (opt *GoogleTravelHotelsOpts) checkParameterValidity(ctx ContextOption) err
return fmt.Errorf("limit, pages and start_page parameters must be greater than 0")
}

if ctx["hotel_occupancy"] != nil && ctx["hotel_occupancy"].(int) < 0 {
return fmt.Errorf("invalid hotel_occupancy parameter: %v", ctx["hotel_occupancy"])
}

if ctx["hotel_classes"] != nil {
for _, value := range ctx["hotel_classes"].([]int) {
if value < 2 || value > 5 {
return fmt.Errorf("invalid hotel_classes parameter: %v", value)
}
}
}

return nil
}

// checkParameterValidity checks validity of google trends explore parameters.
func (opt *GoogleTrendsExploreOpts) checkParameterValidity(ctx ContextOption) error {

if !oxylabs.IsUserAgentValid(opt.UserAgent) {
return fmt.Errorf("invalid user agent parameter: %v", opt.UserAgent)
}
@@ -137,6 +155,10 @@ func (opt *GoogleTrendsExploreOpts) checkParameterValidity(ctx ContextOption) er
return fmt.Errorf("invalid search_type parameter: %v", ctx["search_type"])
}

if ctx["category_id"] != nil && ctx["category_id"].(int) < 0 {
return fmt.Errorf("invalid category_id")
}

return nil
}

@@ -591,6 +613,7 @@ type GoogleTravelHotelsOpts struct {
Limit int
Locale string
GeoLocation string
UserAgent oxylabs.UserAgent
Render oxylabs.Render
CallbackURL string
Context []func(ContextOption)
@@ -627,16 +650,17 @@ func (c *SerpClient) ScrapeGoogleTravelHotels(

// Prepare payload.
payload := map[string]interface{}{
"source": "google_travel_hotels",
"domain": opt.Domain,
"query": query,
"start_page": opt.StartPage,
"pages": opt.Pages,
"limit": opt.Limit,
"locale": opt.Locale,
"geo_location": opt.GeoLocation,
"render": opt.Render,
"callback_url": opt.CallbackURL,
"source": "google_travel_hotels",
"domain": opt.Domain,
"query": query,
"start_page": opt.StartPage,
"pages": opt.Pages,
"limit": opt.Limit,
"locale": opt.Locale,
"geo_location": opt.GeoLocation,
"user_agent_type": opt.UserAgent,
"render": opt.Render,
"callback_url": opt.CallbackURL,
"context": []map[string]interface{}{
{
"key": "hotel_occupancy",
@@ -765,6 +789,15 @@ func (c *SerpClient) ScrapeGoogleTrendsExplore(
modifier(context)
}

// Set defaults.
SetDefaultUserAgent(&opt.UserAgent)

// Check validity of parameters.
err := opt.checkParameterValidity(context)
if err != nil {
return nil, err
}

// Prepare payload.
payload := map[string]interface{}{
"source": "google_trends_explore",
@@ -795,7 +828,7 @@ func (c *SerpClient) ScrapeGoogleTrendsExplore(
if err != nil {
return nil, fmt.Errorf("error marshalling payload: %v", err)
}
fmt.Printf("%+v\n\n", payload)

res, err := c.Req(jsonPayload, false, "POST")
if err != nil {
return nil, err
4 changes: 2 additions & 2 deletions serp/yandex.go
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ type YandexSearchOpts struct {
CallbackUrl string
}

// Scrapes Yandex via its search engine.
// ScrapYandexSearch scrapes yandex with yandex_search as source.
func (c *SerpClient) ScrapeYandexSearch(
query string,
opts ...*YandexSearchOpts,
@@ -126,7 +126,7 @@ type YandexUrlOpts struct {
CallbackUrl string
}

// Scrapes Yandex via provided url.
// ScapeYandexUrl scrapes yandex with yandex as source.
func (c *SerpClient) ScrapeYandexUrl(
url string,
opts ...*YandexUrlOpts,