summaryrefslogtreecommitdiff
path: root/parser/moychay.fnl
blob: 565992e9300bbae6ea9c086abeef2d50653966a6 (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
(local peg
 (if (pick-values 1 (pcall require :lpeg))
   (require :lpeg)
   (require :lpeglj)))
(local number (require :lib.number))
(local fetcher (require :fetcher))
(local json (require :vendor.json))
(local parser (require :parser.parser))
(local libhtml (require :lib.html))

(fn extract-data [html]
 (libhtml.unescape
  (: (parser.anywhere
       (parser.tag
         :div
         {:id "app" :data-page (peg.Cg (parser.till "\""))}))
     :match html)))

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

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

(fn normalize [product]
  (local weight
    (if (~= product.measurement_unit "шт")
      (. product.variations 1 :weight)
      0))
  (local price (number.string->number (. product.variations 1 :price)))
  {:site "moychay"
   :url product.url
   :title product.name
   :archived (not product.available)
   :weight weight
   :price price
   :image (.. "https://moychay.ru" product.image)
   :url (.. "https://moychay.ru" product.url)
   :price-per (if (and price weight (< 0 weight))
               (/ (math.ceil (* (/ price weight) 10)) 10)
               nil)
   :characteristics product.characteristics})

(fn products []
  (fetcher.from-json
    [{:path "krasnyj_chaj" :tags ["Красный чай"]}
     {:path "belyj_chaj" :tags ["Белый чай"]}
     {:path "zheltyj_chaj" :tags ["Желтый чай"]}
     {:path "ulun/yuzhnofudzyanskij_ulun" :tags ["Улун" "Фудзянь"]}
     {:path "ulun/uishanskij_ulun" :tags ["Улун" "Фудзянь"]}
     {:path "ulun/guandunskij_ulun" :tags ["Улун" "Гуандун"]}
     {:path "ulun/tajvanskij_ulun" :tags ["Улун" "Тайвань"]}
     {:path "indian_tea" :tags ["Индийский чай"]}
     {:path "gruzinskij_chaj" :tags ["Грузинский чай"]}
     {:path "chernyj_cha" :tags ["Хэй ча"]}
     {:path "thailand-tea" :tags ["Тайландский чай"]}
     {:path "vietnamese-tea" :tags ["Вьетнамский чай"]}
     {:path "mate" :tags ["Йерба мате"]}
     {:path "posuda" :tags ["Посуда"]}
     {:path "puer/shu_puer_pressovannyj" :tags ["Шу пуэр"]}
     {:path "puer/shu_puer_rassypnoj" :tags ["Шу пуэр"]}
     {:path "puer/shen_puer_pressovannyj" :tags ["Шен пуэр"]}
     {:path "puer/shen_puer_rassypnoj" :tags ["Шен пуэр"]}
     {:path "puer/purpurnyj_shen_puer" :tags ["Шен пуэр"]}
     {:path "puer/belyj_puer" :tags ["Белый пуэр"]}
     {:path "jasmin" :tags ["Жасминовый чай"]}
     {:path "green_tea" :tags ["Зеленый чай"]}
     {:path "gaba-tea" :tags ["Улун" "Габа"]}]
    format-url
    destruct-response
    normalize))

{:products products :title "Мойчай.ру" :url "https://moychay.ru"}