goyml

package module
v0.0.0-...-7605cce Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2018 License: MIT Imports: 10 Imported by: 0

README

goYML

Yandex Market YML (XML) generator for Go (golang)

File format description: https://yandex.ru/support/partnermarket/yml/about-yml.xml

Usage:


	// Create YML
	ymlCat := NewYML("BestShop", "Best online seller Inc.", "http://best.seller.ru/")

	// Additional info
	ymlCat.Shop.Platform = "CMS"
	ymlCat.Shop.Version = "2.3"
	ymlCat.Shop.Agency = "Agency"
	ymlCat.Shop.Email = "CMS@CMS.ru"

	// id, rate, plus
	ymlCat.AddCurrency("RUR", "1", 0)

	// Categories
	ymlCat.AddCategory(1, 0, "Книги")
	ymlCat.AddCategory(2, 1, "Детективы")
	ymlCat.AddCategory(3, 1, "Боевики")
	ymlCat.AddCategory(4, 0, "Видео")
	ymlCat.AddCategory(5, 4, "Комедии")
	ymlCat.AddCategory(6, 0, "Принтеры")
	ymlCat.AddCategory(7, 0, "Оргтехника")

	// Delivery
	// cost, daysFrom, daysTo (if 0 - omitted), orderBefore
	ymlCat.AddDeliveryOption(0, 0, 0, 10)
	ymlCat.AddDeliveryOption(0, 1, 0, 0)

	// Simple Offer
	offer := Offer{
		Id:                   "123",
		Available:            true,
		Bid:                  21,
		Url:                  "http://best.seller.ru/product_page.asp?pid=12348",
		Price:                600,
		OldPrice:             800,
		CurrencyId:           "USD",
		CategoryId:           6,
		Picture:              []string{"http://best.seller.ru/img/device12345.jpg"},
		Store:                false,
		Pickup:               true,
		Delivery:             false,
		Name:                 "Наручные часы Casio A1234567B",
		Vendor:               "Casio",
		VendorCode:           "A1234567B",
		Description:          "Изящные наручные часы.",
		SalesNotes:           "Необходима предоплата.",
		ManufacturerWarranty: true,
		CountryOfOrigin:      "Япония",
		Cpa:                  1,
	}
	offer.AddBarcode("0123456789012")
	offer.AddAge("year", "18")

	// Offer vendor.model
	offer2 := Offer{
		Id:                   "12341",
		Available:            true,
		Type:                 TypeVendorModel,
		Bid:                  13,
		Url:                  "http://best.seller.ru/product_page.asp?pid=12344",
		Price:                16800,
		OldPrice:             17000,
		CurrencyId:           "USD",
		CategoryId:           6,
		Picture:              []string{"http://best.seller.ru/img/device12345.jpg"},
		Store:                false,
		Pickup:               false,
		Delivery:             true,
		TypePrefix:           "Принтер",
		Vendor:               "HP",
		Model:                "Deskjet D2663",
		Description:          "Серия принтеров для людей, которым нужен надежный, простой в использовании цветной принтер для повседневной печати...",
		SalesNotes:           "Необходима предоплата.",
		ManufacturerWarranty: true,
		CountryOfOrigin:      "Япония",
		Cpa:                  1,
		Rec:                  "123123,1214,243",
		Expiry:               "P5Y",
		Weight:               2.07,
		Dimensions:           "100/25.45/11.112",
	}
	offer2.AddBarcode("1234567890120")
	offer2.AddParam("Максимальный формат", "", "А4")
	offer2.AddParam("Технология печати", "", "термическая струйная")
	offer2.AddParam("Тип печати", "", "Цветная")
	offer2.AddParam("Количество страниц в месяц", "стр", "1000")
	offer2.AddParam("Потребляемая мощность", "Вт", "20")
	offer2.AddParam("Вес", "кг", "2.73")

	err := offer.Validate()
	if err != nil {
		fmt.Println(err.Error())
	}
	err = offer2.Validate()
	if err != nil {
		fmt.Println(err.Error())
	}

	ymlCat.AddOffer(offer)
	ymlCat.AddOffer(offer2)

	ExportToFile(ymlCat, "/path/to/file/yml.xml", true)

Documentation

Overview

Yandex YML generator: https://yandex.ru/support/partnermarket/yml/about-yml.xml

Index

Constants

View Source
const Header = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
`

Variables

View Source
var Countries = map[string]struct{}{}/* 210 elements not displayed */

Allowed countries

Functions

func ExportToFile

func ExportToFile(cat Catalog, filename string, pretty bool) error

func ExportToWriter

func ExportToWriter(cat Catalog, w io.Writer, pretty bool) error

Types

type Age

type Age struct {
	Unit  string `xml:"unit,attr"`
	Value string `xml:",chardata"`
}

type Catalog

type Catalog struct {
	XMLName struct{} `xml:"yml_catalog"`
	Shop    Shop     `xml:"shop"`
	Date    string   `xml:"date,attr"`
}

func NewYML

func NewYML(name, company, url string) Catalog

func (*Catalog) AddCategory

func (c *Catalog) AddCategory(id, parentId int, name string)

func (*Catalog) AddCurrency

func (c *Catalog) AddCurrency(id, rate string, plus float64)

func (*Catalog) AddDeliveryOption

func (c *Catalog) AddDeliveryOption(cost int, daysFrom, daysTo int, orderBefore int)

func (*Catalog) AddOffer

func (c *Catalog) AddOffer(offer Offer)

func (*Catalog) SetDate

func (c *Catalog) SetDate(t time.Time)

type Categories

type Categories struct {
	Category []Category `xml:"category"`
}

type Category

type Category struct {
	Id       int    `xml:"id,attr"`
	ParentId int    `xml:"parentId,attr,omitempty"`
	Name     string `xml:",chardata"`
}

type Currencies

type Currencies struct {
	Currency []Currency `xml:"currency"`
}

type Currency

type Currency struct {
	Id   string  `xml:"id,attr"`
	Rate string  `xml:"rate,attr"`
	Plus float64 `xml:"plus,attr"`
}

type DeliveryOption

type DeliveryOption struct {
	Cost        int    `xml:"cost,attr"`
	Days        string `xml:"days,attr"`
	OrderBefore int    `xml:"order-before,attr,omitempty"`
}

type DeliveryOptions

type DeliveryOptions struct {
	Options []DeliveryOption `xml:"option"`
}

type Offer

type Offer struct {
	Id                   string           `xml:"id,attr"`
	Bid                  uint             `xml:"bid,attr,omitempty"`
	CBid                 uint             `xml:"cid,attr,omitempty"`
	Type                 OfferType        `xml:"type,attr,omitempty"`
	Available            bool             `xml:"available,attr"`
	Url                  string           `xml:"url,omitempty"`
	Price                float64          `xml:"price"`
	OldPrice             float64          `xml:"oldprice,omitempty"`
	CurrencyId           string           `xml:"currencyId"`
	CategoryId           int              `xml:"categoryId"`
	MarketCategory       string           `xml:"market_category,omitempty"`
	Picture              []string         `xml:"picture,omitempty"`
	Store                bool             `xml:"store,omitempty"`
	Pickup               bool             `xml:"pickup,omitempty"`
	Delivery             bool             `xml:"delivery,omitempty"`
	DeliveryOptions      *DeliveryOptions `xml:"delivery-options,omitempty"`
	Name                 string           `xml:"name,omitempty"`
	TypePrefix           string           `xml:"typePrefix,omitempty"`
	Vendor               string           `xml:"vendor,omitempty"`
	VendorCode           string           `xml:"vendorCode,omitempty"`
	Model                string           `xml:"model,omitempty"`
	Description          string           `xml:"description,omitempty"`
	SalesNotes           string           `xml:"sales_notes,omitempty"`
	ManufacturerWarranty bool             `xml:"manufacturer_warranty,omitempty"`
	CountryOfOrigin      string           `xml:"country_of_origin,omitempty"`
	Downloadable         bool             `xml:"downloadable,omitempty"`
	Adult                bool             `xml:"adult,omitempty"`
	Age                  *Age             `xml:"age,omitempty"`
	Barcode              []string         `xml:"barcode,omitempty"`
	Cpa                  int              `xml:"cpa,omitempty"`
	Rec                  string           `xml:"rec,omitempty"`
	Expiry               string           `xml:"expiry,omitempty"`
	Weight               float64          `xml:"weight,omitempty"`
	Dimensions           string           `xml:"dimensions,omitempty"`
	Params               []Param          `xml:"param,omitempty"`
}

Available = true - delivery in 2 days Available = false - delivery from 3 days to 2 months

func (*Offer) AddAge

func (o *Offer) AddAge(unit string, value string)

func (*Offer) AddBarcode

func (o *Offer) AddBarcode(barcode string)

func (*Offer) AddDeliveryOption

func (o *Offer) AddDeliveryOption(cost int, daysFrom, daysTo int, orderBefore int)

func (*Offer) AddParam

func (o *Offer) AddParam(name, unit, value string)

func (*Offer) AddPicture

func (o *Offer) AddPicture(pic string)

func (Offer) Validate

func (o Offer) Validate() error

Validate offer

type OfferType

type OfferType string
const (
	TypeVendorModel OfferType = "vendor.model"
)

type Offers

type Offers struct {
	Offers []Offer `xml:"offer"`
}

type Param

type Param struct {
	Name  string `xml:"name,attr"`
	Unit  string `xml:"unit,attr,omitempty"`
	Value string `xml:",chardata"`
}

type Shop

type Shop struct {
	Name            string           `xml:"name"`
	Company         string           `xml:"company"`
	Url             string           `xml:"url"`
	Platform        string           `xml:"platform,omitempty"`
	Version         string           `xml:"version,omitempty"`
	Agency          string           `xml:"agency,omitempty"`
	Email           string           `xml:"email,omitempty"`
	Currencies      Currencies       `xml:"currencies"`
	Categories      Categories       `xml:"categories"`
	DeliveryOptions *DeliveryOptions `xml:"delivery-options"`
	Cpa             int              `xml:"cpa,omitempty"`
	Offers          Offers           `xml:"offers"`
}

Jump to

Keyboard shortcuts

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