summaryrefslogtreecommitdiff
path: root/parser/ozchai.fnl
blob: 6bf62865528c64544319d11351217bba0887159b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(import-macros {: map} :lib.macro)

(local http (require :lib.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}