summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-12-11 20:41:29 +0600
committerunwox <me@unwox.com>2024-12-16 21:20:55 +0600
commit155c9c5bbb703490e5d61f7b4504987e35088a95 (patch)
tree643995b9b90c3759888cfd9b3100d871c0ccfb63
parente35dbf50a93ba734def634317a09211654974d42 (diff)
integrate moychay.ru tea shop
-rw-r--r--bin/fetch.fnl13
-rw-r--r--bin/serve.fnl16
-rw-r--r--fetcher.fnl3
-rw-r--r--lib/html.fnl10
-rw-r--r--parser/artoftea.fnl2
-rw-r--r--parser/chaekshop.fnl2
-rw-r--r--parser/clubcha.fnl2
-rw-r--r--parser/ipuer.fnl2
-rw-r--r--parser/moychay.fnl73
-rw-r--r--parser/ozchai.fnl4
-rw-r--r--static/moychay.pngbin0 -> 1967 bytes
11 files changed, 100 insertions, 27 deletions
diff --git a/bin/fetch.fnl b/bin/fetch.fnl
index a3155a4..006cbea 100644
--- a/bin/fetch.fnl
+++ b/bin/fetch.fnl
@@ -6,6 +6,7 @@
(local array (require :lib.array))
(local cache (require :lib.cache))
+(local moychay (require :parser.moychay))
(local ozchai (require :parser.ozchai))
(local ipuer (require :parser.ipuer))
(local artoftea (require :parser.artoftea))
@@ -101,7 +102,7 @@
(.. "INSERT OR REPLACE INTO products VALUES "
(array.join
(map (fn [_ _] "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
- products)
+ products)
", ")))
(local creation-time (now))
(local vars
@@ -117,7 +118,7 @@
(or product.price 0)
(or product.weight 0)
(or product.price-per 0)
- false
+ (or product.archived false)
creation-time]))
products []))
(must (luna.db.exec-tx tx sql vars))
@@ -150,12 +151,8 @@
;; replace with with-tx
(local tx (must (luna.db.begin db)))
(must (luna.db.exec-tx tx "DELETE FROM product_tags;" []))
-(each [_ products (pairs [chaekshop.products
- clubcha.products
- artoftea.products
- ipuer.products
- ozchai.products])]
- (store-products tx (products)))
+(each [_ parser (pairs [chaekshop clubcha artoftea ipuer ozchai moychay])]
+ (store-products tx (parser.products)))
(cache.clear-tx tx "page:")
(must (luna.db.commit tx))
diff --git a/bin/serve.fnl b/bin/serve.fnl
index 5cfc535..9329bf1 100644
--- a/bin/serve.fnl
+++ b/bin/serve.fnl
@@ -5,6 +5,7 @@
(local math (require :math))
(local fennel (require :vendor.fennel))
(local html (require :vendor.html))
+(local libhtml (require :lib.html))
(local json (require :vendor.json))
(local array (require :lib.array))
(local str (require :lib.string))
@@ -57,15 +58,6 @@
"термос" "бутылка"
"бутылка" "термос"})
-(fn unescape [s]
- (assert (= (type s) :string) "s must be string")
- (pick-values 1
- (-> s
- (string.gsub "&lt;" "<")
- (string.gsub "&gt;" ">")
- (string.gsub "&quot;" "\"")
- (string.gsub "&amp;" "&"))))
-
(fn sanitize-input [input]
(if input
(str.trim (input:gsub "[=()<>']" "") " ")
@@ -333,7 +325,9 @@
:alt product.title} ""]]
(site-name-template product.site)
[:a {:href link :style "text-decoration: none;" :rel "nofollow"}
- [:NO-ESCAPE (.. "<div class=\"title\">" (unescape product.title) "</div>")]]
+ [:NO-ESCAPE (.. "<div class=\"title\">"
+ (libhtml.unescape product.title)
+ "</div>")]]
[:div {:class "price"}
(if product.price (.. (str.format-price product.price) "₽") "")
(if (< 0 product.weight)
@@ -447,7 +441,7 @@
[:option {:value val
:selected (if (= form.site val) "selected" nil)}
(. (require (.. "parser." val)) :title)])
- [:ozchai :clubcha :ipuer :artoftea :chaekshop]))]]
+ [:ozchai :clubcha :ipuer :artoftea :chaekshop :moychay]))]]
[:div {}
[:select {:name "sort"}
[:option {:value ""} "~ Порядок ~"]
diff --git a/fetcher.fnl b/fetcher.fnl
index 617bbfc..7b46266 100644
--- a/fetcher.fnl
+++ b/fetcher.fnl
@@ -5,7 +5,6 @@
(require :lpeg)
(require :lpeglj)))
(local array (require :lib.array))
-(local json (require :vendor.json))
(local parser (require :parser.parser))
(local http (require :lib.http))
(local {: must} (require :lib.utils))
@@ -87,7 +86,7 @@
"")))
(if (= status 200)
- (let [{: items} (response-destructor (json.decode body))]
+ (let [{: items} (response-destructor body)]
(if (or (= items nil) (= 0 (# items)))
knil
(do
diff --git a/lib/html.fnl b/lib/html.fnl
new file mode 100644
index 0000000..9cf7b3a
--- /dev/null
+++ b/lib/html.fnl
@@ -0,0 +1,10 @@
+(fn unescape [s]
+ (assert (= (type s) :string) "s must be string")
+ (pick-values 1
+ (-> s
+ (string.gsub "&lt;" "<")
+ (string.gsub "&gt;" ">")
+ (string.gsub "&quot;" "\"")
+ (string.gsub "&amp;" "&"))))
+
+{ : unescape }
diff --git a/parser/artoftea.fnl b/parser/artoftea.fnl
index a62cd34..83ff0ea 100644
--- a/parser/artoftea.fnl
+++ b/parser/artoftea.fnl
@@ -81,4 +81,4 @@
product-peg
normalize))
-{: products :title "Art of tea" :url "https://artoftea.ru"}
+{:products products :title "Art of tea" :url "https://artoftea.ru"}
diff --git a/parser/chaekshop.fnl b/parser/chaekshop.fnl
index 80e6cd5..68046c1 100644
--- a/parser/chaekshop.fnl
+++ b/parser/chaekshop.fnl
@@ -85,4 +85,4 @@
product-peg
normalize))
-{: products :title "Чаёк" :url "https://chaekshop.ru"}
+{:products products :title "Чаёк" :url "https://chaekshop.ru"}
diff --git a/parser/clubcha.fnl b/parser/clubcha.fnl
index 57334a7..23f9484 100644
--- a/parser/clubcha.fnl
+++ b/parser/clubcha.fnl
@@ -127,4 +127,4 @@
product-peg
normalize))
-{: products :title "Железный Феникс" :url "https://clubcha.ru"}
+{:products products :title "Железный Феникс" :url "https://clubcha.ru"}
diff --git a/parser/ipuer.fnl b/parser/ipuer.fnl
index b501a35..7a1ae6b 100644
--- a/parser/ipuer.fnl
+++ b/parser/ipuer.fnl
@@ -77,4 +77,4 @@
product-peg
normalize))
-{: products :title "Институт чая пуэр" :url "https://ipuer.ru"}
+{:products products :title "Институт чая пуэр" :url "https://ipuer.ru"}
diff --git a/parser/moychay.fnl b/parser/moychay.fnl
new file mode 100644
index 0000000..565992e
--- /dev/null
+++ b/parser/moychay.fnl
@@ -0,0 +1,73 @@
+(local peg
+ (if (pick-values 1 (pcall require :lpeg))
+ (require :lpeg)
+ (require :lpeglj)))
+(local number (require :lib.number))
+(local fetcher (require :fetcher))
+(local json (require :vendor.json))
+(local parser (require :parser.parser))
+(local libhtml (require :lib.html))
+
+(fn extract-data [html]
+ (libhtml.unescape
+ (: (parser.anywhere
+ (parser.tag
+ :div
+ {:id "app" :data-page (peg.Cg (parser.till "\""))}))
+ :match html)))
+
+(fn format-url [path page]
+ (.. "https://moychay.ru/catalog/" path "?page=" (tostring page)))
+
+(fn destruct-response [response]
+ {:items (. (json.decode (extract-data response)) :props :catalog :products)})
+
+(fn normalize [product]
+ (local weight
+ (if (~= product.measurement_unit "шт")
+ (. product.variations 1 :weight)
+ 0))
+ (local price (number.string->number (. product.variations 1 :price)))
+ {:site "moychay"
+ :url product.url
+ :title product.name
+ :archived (not product.available)
+ :weight weight
+ :price price
+ :image (.. "https://moychay.ru" product.image)
+ :url (.. "https://moychay.ru" product.url)
+ :price-per (if (and price weight (< 0 weight))
+ (/ (math.ceil (* (/ price weight) 10)) 10)
+ nil)
+ :characteristics product.characteristics})
+
+(fn products []
+ (fetcher.from-json
+ [{:path "krasnyj_chaj" :tags ["Красный чай"]}
+ {:path "belyj_chaj" :tags ["Белый чай"]}
+ {:path "zheltyj_chaj" :tags ["Желтый чай"]}
+ {:path "ulun/yuzhnofudzyanskij_ulun" :tags ["Улун" "Фудзянь"]}
+ {:path "ulun/uishanskij_ulun" :tags ["Улун" "Фудзянь"]}
+ {:path "ulun/guandunskij_ulun" :tags ["Улун" "Гуандун"]}
+ {:path "ulun/tajvanskij_ulun" :tags ["Улун" "Тайвань"]}
+ {:path "indian_tea" :tags ["Индийский чай"]}
+ {:path "gruzinskij_chaj" :tags ["Грузинский чай"]}
+ {:path "chernyj_cha" :tags ["Хэй ча"]}
+ {:path "thailand-tea" :tags ["Тайландский чай"]}
+ {:path "vietnamese-tea" :tags ["Вьетнамский чай"]}
+ {:path "mate" :tags ["Йерба мате"]}
+ {:path "posuda" :tags ["Посуда"]}
+ {:path "puer/shu_puer_pressovannyj" :tags ["Шу пуэр"]}
+ {:path "puer/shu_puer_rassypnoj" :tags ["Шу пуэр"]}
+ {:path "puer/shen_puer_pressovannyj" :tags ["Шен пуэр"]}
+ {:path "puer/shen_puer_rassypnoj" :tags ["Шен пуэр"]}
+ {:path "puer/purpurnyj_shen_puer" :tags ["Шен пуэр"]}
+ {:path "puer/belyj_puer" :tags ["Белый пуэр"]}
+ {:path "jasmin" :tags ["Жасминовый чай"]}
+ {:path "green_tea" :tags ["Зеленый чай"]}
+ {:path "gaba-tea" :tags ["Улун" "Габа"]}]
+ format-url
+ destruct-response
+ normalize))
+
+{:products products :title "Мойчай.ру" :url "https://moychay.ru"}
diff --git a/parser/ozchai.fnl b/parser/ozchai.fnl
index 9ccce5f..16a3265 100644
--- a/parser/ozchai.fnl
+++ b/parser/ozchai.fnl
@@ -17,7 +17,7 @@
"&size=36"))
(fn destruct-response [response]
- {:items response.products})
+ {:items (. (json.decode response) :products)})
(fn normalize [product]
(local gallery (json.decode product.gallery))
@@ -54,4 +54,4 @@
destruct-response
normalize))
-{: products :title "Чайная #OZCHAI" :url "https://ozchai.ru"}
+{:products products :title "Чайная #OZCHAI" :url "https://ozchai.ru"}
diff --git a/static/moychay.png b/static/moychay.png
new file mode 100644
index 0000000..e1ca82b
--- /dev/null
+++ b/static/moychay.png
Binary files differ