summaryrefslogtreecommitdiff
path: root/parser/artoftea.fnl
blob: 1b8b6e12fb153c0de6ddbd5c94391a096bbd4621 (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
81
82
83
84
85
86
87
88
(import-macros {: map} :lib.macro)

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

(fn format-url [path page]
  (.. "https://artoftea.ru/" path "/?page=" page))

(local product-peg
  (*
   (parser.anywhere
    (parser.tag :div {:class "front-image"}
     (parser.tag :a {:href (peg.Cg (parser.till "\"") :url)}
      (parser.tag :img {:src (peg.Cg (parser.till "\"") :image)
                        :title "*" :class "*" :alt "*"}))))
   (parser.anywhere
    (parser.tag :div {:class "name"}
     (parser.tag :a {:href "*"} (peg.Cg (parser.till "</a>") :title))))
   (parser.anywhere
    (parser.tag :p {:class "description"}
     (peg.Cg (parser.till "</p>") :description)))
   (+
    (*
     (parser.anywhere
      (parser.tag :option {:value "*" :selected "selected"}
        (* (peg.Cg parser.pegs.number :weight) (+ (peg.P "г") " гр")
           parser.pegs.spaces)))
     (parser.anywhere
      (parser.tag :p {:class "price"}
       (parser.tag :span {:id "*"}
       (peg.Cg (parser.till "</span>") :price)))))
    (parser.anywhere
     (parser.tag :p {:class "price"}
      (parser.tag :span {:id "*"}
      (peg.Cg (parser.till "</span>") :price)))))
   (parser.anywhere
    (parser.tag :input {:type "hidden"
                        :name "product_id"
                        :value (peg.Cg parser.pegs.number :id)}))
   (parser.anywhere
     (parser.tag :button {:type "*" :onclick "*" :class "*"} "Купить"))))

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

(fn products []
  (fetcher.from-html
    [{:path "redtea" :category "Красный чай"}
     {:path "greentea" :category "Зеленый чай"}
     {:path "puer/shu-puer" :category "Шу пуэр"}
     {:path "puer/sheng-puer" :category "Шен пуэр"}
     {:path "rassypnoi-puer"}
     {:path "oolong" :category "Улун"}
     {:path "whitetea" :category "Белый чай"}
     {:path "yellowtea" :category "Желтый чай"}
     {:path "xej-cha-chernyj-chaj" :category "Хэй ча"}
     {:path "posuda" :category "Посуда"}
     {:path "tipoty-lightking" :category "Посуда"}
     {:path "accesories" :category "Аксессуары"}
     {:path "matcha-i-aksessuary"}
     {:path "upakovka"}
     {:path "eksklyuzivny-chay"}
     {:path "taiwan-tea"}
     {:path "sinij-chaj"}]
    format-url
    product-peg
    normalize))

{: products}