summaryrefslogtreecommitdiff
path: root/parser/ipuer.fnl
blob: 7a1ae6b52c1991083f5e2b2d4ce36d111f346c02 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(import-macros {: map} :lib.macro)

(local peg
 (if (pick-values 1 (pcall require :lpeg))
   (require :lpeg)
   (require :lpeglj)))
(local number (require :lib.number))
(local parser (require :parser.parser))
(local fetcher (require :fetcher))

(fn format-url [path page]
  (.. "https://ipuer.ru/catalog/" path "/?viewAs=table&p=" page))

(local product-peg
  (* ;; id
     (parser.anywhere
      (parser.tag :div {:data-id (peg.Cg parser.pegs.number :id) :class "*"}))
     ;; url and image
     (parser.anywhere
      (parser.tag :a {:href (peg.Cg (parser.till "\"") :url)}
       (parser.tag :img {:src (peg.Cg (parser.till "\"") :image) :alt "*"})))
     ;; title
     (parser.anywhere
      (parser.tag :div {:class "card-product_title"}
       (parser.tag :a {:href "*"}
        (parser.tag :span {} (peg.Cg (parser.till "</span>") :title)))))
     ;; description
     (+
      (parser.anywhere
       (parser.tag :span {:class "card-price_new"}
                   (* (peg.Cg
                         (* parser.pegs.number
                           (parser.maybe (* " " parser.pegs.number)))
                         :price)
                       " р.")))
      (*
       (parser.anywhere
         (parser.tag :div {:class "card-product_description"}
                     (peg.Cg (parser.till "</div>") :description)))
       (parser.anywhere
         (parser.tag :span {:class "card-price_new"}
                     (* (peg.Cg
                           (* parser.pegs.number
                             (parser.maybe (* " " parser.pegs.number)))
                           :price)
                         " р.")))))
     (parser.anywhere
       (+ (parser.tag :a {:data-url "*" :class "*" :data-add-text "*"} "В корзину")
          (parser.tag :a {:data-url "*" :class "*"} "В корзину")))))

(fn normalize [product]
  (local weight (parser.guess-weight product.title))
  (local price (number.string->number product.price))
  {:site "ipuer"
   :id product.id
   :url (.. "https://ipuer.ru" product.url)
   :title product.title
   :description product.description
   :image (.. "https://ipuer.ru" product.image)
   :year (parser.guess-year product.title)
   :price price
   :weight weight
   :category product.category
   :price-per (if (and price weight (< 0 weight))
                  (/ (math.ceil (* (/ price weight) 10)) 10)
                  nil)})

(fn products []
  (fetcher.from-html
    [{:path "shen-puer" :tags ["Шен пуэр"]}
     {:path "shu-puer" :tags ["Шу пуэр"]}
     {:path "drugoy-chay"}
     {:path "blagovoniya" :tags ["Благовония"]}
     {:path "posuda" :tags ["Посуда"]}
     {:path "282" :tags ["Посуда"]}]
    format-url
    product-peg
    normalize))

{:products products :title "Институт чая пуэр" :url "https://ipuer.ru"}