(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"}