blob: d575716d12309d3d4bcd43285815a1f90de71084 (
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
|
(import-macros {: map} :lib.macro)
(local peg
(if (pick-values 1 (pcall require :lpeg))
(require :lpeg)
(require :lpeglj)))
(local number (require :lib.number))
(local parser (require :parser.parser))
(local fetcher (require :fetcher))
(local utils (require :lib.utils))
(fn format-url [path page]
(.. "https://tea108.ru/shop/" path "/?page=" page))
(local product-peg
(*
;; a delimiter to clearly separate products
(parser.anywhere "data-productid=")
;; url
(parser.anywhere
(parser.tag :a {:href (peg.Cg (parser.till "\"") :url)
:class "product js--hover-preview"}))
;; image
(parser.anywhere (* "data-src=\""
(peg.Cg (parser.till "\"") :image)
"\""))
;; name
(parser.anywhere
(parser.tag :div {:class "product-name"}
(peg.Cg (parser.till "</div>") :title)))
;; price
(parser.anywhere
(parser.tag :span {:class (* "product-price-min" (parser.till "\""))}
(peg.Cg (parser.till "</span>") :price)))))
(fn normalize [product]
(local weight (parser.guess-weight product.title [(* "г" (parser.not "."))]))
(local price (number.string->number product.price))
{:site "tea108"
:url product.url
:title product.title
:description product.description
:image (.. "https:" product.image)
:price price
:weight weight
:volume (parser.guess-volume product.title)
:category product.category
:price-per (if (and price weight (< 0 weight))
(/ (math.ceil (* (/ price weight) 10)) 10)
nil)})
(fn products []
(fetcher.from-html
[{:path "wu-yi-yan-cha" :tags ["Улун" "Фуцзянь"]}
{:path "feng-huang-dancong" :tags ["Улун" "Дань Цун"]}
{:path "taiwan-oolong" :tags ["Улун" "Тайвань"]}
{:path "hong-cha" :tags ["Красный чай"]}
{:path "bai-cha" :tags ["Белый чай"]}
{:path "sheng" :tags ["Шен пуэр"]}
{:path "shu-puer" :tags ["Шу пуэр"]}
{:path "tea-ware" :tags ["Посуда"]}]
format-url
product-peg
normalize))
{:products products :title "Tea108" :url "https://tea108.ru"}
|