summaryrefslogtreecommitdiff
path: root/parser/kolokolnikovchai.fnl
blob: acf52c5cea902c6c3050248532f9be93191a2e5a (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
(import-macros {: map} :lib.macro)

(local parser (require :parser.parser))
(local array (require :lib.array))
(local http (require :lib.http))
(local number (require :lib.number))
(local fetcher (require :fetcher))
(local json (require :vendor.json))

(fn format-url [path page]
  (.. "https://store.tildaapi.com/api/getproductslist/"
      "?storepartuid=" path
      "&slice=" page
      "&recid=280779251"
      "&c=1723216515077"
      "&getparts=true"
      "&getoptions=true"
      "&size=36"))

(fn destruct-response [response]
  {:items (. (json.decode response) :products)})

(fn normalize [product]
  (var weight nil)

  (when product.characteristics
    (each [_ c (ipairs product.characteristics)]
      (when (= (. c :title) "Вес")
        (set weight (parser.guess-weight c.value ["г"])))))

  (when (and product.properties (not weight))
    (each [_ c (ipairs product.properties)]
      (when (= (. c :title) "Выберите вес:") ;; FRAGILE!
        (set weight (parser.guess-weight c.values ["г"])))))

  (when (~= product.quantity "0")
    (local gallery (json.decode product.gallery))
    (local price (number.string->number (. product.editions 1 :price)))

    {:site "kolokolnikovchai"
     :url product.url
     :title product.title
     :description product.descr
     ;; FIXME: parse all editions into different products
     :image (if (< 0 (# gallery))
              (. gallery 1 :img)
              "")
     :weight weight
     :volume (or (parser.guess-volume product.descr)
                 (parser.guess-volume product.text))
     :price price
     :price-per (if (and price weight (< 0 weight))
                 (/ (math.ceil (* (/ price weight) 10)) 10)
                 nil)
     :characteristics product.characteristics}))

(fn products []
  (fetcher.from-json
    [{:path "409075863661" :tags ["Красный чай"]}
     {:path "754089666541" :tags ["Красный чай"]}
     {:path "372064040221" :tags ["Шу пуэр"]}
     {:path "454350410711" :tags ["Хэй ча"]}
     {:path "841704512611" :tags ["Шен пуэр"]}
     {:path "848263867181" :tags ["Белый чай"]}
     {:path "990406163191" :tags ["Зеленый чай"]}
     {:path "451142994821" :tags ["Улун"]}
     {:path "770906787081" :tags ["Габа"]}
     {:path "909586139961" :tags ["Посуда" "Чайник"]}
     {:path "351075619951" :tags ["Посуда" "Гайвань"]}
     {:path "874916565001" :tags ["Посуда" "Пиала"]}
     {:path "927453990401" :tags ["Посуда" "Чахай"]}
     {:path "242398612931" :tags ["Посуда" "Чабань"]}
     {:path "208270710991" :tags ["Посуда" "Аксессуар"]}
     {:path "542771614531" :tags ["Посуда" "Заварник"]}]
    format-url
    destruct-response
    normalize))

{:products products :title "Колокольников Чай" :url "https://kolokolnikovchai.ru"}