eansearch

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 9 Imported by: 0

README

A Go module for EAN, GTIN and ISBN name lookup and validation using the API on https://www.ean-search.org

package main

import (
	eansearch "github.com/eansearch/go-ean-search"
	"fmt"
)

func printProduct(p eansearch.Product) {
	fmt.Println("EAN:\t", p.Ean)
	fmt.Println("\t Name:", p.Name)
	fmt.Println("\t CategoryID:", p.CategoryID)
	fmt.Println("\t CategoryName:", p.CategoryName)
	fmt.Println("\t IssuingCountry:", p.IssuingCountry)
}

func main() {
	var products []eansearch.Product
	var more bool
	var err error
	var country string

	// get an API token at https://www.ean-search.org/ean-database-api.html
	token := "abcdefg"
	eansearch.SetToken(token)

	products, err = eansearch.BarcodeLookup("5099750442227", eansearch.English)

	if err != nil {
		fmt.Println(err)
	} else if len(products) == 0 {
		fmt.Println("No results found")
	} else {
		printProduct(products[0])
	}

	products, err = eansearch.ISBNLookup("1119578884")

	if err != nil {
		fmt.Println(err)
	} else if len(products) == 0 {
		fmt.Println("No results found")
	} else {
		printProduct(products[0])
	}

	products, more, err = eansearch.ProductSearch("esprit pullover", 0, eansearch.English)

	if err != nil {
		fmt.Println(err)
	} else if len(products) == 0 {
		fmt.Println("No results found")
	} else {
		for _, p := range products {
			printProduct(p)
		}
		if more {
			fmt.Println("More results available")
		}
	}

	products, more, err = eansearch.BarcodePrefixSearch("40620999", 0, eansearch.AnyLanguage)

	if err != nil {
		fmt.Println(err)
	} else if len(products) == 0 {
		fmt.Println("No results found")
	} else {
		for _, p := range products {
			printProduct(p)
		}
		if more {
			fmt.Println("More results available")
		}
	}

	ean := "5099750442227"
	country, err = eansearch.IssuingCountryLookup(ean)

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("EAN %s was issued in %s\n", ean, country)
	}


        ean = "5099750442228"
        valid, err = eansearch.VerifyChecksum(ean)

        if err != nil {
                fmt.Println(err)
        } else {
                if (valid) {
                        fmt.Printf("EAN %s is VALID\n", ean)
                } else {
                        fmt.Printf("EAN %s is INVALID\n", ean)
                }
        }

        ean = "5099750442227"
        image, err = eansearch.BarcodeImage(ean)

        if err != nil {
                fmt.Println(err)
        } else {
                //fmt.Print("Content-Type: image/png\n\n")
                //fmt.Printf("%s", image)
                _ = image
                fmt.Println("Barcode image received")
        }

}

Documentation

Overview

Package eansearch is a Go module for EAN, GTIN and ISBN name lookup and validation

Index

Constants

View Source
const AnyLanguage uint = 99
View Source
const Danish uint = 2
View Source
const Dutch uint = 10
View Source
const English uint = 1
View Source
const Finish uint = 5
View Source
const French uint = 6
View Source
const German uint = 3
View Source
const Italian uint = 8
View Source
const Norwegian uint = 11
View Source
const Polish uint = 12
View Source
const Portuguese uint = 13
View Source
const Spanish uint = 4
View Source
const Swedish uint = 15

Variables

This section is empty.

Functions

func BarcodeImage added in v1.3.0

func BarcodeImage(ean string) ([]byte, error)

func IssuingCountryLookup added in v1.3.0

func IssuingCountryLookup(ean string) (string, error)

func SetToken

func SetToken(t string) error

SetToken initialises the API with the token, you can apply for at https://www.ean-search.org/ean-database-api.html

func VerifyChecksum added in v1.3.0

func VerifyChecksum(ean string) (bool, error)

Types

type Checksum added in v1.3.0

type Checksum struct {
	Ean   string
	Valid string
}

type ChecksumOrError added in v1.3.0

type ChecksumOrError struct {
	Checksum
	Error string
}

type Image added in v1.3.0

type Image struct {
	Ean     string
	Barcode string
}

type ImageOrError added in v1.3.0

type ImageOrError struct {
	Image
	Error string
}

type Product

type Product struct {
	Ean            string
	Name           string
	CategoryID     uint `json:",string"`
	CategoryName   string
	IssuingCountry string
}

Product holds datasets returned by the API

func BarcodeLookup

func BarcodeLookup(ean string, lang uint) ([]Product, error)

BarcodeLookup searches for a single EAN code

func BarcodePrefixSearch

func BarcodePrefixSearch(prefix string, page uint, lang uint) ([]Product, bool, error)

BarcodePrefixSearch find all EANs strating with a certain prefix

func CategorySearch added in v1.3.0

func CategorySearch(category uint, name string, page uint, lang uint) ([]Product, bool, error)

CategorySearch searches for products by category and name

func ISBNLookup added in v1.3.0

func ISBNLookup(isbn string) ([]Product, error)

ISBNLookup searches for a single ISBN-10 or ISBN-13 code

func ProductSearch

func ProductSearch(name string, page uint, lang uint) ([]Product, bool, error)

ProductSearch searches for products by name

type ProductOrError added in v1.3.0

type ProductOrError struct {
	Product
	Error string
}

Jump to

Keyboard shortcuts

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