blob: 50ec508f4a0193b8b70ae8a88e381fc4cd822ace (
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
(import-macros {: reduce} :lib.macro)
(local peg
(if (pick-values 1 (pcall require :lpeg))
(require :lpeg)
(require :lpeglj)))
(local parser (require :parser.parser))
(local number (require :lib.number))
(local fetcher (require :fetcher))
(fn html-cyrillic->utf [str]
(local replacement-map
{"А" "А"
"Б" "Б"
"В" "В"
"Г" "Г"
"Д" "Д"
"Е" "Е"
"Ж" "Ж"
"З" "З"
"И" "И"
"Й" "Й"
"К" "К"
"Л" "Л"
"М" "М"
"Н" "Н"
"О" "О"
"П" "П"
"Р" "Р"
"С" "С"
"Т" "Т"
"У" "У"
"Ф" "Ф"
"Х" "Х"
"Ц" "Ц"
"Ч" "Ч"
"Ш" "Ш"
"Щ" "Щ"
"Ъ" "Ъ"
"Ы" "Ы"
"Ь" "Ь"
"Э" "Э"
"Ю" "Ю"
"Я" "Я"
"а" "а"
"б" "б"
"в" "в"
"г" "г"
"д" "д"
"е" "е"
"ж" "ж"
"з" "з"
"и" "и"
"й" "й"
"к" "к"
"л" "л"
"м" "м"
"н" "н"
"о" "о"
"п" "п"
"р" "р"
"с" "с"
"т" "т"
"у" "у"
"ф" "ф"
"х" "х"
"ц" "ц"
"ч" "ч"
"ш" "ш"
"щ" "щ"
"ъ" "ъ"
"ы" "ы"
"ь" "ь"
"э" "э"
"ю" "ю"
"я" "я"})
(var result str)
(each [code letter (pairs replacement-map)]
(set result (: result :gsub code letter)))
result)
(fn format-url [path page]
(.. "https://daochai.ru/" path
"/" (if (< 1 page) (.. "page-" page "/") "")))
(local product-peg
(*
(parser.anywhere
(+
;; eager and lazy loaded versions of img
(parser.tag :img {:class "ty-pict cm-image" ;; FRAGILE
:src (peg.Cg (parser.till "\"") :image)
:id "*"
:title "*"
:alt "*"
:srcset "*"
:width "*"
:height "*"})
(parser.tag :img {:class "ty-pict cm-image" ;; FRAGILE
:src "*"
:data-src (peg.Cg (parser.till "\"") :image)
:id "*"
:title "*"
:alt "*"
:data-srcset "*"
:width "*"
:height "*"})))
(parser.anywhere
(parser.tag :a {:class "product-title"
:href (peg.Cg (parser.till "\"") :url)
:title "*"}
(peg.Cg (parser.till "</a>") :title)))
(+
(*
(parser.anywhere
(parser.tag :span {:class "ty-price-num" :id "*"}
(peg.Cg (parser.till "</span>") :price)))
(parser.anywhere
;; "за" and "гр" words are html-encoded for some reason
(* "за " (peg.Cg parser.pegs.number :weight) " гр.")))
(parser.anywhere
(parser.tag :span {:class "ty-price-num" :id "*"}
(peg.Cg (parser.till "</span>") :price))))
(parser.anywhere
;; "Купить"
(parser.tag :span {} "Купить"))))
(fn normalize [product]
(local title (html-cyrillic->utf product.title))
(local year (parser.guess-year title))
(local weight (number.string->number product.weight))
(local price (number.string->number product.price))
{:site "daochai"
:title title
:url product.url
:description nil
:image product.image
:year year
:price price
:weight weight
:volume (parser.guess-volume title)
:price-per (if (and price weight (< 0 weight))
(/ (math.ceil (* (/ price weight) 10)) 10)
nil)})
(fn products []
(fetcher.from-html
[{:path "vid-chaya/pu-erh/shu-puer" :tags ["Шу пуэр"]}
{:path "vid-chaya/pu-erh/shen" :tags ["Шен пуэр"]}
{:path "vid-chaya/ulun" :tags ["Улун"]}
{:path "vid-chaya/ulun/fudzjanskie-uluny" :tags ["Улун" "Фудзянь"]}
{:path "vid-chaya/ulun/guandunskie-uluny" :tags ["Улун" "Гуандун"]}
{:path "vid-chaya/ulun/uishanskie-uluny" :tags ["Улун" "Уишань"]}
{:path "vid-chaya/ulun/taiwan-ulun" :tags ["Улун" "Тайвань"]}
{:path "vid-chaya/ulun/yunnanskiy-uluny" :tags ["Улун" "Юннань"]}
{:path "vid-chaya/krasnyj-chaj" :tags ["Красный чай"]}
{:path "vid-chaya/zeljonyj-chaj" :tags ["Зеленый чай"]}
{:path "vid-chaya/white" :tags ["Белый чай"]}
{:path "vid-chaya/zheltyy-chay" :tags ["Желтый чай"]}
{:path "vid-chaya/heicha" :tags ["Хэй ча"]}
{:path "posuda/jianshuizitao" :tags ["Посуда" "Чайник"]}
{:path "posuda/nisintao" :tags ["Посуда" "Чайник"]}
{:path "posuda/chahu-taozi" :tags ["Посуда" "Чайник"]}
{:path "posuda/chayniki-iz-chaochzhou" :tags ["Посуда" "Чайник"]}
{:path "posuda/jingdezhen" :tags ["Посуда"]}
{:path "posuda/chahai" :tags ["Посуда"]}
{:path "posuda/gajvan" :tags ["Посуда"]}
{:path "posuda/chahaj" :tags ["Посуда"]}
{:path "posuda/chaban" :tags ["Посуда"]}
{:path "posuda/chajnye-prudy" :tags ["Посуда"]}
{:path "posuda/sito" :tags ["Посуда"]}
{:path "posuda/posuda-chajnoj-ceremonii" :tags ["Посуда"]}
{:path "posuda/termosy" :tags ["Посуда"]}
{:path "posuda/alternativa" :tags ["Посуда"]}
{:path "tea-accessorize" :tags ["Посуда"]}
{:path "chay-i-chan/aroma" :tags ["Благовония"]}
{:path "chay-i-chan/kurilnicy-i-podstavki-pod-blagovoniya" :tags ["Благовония"]}
{:path "chay-i-chan/chetki" :tags ["Четки"]}
{:path "chay-i-chan/dekorirovanie-prostranstva" :tags ["Декор"]}
{:path "chay-i-chan/figurki-iz-dereva" :tags ["Фигурки"]}]
format-url
product-peg
normalize))
{:products products :title "DaoChai" :url "https://daochai.ru"}
|