(import-macros {: map} :lib.macro) (local peg (if (pick-values 1 (pcall require :lpeg)) (require :lpeg) (require :lpeglj))) (local parser (require :parser.parser)) (local number (require :lib.number)) (local fetcher (require :fetcher)) (fn format-url [path page] (.. "https://chaekshop.ru/catalog/" path "?page=" page)) (local data-v-peg (* (peg.P "data-v-") (^ (+ parser.pegs.lat-letters parser.pegs.number) 1))) (local product-peg (* (parser.anywhere (parser.tag :a {data-v-peg "" :href (peg.Cg (parser.till "\"") :url) :class "*" :itemprop "url" :section_id "*"})) (parser.anywhere (parser.tag :img {:src (peg.Cg (parser.till "\"") :image) :alt "*" :title (peg.Cg (parser.till "\"") :title) :loading "lazy" data-v-peg ""})) (parser.anywhere (parser.tag :p {:itemprop "price" :class "*" data-v-peg ""} (* (peg.Cg (^ (+ parser.pegs.number " " ",") 1) :price) "₽"))) (parser.anywhere (parser.tag :p {:class "*" data-v-peg ""} (+ (* (peg.Cg parser.pegs.number :weight) " грамм") (* parser.pegs.number " шт")))))) (fn normalize [product] (local weight (or (number.string->number product.weight) (parser.guess-weight product.title))) (local price (number.string->number (string.gsub product.price "," "."))) {:site "chaekshop" :title product.title :url (.. "https://chaekshop.ru" product.url) :description "" :image (.. "https://chaekshop.ru" product.image) :year (parser.guess-year product.title) :price price :weight weight :volume (parser.guess-volume product.title) :price-per (if (and price weight (< 0 weight)) (/ (math.ceil (* (/ price weight) 10)) 10) nil)}) (fn products [] (fetcher.from-html [{:path "chay/belyy_chay" :tags ["Белый чай"]} {:path "chay/guandunskie_uluny" :tags ["Улун"]} {:path "chay/zhyeltyy_chay" :tags ["Желтый чай"]} {:path "chay/zelyenyy_chay" :tags ["Зеленый чай"]} {:path "chay/krasnyy_chay" :tags ["Красный чай"]} {:path "chay/severo_futszyanskie_uluny" :tags ["Улун" "Фудзянь"]} {:path "chay/tayvanskie_uluny" :tags ["Улун" "Тайвань"]} {:path "chay/khey_cha" :tags ["Хэй ча"]} {:path "chay/shen_puer" :tags ["Шен пуэр"]} {:path "chay/shu_puery" :tags ["Шу пуэр"]} {:path "chay/yuzhno_futszyanskie_uluny_" :tags ["Улун"]} {:path "chay/yunnanskie_uluny" :tags ["Улун"]} {:path "chay/gaba_chay" :tags ["Улун"]} {:path "chay/na_kazhdyy_den"} {:path "chay/eksklyuziv"} {:path "posuda_i_aksessuary/pialy" :tags ["Посуда"]} {:path "posuda_i_aksessuary/isinskaya_glina" :tags ["Посуда"]} {:path "posuda_i_aksessuary/keramika_iz_tszindechzhen" :tags ["Посуда"]} {:path "posuda_i_aksessuary/keramika_iz_tszyanshuy" :tags ["Посуда"]} {:path "posuda_i_aksessuary/kolby_termosy_nabory" :tags ["Посуда"]} {:path "posuda_i_aksessuary/chabani" :tags ["Посуда"]} {:path "posuda_i_aksessuary/chaynye_figurki" :tags ["Посуда"]} {:path "posuda_i_aksessuary/slivniki" :tags ["Посуда"]} {:path "posuda_i_aksessuary/gayvani" :tags ["Посуда"]} {:path "posuda_i_aksessuary/keramika_iz_tsinchzhou" :tags ["Посуда"]} {:path "posuda_i_aksessuary/aksessuary"} {:path "posuda_i_aksessuary/pialy_tszindechzhen" :tags ["Посуда"]} {:path "posuda_i_aksessuary/keramika_dekhua" :tags ["Посуда"]} {:path "posuda_i_aksessuary/chayniki_iz_farfora" :tags ["Посуда"]}] format-url product-peg normalize)) {:products products :title "Чаёк" :url "https://chaekshop.ru"}