From 3a1b9d39ff109d9b7c92c0283e262e625a00a39a Mon Sep 17 00:00:00 2001 From: unwox Date: Sun, 25 May 2025 21:20:28 +0600 Subject: add batatcha parser --- bin/fetch.fnl | 7 ++++--- bin/serve.fnl | 2 +- parser/batatcha.fnl | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ parser/ozchai.fnl | 5 +---- static/batatcha.webp | Bin 0 -> 11208 bytes 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 parser/batatcha.fnl create mode 100644 static/batatcha.webp diff --git a/bin/fetch.fnl b/bin/fetch.fnl index 72d4c1f..794d147 100644 --- a/bin/fetch.fnl +++ b/bin/fetch.fnl @@ -9,6 +9,7 @@ (local {: must} (require :lib.utils)) (local artoftea (require :parser.artoftea)) +(local batatcha (require :parser.batatcha)) (local chaekshop (require :parser.chaekshop)) (local clubcha (require :parser.clubcha)) (local daochai (require :parser.daochai)) @@ -19,8 +20,8 @@ (local ozchai (require :parser.ozchai)) (local suhexuan (require :parser.suhexuan)) (local tea108 (require :parser.tea108)) -(local yoceramics (require :parser.yoceramics)) (local teaworkshop (require :parser.teaworkshop)) +(local yoceramics (require :parser.yoceramics)) (when _G.unpack (tset table :unpack _G.unpack)) @@ -193,9 +194,9 @@ FROM products;" [])) (must (luna.db.commit tx))) - (each [_ parser (pairs [yoceramics daochai gorkovchay moychay ozchai suhexuan + (each [_ parser (pairs [batatcha yoceramics daochai gorkovchay ozchai suhexuan ipuer artoftea clubcha chaekshop kolokolnikovchai - tea108 teaworkshop])] + tea108 teaworkshop moychay])] (local products (parser.products)) (when (< 0 (# products)) ;; replace with with-tx diff --git a/bin/serve.fnl b/bin/serve.fnl index 3b85110..0d742e3 100644 --- a/bin/serve.fnl +++ b/bin/serve.fnl @@ -468,7 +468,7 @@ [:option {:value val :selected (if (= form.site val) "selected" nil)} (. (require (.. "parser." val)) :title)]) - [:suhexuan :ozchai :kolokolnikovchai :yoceramics :tea108 + [:suhexuan :ozchai :batatcha :kolokolnikovchai :yoceramics :tea108 :ipuer :clubcha :daochai :chaekshop :teaworkshop :artoftea :moychay :gorkovchay]))]] [:div {} diff --git a/parser/batatcha.fnl b/parser/batatcha.fnl new file mode 100644 index 0000000..0d88ee0 --- /dev/null +++ b/parser/batatcha.fnl @@ -0,0 +1,56 @@ +(import-macros {: map} :lib.macro) + +(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://store.tildaapi.com/api/getproductslist/" + "?storepartuid=" path + "&slice=" page + "&recid=995758736" + "&c=1748185006948" + "&getparts=true" + "&getoptions=true" + "&size=36")) + +(fn destruct-response [response] + {:items (. (json.decode response) :products)}) + +(fn normalize [product] + (local gallery (json.decode product.gallery)) + (local weight (or (parser.guess-weight (. product.editions 1 :Вес) ["г"]) + (number.string->number (. product.editions 1 :Вес)))) + (local price (number.string->number (. product.editions 1 :price))) + (var volume (parser.guess-volume product.title)) + + {:site "batatcha" + :url product.url + :title product.title + :description product.descr + :image (if (< 0 (# gallery)) (. gallery 1 :img) "") + :weight weight + :volume volume + :price price + :price-per (if (and price weight (< 0 weight)) + (/ (math.ceil (* (/ price weight) 10)) 10) + nil) + :characteristics product.characteristics}) + +(fn products [] + (fetcher.from-json + [{:path "252209460212" :tags ["Посуда"]} + {:path "547479831241" :tags ["Шу пуэр"]} + {:path "826048744931" :tags ["Улун"]} + {:path "979435309971" :tags ["Красный чай"]} + {:path "770446922881" :tags ["Зеленый чай"]} + {:path "770679255051" :tags ["Белый чай"]} + {:path "596866221302" :tags ["Тизан"]}] + format-url + destruct-response + normalize)) + +{:products products :title "батат абрикос земля мята" :url "https://batatcha.com"} diff --git a/parser/ozchai.fnl b/parser/ozchai.fnl index e59ddf0..40ead94 100644 --- a/parser/ozchai.fnl +++ b/parser/ozchai.fnl @@ -34,10 +34,7 @@ :url product.url :title product.title :description product.descr - ;; FIXME: parse all editions into different products - :image (if (< 0 (# gallery)) - (. gallery 1 :img) - "") + :image (if (< 0 (# gallery)) (. gallery 1 :img) "") :weight weight :volume volume :price price diff --git a/static/batatcha.webp b/static/batatcha.webp new file mode 100644 index 0000000..194a4b5 Binary files /dev/null and b/static/batatcha.webp differ -- cgit v1.2.3