support parsing item page info
This commit is contained in:
@ -11,12 +11,14 @@ import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.loyso.art/frx/eway/internal/config"
|
||||
"git.loyso.art/frx/eway/internal/crypto"
|
||||
"git.loyso.art/frx/eway/internal/entity"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/gocolly/colly"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
@ -26,6 +28,7 @@ type Client interface {
|
||||
context.Context,
|
||||
GetGoodsNewParams,
|
||||
) (items []entity.GoodsItemRaw, total int, err error)
|
||||
GetProductInfo(context.Context, int64) (entity.GoodsItemInfo, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
@ -40,7 +43,6 @@ type Config config.Eway
|
||||
func New(ctx context.Context, cfg Config, log zerolog.Logger) (client, error) {
|
||||
httpclient := resty.New().
|
||||
SetDebug(cfg.Debug).
|
||||
// SetCookies(cookies).
|
||||
SetBaseURL("https://eway.elevel.ru/api")
|
||||
|
||||
c := client{
|
||||
@ -269,3 +271,64 @@ func (c client) login(ctx context.Context, user, pass string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ProductInfo struct {
|
||||
ImageLinks []string
|
||||
Parameters map[string]string
|
||||
}
|
||||
|
||||
type parameterSelector struct {
|
||||
Name string `selector:"div"`
|
||||
Value string `selector:"div.text-right"`
|
||||
}
|
||||
|
||||
func (c client) GetProductInfo(ctx context.Context, cart int64) (pi entity.GoodsItemInfo, err error) {
|
||||
collector := colly.NewCollector(
|
||||
colly.AllowedDomains("eway.elevel.ru"),
|
||||
colly.AllowURLRevisit(),
|
||||
)
|
||||
|
||||
pi.Parameters = map[string]string{}
|
||||
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
elapsed := time.Since(start).Seconds()
|
||||
c.log.Info().Float64("elapsed", elapsed).Msg("request processed")
|
||||
}()
|
||||
|
||||
collector.OnHTML("body > div.page-container > div.page-content > div.content-wrapper > div.content > div.row > div.col-md-4 > div > div > div:nth-child(6)", func(e *colly.HTMLElement) {
|
||||
e.ForEach("div.display-flex", func(i int, h *colly.HTMLElement) {
|
||||
var s parameterSelector
|
||||
err = h.Unmarshal(&s)
|
||||
if err != nil {
|
||||
c.log.Warn().Err(err).Msg("unable to unmarshal")
|
||||
return
|
||||
}
|
||||
|
||||
if s.Name == "" || s.Value == "" {
|
||||
c.log.Warn().Msg("got empty key or value, skipping")
|
||||
return
|
||||
}
|
||||
|
||||
pi.Parameters[s.Name] = s.Value
|
||||
})
|
||||
})
|
||||
collector.OnHTML("div.gallery_panel", func(h *colly.HTMLElement) {
|
||||
h.ForEach("div.gallery_thumbnail > img", func(i int, h *colly.HTMLElement) {
|
||||
imageURL := h.Attr("src")
|
||||
|
||||
if imageURL == "" {
|
||||
return
|
||||
}
|
||||
|
||||
pi.PhotoURLs = append(pi.PhotoURLs, imageURL)
|
||||
})
|
||||
})
|
||||
|
||||
err = collector.Visit("https://eway.elevel.ru/product/" + strconv.Itoa(int(cart)) + "/")
|
||||
if err != nil {
|
||||
return pi, fmt.Errorf("visiting site: %w", err)
|
||||
}
|
||||
|
||||
return pi, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user