gozon

package module
v0.0.0-...-78a9764 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 17 Imported by: 0

README

Amazon Product Information Extractor in Go

Overview

This project is an open-source tool developed in Golang for extracting product information from Amazon. It's designed to be fast, efficient, and easy to use, making it an ideal solution for developers looking for Amazon product data.

Features

  • Extracts detailed product information from Amazon
  • Implemented in Go for performance and efficiency
  • Easy to integrate with existing Go projects
  • The code is optimize to work on this format: https://www.amazon.com/[label]/dp/[id]?th=1&psc=1

Examples

Quick testing
    package main

    import (
        "github.com/johnbalvin/gozon"
    )
    func main(){
        //you need to have write permissions, the result will be save inside folder "test"
        gozon.Test()
    }
Basic data
    package main

    import (
        "encoding/json"
        "log"
        "os"
        "github.com/johnbalvin/gozon"
    )
    func main(){
        //you need to have write permissions, the result will be save inside folder "test"
        if err := os.MkdirAll("./test", 0644); err != nil {
            log.Println("test 1 -> err: ", err)
            return
        }
        productURL:="https://www.amazon.com/[label]/dp/[id]?th=1&psc=1"
        client := gozon.DefaulClient()
        data, _, err := client.GetFromURL(productURL)
        if err != nil {
            log.Println("test:2 -> err: ", err)
            return
        }
        f, err := os.Create("./test/data.json")
        if err != nil {
            log.Println("test:3 -> err: ", err)
            return
        }
        json.NewEncoder(f).Encode(data)
    }
Basic data and images
    package main

    import (
        "encoding/json"
        "log"
        "os"
        "github.com/johnbalvin/gozon"
    )
    func main(){
        //you need to have write permissions, the result will be save inside folder "test"
        if err := os.MkdirAll("./test/images", 0644); err != nil {
            log.Println("test 1 -> err: ", err)
            return
        }
        productURL:="https://www.amazon.com/[label]/dp/[id]?th=1&psc=1"
        client := gozon.DefaulClient()
	    client.Currency = "EUR" //you can change the currency
        data, _, err := client.GetFromURL(productURL)
        if err != nil {
            log.Println("test:2 -> err: ", err)
            return
        }
        if err := data.SetImages(client.ProxyURL); err != nil {
            log.Println("test:3 -> err: ", err)
            return
        }
		for j, imgs := range data.Images {
			if len(imgs.Large.Content) != 0 {
				f_name1 := fmt.Sprintf("./test/images/%d_large%s", j, imgs.Large.Extension)
				os.WriteFile(f_name1, imgs.Large.Content, 0644)
			}
			if len(imgs.Thumb.Content) != 0 {
				f_name2 := fmt.Sprintf("./test/images/%d_thumb%s", j, imgs.Thumb.Extension)
				os.WriteFile(f_name2, imgs.Thumb.Content, 0644)
			}
			if len(imgs.HiRes.Content) != 0 {
				f_name3 := fmt.Sprintf("./test/images/%d_hires%s", j, imgs.HiRes.Extension)
				os.WriteFile(f_name3, imgs.HiRes.Content, 0644)
			}
		}
        f, err := os.Create("./test/data.json")
        if err != nil {
            log.Println("test:4 -> err: ", err)
            return
        }
        json.NewEncoder(f).Encode(data)
    }
With proxy
    package main

    import (
        "encoding/json"
        "log"
        "os"
        "github.com/johnbalvin/gozon"
    )
    func main(){
        proxyURL, err := gozon.ParseProxy("http://[IP | domain]:[port]", "username", "password")
        if err != nil {
            log.Println("test:1 -> err: ", err)
            return
        }
        //You need to place the country code, otherwise it will amazon will get it from the IP
        client := gozon.NewClient("US","MXN", proxyURL, 6)
        productsURLs, _, err := client.GetMainURLs()
        data, _, err := client.GetFromURL(productURL)
        if err != nil {
            log.Println("test:2 -> err: ", err)
            continue
        }
        if err := data.SetImages(client.ProxyURL); err != nil {
            log.Println("test:3 -> err: ", err)
            return
        }
        for j, imgs := range data.Images {
            if len(imgs.Large.Content) != 0 {
                f_name1 := fmt.Sprintf("./test/images/%d_large%s", j, imgs.Large.Extension)
                os.WriteFile(f_name1, imgs.Large.Content, 0644)
            }
            if len(imgs.Thumb.Content) != 0 {
                f_name2 := fmt.Sprintf("./test/images/%d_thumb%s", j, imgs.Thumb.Extension)
                os.WriteFile(f_name2, imgs.Thumb.Content, 0644)
            }
            if len(imgs.HiRes.Content) != 0 {
                f_name3 := fmt.Sprintf("./test/images/%d_hires%s", j, imgs.HiRes.Extension)
                os.WriteFile(f_name3, imgs.HiRes.Content, 0644)
            }
        }
        f, err := os.Create("./test/data.json")
        if err != nil {
            log.Println("test:4 -> err: ", err)
            return
        }
        json.NewEncoder(f).Encode(data)
    }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetImg

func GetImg(imgURL string, proxyURL *url.URL) (string, []byte, error)

func GetMainURLs

func GetMainURLs(countryCode, currency string, proxyURL *url.URL, tries int) ([]string, int, error)

func ParseBodyMainList

func ParseBodyMainList(body []byte) ([]string, error)

func ParseProxy

func ParseProxy(urlToParse, userName, password string) (*url.URL, error)

func RemoveSpace

func RemoveSpace(value string) string

func Test

func Test()

func TestWithProxy

func TestWithProxy()

Types

type Asin

type Asin struct {
	Me     string
	Parent string
}

type Client

type Client struct {
	CountryCode string //ISO country code, example: USD, MX
	Currency    string //ISO currency, example: USD, EUR
	ProxyURL    *url.URL
	Tries       int
}

func DefaulClient

func DefaulClient() Client

func NewClient

func NewClient(countryCode, currency string, proxyURL *url.URL, tries int) Client

func (Client) GetFromID

func (cl Client) GetFromID(label, id string) (Data, int, error)

func (Client) GetFromURL

func (cl Client) GetFromURL(productURL string) (Data, int, error)

make sure your url contains "?th=1&psc=1" for better results, sometimes if you don't add it it won't show the price it will return the data along with how many tries did for extracting the data

func (Client) GetMainURLs

func (cl Client) GetMainURLs() ([]string, int, error)

type Combination

type Combination struct {
	Asin   string
	Values []string
}

type Data

type Data struct {
	Asin            Asin
	URL             string
	Title           string
	MerchandID      string
	Rating          float32
	Available       bool
	Price           Price
	ShippingAddress string
	Variations      Variations
	MainPanelDesc   MainPanelDesciptions
	Images          []Imgs
}

func GetFromID

func GetFromID(label, id, countryCode, currency string, proxyURL *url.URL, tries int) (Data, int, error)

func GetFromURL

func GetFromURL(productURL, countryCode, currency string, proxyURL *url.URL, tries int) (Data, int, error)

GetFromURL gets the information with the product url //the code is optimize to work on this format: https://www.amazon.com/[label]/dp/[id]?th=1&psc=1 proxyURL is the proxy you would like to use, its recommended to use residential proxies countryCode, set to any country code, this will change the end result, probably the pricing or if available or not -->>>iF COUNTRY CODE IS NOT PRESENT AMAZON WILL CONSIDER THE IP LOCATION <----- tries is how many tries you want to make before throwing an error, this is usefull because sometimes amazom will require a captcha make soure your url contains "?th=1&psc=1" for better results, sometimes if you don't add it it wont' show the price it will return the data along with how many tries did for extracting the data

func ParseBodyDetails

func ParseBodyDetails(body []byte) (Data, error)

func (*Data) SetImages

func (data *Data) SetImages(proxyURL *url.URL) error

type IconPanel

type IconPanel struct {
	URL   string
	Label string
	Value string
}

type Imgs

type Imgs struct {
	Variant string
	Large   URLImg
	Thumb   URLImg
	HiRes   URLImg
}

func (*Imgs) SetImages

func (imgs *Imgs) SetImages(proxyURL *url.URL) error

type ImgsPage

type ImgsPage struct {
	Variant string `json:"variant"`
	Large   string `json:"large"`
	Thumb   string `json:"thumb"`
	HiRes   string `json:"hiRes"`
}

type Label

type Label struct {
	Size_name  string `json:"size_name"`
	Color_name string `json:"color_name"`
}

type LabelValue

type LabelValue struct {
	Label string
	Value string
}

type LabelValues

type LabelValues struct {
	Label  string
	Values []string
}

type MainPanelDesciptions

type MainPanelDesciptions struct {
	Details    []LabelValue
	AboutThis  []string
	IconsPanel []IconPanel
}

type Price

type Price struct {
	Low               float32
	High              float32
	Base              float32
	SavingsPercentage int
	CurrencySymbol    string
}

type URLImg

type URLImg struct {
	ContentType string
	Extension   string
	URL         string
	Content     []byte `json:"-"`
}

type Variations

type Variations struct {
	Labels       []LabelValues
	Combinations []Combination
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL