diff options
| author | unwox <me@unwox.com> | 2024-09-27 15:26:33 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2024-09-27 15:44:16 +0600 |
| commit | dd449357f502dbe9ca4487d4b06a06ee4e597146 (patch) | |
| tree | 9847488a6cc2c1aaf1fc80578e1a7a5d4af99ff5 /parser/artoftea.fnl | |
| parent | 9b82db238f9e2e02a76f95c793f8d6ef2387ecfd (diff) | |
new structure
Diffstat (limited to 'parser/artoftea.fnl')
| -rw-r--r-- | parser/artoftea.fnl | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/parser/artoftea.fnl b/parser/artoftea.fnl new file mode 100644 index 0000000..1f03ed1 --- /dev/null +++ b/parser/artoftea.fnl @@ -0,0 +1,71 @@ +(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 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 "</a>") :title)))) + (parser.anywhere + (parser.tag :p {:class "description"} + (peg.Cg (parser.till "</p>") :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 "</span>") :price))))) + (parser.anywhere + (parser.tag :p {:class "price"} + (parser.tag :span {:id "*"} + (peg.Cg (parser.till "</span>") :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 (parser.guess-year 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 + [{:path "redtea" :category "Красный чай"} + {:path "greentea" :category "Зеленый чай"}] + url-formatter + product-peg + normalize)) + +{: products} |
