summaryrefslogtreecommitdiff
path: root/site/ozchai.fnl
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-09-26 17:46:38 +0600
committerunwox <me@unwox.com>2024-09-26 17:46:38 +0600
commit9b82db238f9e2e02a76f95c793f8d6ef2387ecfd (patch)
treecdb2a16d01f09553b560ab1034d53392d07bae42 /site/ozchai.fnl
init
Diffstat (limited to 'site/ozchai.fnl')
-rw-r--r--site/ozchai.fnl70
1 files changed, 70 insertions, 0 deletions
diff --git a/site/ozchai.fnl b/site/ozchai.fnl
new file mode 100644
index 0000000..90c4edc
--- /dev/null
+++ b/site/ozchai.fnl
@@ -0,0 +1,70 @@
+(import-macros {: map} :lib.macro)
+
+(local http (require :http))
+(local array (require :lib.array))
+(local json (require :vendor.json))
+
+(local %all-products-partuid 176163172341)
+
+(fn string->number [str]
+ (if str
+ (tonumber (pick-values 1 (str:gsub "[^0-9.]" "")))
+ nil))
+
+(fn request [partuid slice]
+ (print (.. "https://store.tildaapi.com/api/getproductslist/"
+ "?storepartuid="
+ partuid
+ "&recid=280779251&c=1723216515077"
+ "&getparts=true&getoptions=true&slice=%d&size=36"))
+ (let [(status headers body)
+ (luna.http.request
+ "GET"
+ (string.format
+ (.. "https://store.tildaapi.com/api/getproductslist/"
+ "?storepartuid="
+ partuid
+ "&recid=280779251&c=1723216515077"
+ "&getparts=true&getoptions=true&slice=%d&size=36")
+ slice)
+ {:Content-Type "application/json"
+ :User-Agent (http.random-user-agent)}
+ "")]
+ (json.decode body)))
+
+(fn walk-slices [partuid]
+ (fn gather [slice knil]
+ (let [{: nextslice : products} (request partuid slice)
+ res (array.concat knil products)]
+ (if (= 0 (# products))
+ knil
+ (do
+ (os.execute "sleep 1")
+ (gather (+ slice 1) res)))))
+ (gather 1 []))
+
+(fn normalize [_ product]
+ (local gallery (json.decode product.gallery))
+ (local weight (string->number (. (. product.editions 1) :Вес)))
+ (local price (string->number (. (. product.editions 1) :price)))
+
+ {:site "ozchai"
+ :id product.url
+ :url product.url
+ :title product.title
+ :description product.descr
+ ;; FIXME: parse all editions into different projects
+ :image (if (< 0 (# gallery))
+ (. (. gallery 1) :img)
+ "")
+ :weight weight
+ :price price
+ :price-per (if (and price weight (< 0 weight))
+ (/ (math.ceil (* (/ price weight) 10)) 10)
+ nil)
+ :characteristics product.characteristics})
+
+(fn products []
+ (map normalize (walk-slices %all-products-partuid)))
+
+{: products}