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
Show file tree
Hide file tree
Changes from 9 commits
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
Empty file added CHANGELOG.md
Empty file.
Empty file added CODE_OF_CONDUCT.md
Empty file.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/mslmio/oxylabs-sdk-go

go 1.21.0
40 changes: 40 additions & 0 deletions oxylabs/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package oxylabs

import (
"fmt"
"net/url"
"strings"
)

func ValidateURL(
inputURL string,
host string,
) error {
// Check if url is empty.
if inputURL == "" {
return fmt.Errorf("url parameter is empty")
}

// Parse the URL
parsedURL, err := url.ParseRequestURI(inputURL)
if err != nil {
return fmt.Errorf("failed to parse URL: %v", err)
}

// Check if the scheme (protocol) is present and non-empty.
if parsedURL.Scheme == "" {
return fmt.Errorf("url is missing scheme")
}

// Check if the Host is present and non-empty.
if parsedURL.Host == "" {
return fmt.Errorf("url is missing a host")
}

// Check if the Host matches the expected domain/host.
if !strings.Contains(parsedURL.Host, host) {
return fmt.Errorf("url does not belong to %s", host)
}

return nil
}
84 changes: 84 additions & 0 deletions oxylabs/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package oxylabs

type UserAgent string

var (
UA_DESKTOP UserAgent = "desktop"
UA_DESKTOP_CHROME UserAgent = "desktop_chrome"
UA_DESKTOP_EDGE UserAgent = "desktop_edge"
UA_DESKTOP_FIREFOX UserAgent = "desktop_firefox"
UA_DESKTOP_OPERA UserAgent = "desktop_opera"
UA_DESKTOP_SAFARI UserAgent = "desktop_safari"
UA_MOBILE UserAgent = "mobile"
UA_MOBILE_ANDROID UserAgent = "mobile_android"
UA_MOBILE_IOS UserAgent = "mobile_ios"
UA_TABLET UserAgent = "tablet"
UA_TABLET_ANDROID UserAgent = "tablet_android"
UA_TABLET_IOS UserAgent = "tablet_ios"
)

func IsUserAgentValid(ua UserAgent) bool {
switch ua {
case
UA_DESKTOP,
UA_DESKTOP_CHROME,
UA_DESKTOP_EDGE,
UA_DESKTOP_FIREFOX,
UA_DESKTOP_OPERA,
UA_DESKTOP_SAFARI,
UA_MOBILE,
UA_MOBILE_ANDROID,
UA_MOBILE_IOS,
UA_TABLET,
UA_TABLET_ANDROID,
UA_TABLET_IOS:
return true
default:
return false
}
}

type Render string

var (
HTML Render = "html"
PNG Render = "png"
)

func IsRenderValid(render Render) bool {
switch render {
case
HTML,
PNG:
return true
default:
return false
}
}

type Domain string

var (
DOMAIN_COM Domain = "com"
DOMAIN_RU Domain = "ru"
DOMAIN_UA Domain = "ua"
DOMAIN_BY Domain = "by"
DOMAIN_KZ Domain = "kz"
DOMAIN_TR Domain = "tr"
DOMAIN_CN Domain = "cn"
)

type Locale string

var (
LOCALE_EN Locale = "en"
LOCALE_RU Locale = "ru"
LOCALE_BY Locale = "by"
LOCALE_DE Locale = "de"
LOCALE_FR Locale = "fr"
LOCALE_ID Locale = "id"
LOCALE_KK Locale = "kk"
LOCALE_TT Locale = "tt"
LOCALE_TR Locale = "tr"
LOCALE_UK Locale = "uk"
)
11 changes: 11 additions & 0 deletions oxylabs/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oxylabs

// Checks if the parameter is in the list of accepted parameters.
func InList[T comparable](val T, list []T) bool {
for _, item := range list {
if item == val {
return true
}
}
return false
}
143 changes: 143 additions & 0 deletions serp/baidu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package serp

import (
"encoding/json"
"fmt"

"github.com/mslmio/oxylabs-sdk-go/oxylabs"
)

// Accepted parameters for baidu.
var BaiduSearchAcceptedDomainParameters = []oxylabs.Domain{
oxylabs.DOMAIN_COM,
oxylabs.DOMAIN_CN,
}

// checkParameterValidity checks validity of baidu search parameters.
func (opt *BaiduSearchOpts) checkParameterValidity() error {
if !oxylabs.InList(opt.Domain, BaiduSearchAcceptedDomainParameters) {
return fmt.Errorf("invalid domain parameter: %s", opt.Domain)
}

if !oxylabs.IsUserAgentValid(opt.UserAgent) {
return fmt.Errorf("invalid user agent parameter: %v", opt.UserAgent)
}

return nil
}

// checkParameterValidity checks validity of baidu url parameters.
func (opt *BaiduUrlOpts) checkParameterValidity() error {
if !oxylabs.IsUserAgentValid(opt.UserAgent) {
return fmt.Errorf("invalid user agent parameter: %v", opt.UserAgent)
}

return nil
}

type BaiduSearchOpts struct {
Domain oxylabs.Domain
StartPage int
Pages int
Limit int
UserAgent oxylabs.UserAgent
CallbackUrl string
}

// ScrapeBaiduSearch scrapes baidu with baidu_search as source.
func (c *SerpClient) ScrapeBaiduSearch(
query string,
opts ...*BaiduSearchOpts,
) (*Response, error) {
// Prepare options
opt := &BaiduSearchOpts{}
if len(opts) > 0 && opts[len(opts)-1] != nil {
opt = opts[len(opts)-1]
}

// Set defaults.
SetDefaultDomain(&opt.Domain)
SetDefaultStartPage(&opt.StartPage)
SetDefaultLimit(&opt.Limit)
SetDefaultUserAgent(&opt.UserAgent)

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

// Prepare payload.
payload := map[string]interface{}{
"source": "baidu_search",
"domain": opt.Domain,
"query": query,
"start_page": opt.StartPage,
"pages": opt.Pages,
"limit": opt.Limit,
"user_agent_type": opt.UserAgent,
"callback_url": opt.CallbackUrl,
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("error marshalling payload: %v", err)
}

res, err := c.Req(jsonPayload, false, "POST")
if err != nil {
return nil, err
}

return res, nil
}

type BaiduUrlOpts struct {
UserAgent oxylabs.UserAgent
CallbackUrl string
}

// ScrapeBaiduUrl scrapes baidu with baidu as source.
func (c *SerpClient) ScrapeBaiduUrl(
url string,
opts ...*BaiduUrlOpts,
) (*Response, error) {
// Check validity of url.
err := oxylabs.ValidateURL(url, "baidu")
if err != nil {
return nil, err
}

// Prepare options
opt := &BaiduUrlOpts{}
if len(opts) > 0 && opts[len(opts)-1] != nil {
opt = opts[len(opts)-1]
}

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

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

// Prepare payload.
payload := map[string]interface{}{
"source": "baidu",
"url": url,
"user_agent_type": opt.UserAgent,
"callback_url": opt.CallbackUrl,
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("error marshalling payload: %v", err)
}

res, err := c.Req(jsonPayload, false, "POST")
if err != nil {
return nil, err
}

return res, nil
}
Loading