This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalmart.go
executable file
·61 lines (55 loc) · 1.57 KB
/
walmart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package radardaoferta
import (
"encoding/json"
"errors"
"regexp"
"strconv"
"strings"
)
type WalmartProduto struct {
Product []struct {
ID int `json:"productId"`
Nome string `json:"productName"`
Valor string `json:"productPrice"`
Imagens string `json:"productImage"`
}
Link string
}
func WalmartParse(url string) (ProdutoGenerico, error) {
produto, err := WalmartGetInfo(url)
if err != nil {
return ProdutoGenerico{}, err
}
produto.Link = url
return WalmartDePara(produto), nil
}
func WalmartGetInfo(url string) (WalmartProduto, error) {
url = strings.Replace(url, "://", "://www.", 1)
body := string(RequestBody(url))
r, _ := regexp.Compile("\\<script\\>var dataLayer \\= \\[(.*?)\\]\\;dataLayer\\.push")
match := r.FindStringSubmatch(string(body))
if len(match) >= 2 {
produto := WalmartProduto{}
err := json.Unmarshal([]byte(match[1]), &produto)
if err != nil {
return WalmartProduto{}, err
}
return produto, nil
}
return WalmartProduto{}, errors.New("walmart nao identificou o javascript")
}
func WalmartDePara(w WalmartProduto) ProdutoGenerico {
p := ProdutoGenerico{}
p.IDProduto = strconv.Itoa(w.Product[0].ID)
p.Nome = w.Product[0].Nome
p.Created = TimeNowIso()
p.Loja = "walmart"
valor64, err := strconv.ParseFloat(w.Product[0].Valor, 32)
if err == nil {
p.Valor = float32(valor64)
}
imagem := "http://" + strings.Replace(w.Product[0].Imagens[2:], ".jpg", "/a.jpg", 1)
p.Imagens = []string{imagem}
p.Link = strings.Replace(w.Link, "://", "://www.", 1)
return p
}