diff options
Diffstat (limited to 'parser/moychay.fnl')
| -rw-r--r-- | parser/moychay.fnl | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/parser/moychay.fnl b/parser/moychay.fnl new file mode 100644 index 0000000..565992e --- /dev/null +++ b/parser/moychay.fnl @@ -0,0 +1,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"} |
