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

(local fennel (require :vendor.fennel))
(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://api.teaworkshop.ru/shop-api/filters/popular/" path
      "?limit=40&page=" page))

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

(fn normalize [product]
  (local image (?. product :images 1 :cachedPath))
  (var variant nil)
  (var weight nil)

  ;; choosing a variant with parseable weight and tracked = 0 (meaning the
  ;; variant is in stock)
  (each [_ v (pairs product.variants) &until variant]
    (when (= v.tracked 0)
      (each [_ axis (pairs v.nameAxis) &until weight]
        (set weight (parser.guess-weight axis ["г"]))
        (when weight
          (set variant v)))))

  ;; if there are no variants with parsable weight: choose the first one with
  ;; tracked == 0
  (when (not variant)
    (each [_ v (pairs product.variants) &until variant]
      (when (= v.tracked 0)
        (set variant v))))

  (when variant
    {:site "teaworkshop"
     :url (.. "https://teaworkshop.ru/product/" product.slug)
     :title product.name
     :description nil
     :image image
     :weight weight
     :volume (parser.guess-volume product.name)
     :price (/ variant.price.current 100)
     :price-per (if (and variant.price.current weight (< 0 weight))
                  (/ (math.ceil (* (/ variant.price.current weight 100) 10)) 10)
                  nil)}))

(fn products []
  (fetcher.from-json
    [{:path "shu-pujer-chernyj" :tags ["Шу пуэр"]}
     {:path "shen-puer" :tags ["Шен пуэр"]}
     {:path "red" :tags ["Красный чай"]}
     {:path "zheltyi-chai" :tags ["Желтый чай"]}
     {:path "ansi" :tags ["Улун" "Фудзянь" "Аньси"]}
     {:path "guandun" :tags ["Улун" "Гуандун"]}
     {:path "tajvan" :tags ["Улун" "Тайвань"]}
     {:path "uishan" :tags ["Улун" "Фудзянь" "Уишань"]}
     {:path "ulun" :tags ["Улун"]}
     {:path "green" :tags ["Зеленый чай"]}
     {:path "white" :tags ["Белый чай"]}
     {:path "hei-cha-chiornyi-chai" :tags ["Хэй ча"]}
     {:path "nabori" :tags ["Набор"]}
     {:path "avtorskii-chai" :tags ["Авторский чай"]}
     {:path "gaba" :tags ["Габа"]}
     {:path "tailand-tea" :tags ["Тайланд"]}
     {:path "konkursnyi-chai" :tags ["Конкурсный чай"]}
     {:path "krasnodar-chai" :tags ["Краснодар"]}
     {:path "clay-teapot" :tags ["Посуда" "Чайник" "Исин"]}
     {:path "chainiki-keramika-tszindechzhen" :tags ["Посуда" "Цзиндэчжэнь"]}
     {:path "gaiwan" :tags ["Посуда" "Гайвань"]}
     {:path "easy-teapot" :tags ["Посуда" "Типот"]}
     {:path "cinchzhouskaja-keramika-nisin-tao" :tags ["Посуда" "Чайник" "Гуанси"]}
     {:path "keramika-dehua" :tags ["Посуда" "Керамика" "Дэхуа"]}
     {:path "czjanshujskaja-keramika" :tags ["Посуда" "Керамика" "Цзяньшуй"]}
     {:path "chajniki-keramika" :tags ["Посуда" "Чайник"]}
     {:path "glass-teapot" :tags ["Посуда" "Чайник" "Стекло"]}
     {:path "chashki" :tags ["Посуда" "Пиала"]}
     {:path "chaxaj-slivnik" :tags ["Посуда" "Чахай"]}
     {:path "chabani" :tags ["Посуда" "Чабань"]}
     {:path "prinadlezhnosti" :tags ["Аксессуар"]}
     {:path "tea-spirit" :tags ["Фигурка"]}
     {:path "banochki" :tags ["Посуда" "Чайница"]}
     {:path "nabori-stuff" :tags ["Посуда" "Набор"]}]
    format-url
    destruct-response
    normalize))

{:products products :title "Чайная мастерская" :url "https://teaworkshop.ru"}