summaryrefslogtreecommitdiff
path: root/parser/clubcha.fnl
blob: 200be945760f7e2c00e72e6619094d90103fd62b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(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://clubcha.ru/catalog/" path "/?page=" page))

(local product-peg
  (*
   (parser.anywhere
    (parser.tag
     :a
     {:href (peg.Cg (parser.till "\"") :url)
      :class "*"
      :data-product (peg.Cg parser.pegs.number :id)}
     (*
      (parser.anywhere
       (parser.tag
         :img {:src (peg.Cg (parser.till "\"") :image)
               :alt "*"}))
      (parser.anywhere
       (parser.tag
         :span {:class "item__name"}
         (peg.Cg (parser.till "</span>") :title))))))
   (parser.anywhere
     (parser.tag :span {:class "js-prod-price"}
      (peg.Cg (^ (+ parser.pegs.number parser.pegs.spaces) 1) :price)))
   (parser.maybe
    (parser.anywhere
      (parser.tag
        :option
        {:data-price "*"
        :data-stock "*"
        :data-units "*"
        :data-oldprice "*"
        :value "*"}
        (peg.Cg (parser.till "</option>") :weight))))
   (parser.anywhere
    (parser.tag
      :button
      {:type "submit"
       :value "Купить"
       :data-added-text "В корзине"
       :class "*"}
      (parser.anywhere "Купить")))))

(fn normalize [product]
  (local weight (parser.guess-weight product.weight))
  (local price (number.string->number product.price))
  {:site "clubcha"
   :id product.id
   :title product.title
   :url (.. "https://clubcha.ru/" product.url)
   :description ""
   :image product.image
   :year 0
   :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 "shu-puer" :tags ["Шу пуэр"]}
     {:path "shen-puer" :tags ["Шен пуэр"]}
     {:path "bai-hao-puer"}
     {:path "hei-cha" :tags ["Хэй ча"]}
     {:path "yuzhnofutszyanskij-ulun" :tags ["Улун"]}
     {:path "guandunskij-ulun" :tags ["Улун"]}
     {:path "severofujianskiy-ulun" :tags ["Улун"]}
     {:path "taiwanskiy-ulun" :tags ["Улун"]}
     {:path "gaba-ulun-gaba-alishan-i-dr" :tags ["Улун"]}
     {:path "zelenyi-chay" :tags ["Зеленый чай"]}
     {:path "belyi-chay" :tags ["Белый чай"]}
     {:path "zheltyi-chay" :tags ["Желтый чай"]}
     {:path "kitajskij-krasnyj-chaj" :tags ["Красный чай"]}
     {:path "tajvanskij-krasnyj-chaj" :tags ["Красный чай"]}
     {:path "Nechaynyye_chai_i_tsvetochnyye_dobavki"}
     {:path "upakovka-dlya-puera"}
     {:path "upakovka-dlya-posudy"}
     {:path "iz-lichnoj-kollektsii"}
     ;; FIXME: expand this tags [to subcategories because the main one]
     ;; does not contain all the products
     {:path "gaivan" :tags ["Посуда"]}
     {:path "chashka" :tags ["Посуда"]}
     {:path "chaynyi-nabor" :tags ["Посуда"]}
     {:path "chaynik" :tags ["Посуда"]}
     {:path "glina-farfor" :tags ["Посуда"]}
     {:path "chaban-chajnaya-doska" :tags ["Посуда"]}
     {:path "chajnyj-podnos" :tags ["Посуда"]}
     {:path "chajnyj-prud" :tags ["Посуда"]}
     {:path "professionalnye" :tags ["Посуда"]}
     {:path "glinyanye" :tags ["Посуда"]}
     {:path "farforovye" :tags ["Посуда"]}
     {:path "zhestyanye-i-kartonnye" :tags ["Посуда"]}
     {:path "puernitsy" :tags ["Посуда"]}
     {:path "glina" :tags ["Посуда"]}
     {:path "farfor" :tags ["Посуда"]}
     {:path "steklo" :tags ["Посуда"]}
     {:path "kruzhka" :tags ["Посуда"]}
     {:path "Chaynyye_instrumenty" :tags ["Посуда"]}
     {:path "chahe" :tags ["Посуда"]}
     {:path "sito" :tags ["Посуда"]}
     {:path "Chaynyy_suvenir" :tags ["Посуда"]}
     {:path "Prochaya_utvar" :tags ["Посуда"]}
     {:path "farfor-eilong" :tags ["Посуда"]}
     {:path "butylka-dlya-zavarivaniya" :tags ["Посуда"]}
     {:path "Predmety_interyera_i_fenshuy"}
     {:path "fo-chzhu-chetki" :tags ["Четки"]}
     {:path "nefrit-1" :tags ["Нефрит"]}]
    format-url
    product-peg
    normalize))

{: products}