From 9b82db238f9e2e02a76f95c793f8d6ef2387ecfd Mon Sep 17 00:00:00 2001 From: unwox Date: Thu, 26 Sep 2024 17:46:38 +0600 Subject: init --- site/artoftea.fnl | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 site/artoftea.fnl (limited to 'site/artoftea.fnl') diff --git a/site/artoftea.fnl b/site/artoftea.fnl new file mode 100644 index 0000000..382b0ef --- /dev/null +++ b/site/artoftea.fnl @@ -0,0 +1,78 @@ +(import-macros {: map} :lib.macro) + +(local peg + (if (pick-values 1 (pcall require :lpeg)) + (require :lpeg) + (require :vendor.lpeglj))) +(local parser (require :parser)) +(local number (require :lib.number)) +(local fetcher (require :fetcher)) + +(fn url-formatter [path page] + (.. "https://artoftea.ru/" path "/?page=" page)) + +(local product-peg + (* ;; id + (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) " гр" 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 :input {:type "hidden" + :name "product_id" + :value (peg.Cg parser.pegs.number :id)})) + (parser.anywhere + (parser.tag :button {:type "*" :onclick "*" :class "*"} "Купить")))) + +(fn normalize [product] + (local year + (number.string->number + (: (parser.anywhere + (* (peg.C (^ (peg.R "09") 4)) + (parser.maybe " ") + (- "г" (peg.P "гр")))) + :match product.title))) + (local weight (number.string->number product.weight)) + (local price (number.string->number product.price)) + + {:site "artoftea" + :id product.id + :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 + url-formatter + [{:path "redtea" :category "Красный чай"} + {:path "greentea" :category "Зеленый чай"}] + normalize + product-peg)) + +{: products} -- cgit v1.2.3