(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://artoftea.ru/" path "/?page=" page)) (local product-peg (* (parser.anywhere (parser.tag :div {:class "front-image"} (parser.tag :a {:href (peg.Cg (parser.till "\"") :url)} (parser.tag :img {:src (peg.Cg (parser.till "\"") :image) :title "*" :class "*" :alt "*"})))) (parser.anywhere (parser.tag :div {:class "name"} (parser.tag :a {:href "*"} (peg.Cg (parser.till "") :title)))) (parser.anywhere (parser.tag :p {:class "description"} (peg.Cg (parser.till "

") :description))) (+ (* (parser.anywhere (parser.tag :option {:value "*" :selected "selected"} (* (peg.Cg parser.pegs.number :weight) (+ (peg.P "г") " гр") parser.pegs.spaces))) (parser.anywhere (parser.tag :p {:class "price"} (parser.tag :span {:id "*"} (peg.Cg (parser.till "") :price))))) (parser.anywhere (parser.tag :p {:class "price"} (parser.tag :span {:id "*"} (peg.Cg (parser.till "") :price))))) (parser.anywhere (parser.tag :button {:type "*" :onclick "*" :class "*"} "В корзину")))) (fn normalize [product] (local year (parser.guess-year product.title)) (local weight (number.string->number product.weight)) (local price (number.string->number product.price)) {:site "artoftea" :id product.id :title product.title :url product.url :description product.description :image product.image :year year :price price :weight weight :price-per (if (and price weight (< 0 weight)) (/ (math.ceil (* (/ price weight) 10)) 10) nil)}) (fn products [] (fetcher.from-html [{:path "redtea" :tags ["Красный чай"]} {:path "greentea" :tags ["Зеленый чай"]} {:path "puer/shu-puer" :tags ["Шу пуэр"]} {:path "puer/sheng-puer" :tags ["Шен пуэр"]} {:path "rassypnoi-puer"} {:path "oolong" :tags ["Улун"]} {:path "whitetea" :tags ["Белый чай"]} {:path "yellowtea" :tags ["Желтый чай"]} {:path "xej-cha-chernyj-chaj" :tags ["Хэй ча"]} {:path "posuda" :tags ["Посуда"]} {:path "tipoty-lightking" :tags ["Посуда"]} {:path "accesories" :tags ["Посуда"]} {:path "matcha-i-aksessuary" :tags ["Матча"]} {:path "upakovka" :tags ["Упаковка"]} {:path "eksklyuzivny-chay"} {:path "taiwan-tea" :tags ["Тайвань"]} {:path "sinij-chaj" :tags ["Синий чай"]}] format-url product-peg normalize)) {:products products :title "Art of tea" :url "https://artoftea.ru"}