-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.js
293 lines (245 loc) · 5.94 KB
/
engine.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
_KS = {
baseUrl: 'http://api.kopeechka.store',
strictMode: false,
debug: false,
storage: new _KStorageLocal(),
setDefaultSetting: function (ctxt) {
if (!ctxt || !Object.keys(ctxt).length) return
_KS.baseUrl = Boolean(ctxt.ssl)
? 'https://api.kopeechka.store'
: 'http://api.kopeechka.store'
_KS.debug = Boolean(ctxt.debug)
_KS.storage =
ctxt.storage === 'local' || !ctxt.storage
? new _KStorageLocal()
: ctxt.storage === 'global'
? new _KStorageGlobal()
: ctxt.storage === 'resource'
? new _KStorageresource()
: fail('Storage ' + _KS.storage + ' is not implemented')
if (ctxt.failErrors) {
_KS.tools.setErrors({
err: ctxt.failErrors,
errType: 'fail',
stop: Boolean(ctxt.failStop)
})
}
if (ctxt.dieErrors) {
_KS.tools.setErrors({
err: ctxt.dieErrors,
errType: 'die',
instantly: Boolean(ctxt.dieInstantly)
})
}
if (ctxt.ignoreErrors) _KS.tools.setErrors({ err: ctxt.ignoreErrors })
if (ctxt.applyGlobally) {
PSet('kopeechka', 'config', JSON.stringify(ctxt))
}
return
},
strict: function (ctxt) {
_KS.strictMode = Boolean(ctxt)
return
},
getBalance: function () {
var ctxt = _arguments()
var params = { token: ctxt.token }
var timeout = Number(ctxt.timeout) || 5000
var interval = Number(ctxt.interval) || 2000
var maxTime = Number(ctxt.maxTime) || 60000
var path = '/user-balance'
var schema = {
properties: {
balance: {
type: 'number'
}
},
required: ['balance']
}
_call_function(_KS.request.make, {
path: path,
params: params,
schema: schema,
timeout: timeout,
interval: interval,
maxTime: maxTime
})!
candidate = _result_function()
var response = candidate ? Number(candidate.balance) : null
_function_return(response)
},
getEmail: function () {
var ctxt = _arguments()
var params = _KS.tools.cleanObject(ctxt, [
'token',
'site',
'email',
'mail_type',
'sender',
'regex',
'investor',
'subject',
'clear',
'soft'
])
params.soft = params.soft || '7'
params.clear = Number(params.clear) || 0
params.investor = Number(params.investor) || 0
var timeout = Number(ctxt.timeout) || 15000
var interval = Number(ctxt.interval) || 3000
var maxTime = Number(ctxt.maxTime) || 60000
var path = params.email ? '/mailbox-reorder' : '/mailbox-get-email'
var schema = {
properties: {
id: {
type: 'string',
minLength: 1
},
mail: {
type: 'string',
minLength: 3
}
},
required: ['id', 'mail']
}
_call_function(_KS.request.make, {
path: path,
params: params,
schema: schema,
timeout: timeout,
interval: interval,
maxTime: maxTime
})!
candidate = _result_function()
var response = candidate ? candidate.mail : null
if (response) {
_KS.storage.set(response, {
id: candidate.id,
token: params.token
})
}
_function_return(response)
},
getMessage: function () {
var ctxt = _arguments()
var params = _KS.storage.get(ctxt.email)
var response = { parsed: null, full: null }
if (!params) {
_KS.tools.errorsHandler('RECORD_NOT_FOUND', ctxt.email)
_function_return(response)
}
params.full = Number(ctxt.full) || 0
var timeout = Number(ctxt.timeout) || 15000
var interval = Number(ctxt.interval) || 5000
var maxTime = Number(ctxt.maxTime) || 60000
var path = '/mailbox-get-message'
var schema = {
properties: {
value: {
type: 'string',
minLength: 1
},
fullmessage: {
type: 'string',
minLength: 1
}
},
required: [params.full ? 'fullmessage' : 'value']
}
_call_function(_KS.request.make, {
path: path,
params: params,
schema: schema,
timeout: timeout,
interval: interval,
maxTime: maxTime
})!
candidate = _result_function()
response = {
parsed: candidate && candidate.value ? candidate.value : null,
full:
candidate && candidate.fullmessage
? candidate.fullmessage
: null
}
_function_return(response)
},
closeEmail: function () {
var ctxt = _arguments()
var params = _KS.storage.get(ctxt.email)
if (!params) {
_KS.tools.errorsHandler('RECORD_NOT_FOUND', ctxt.email)
_function_return()
}
_KS.storage.remove(ctxt.email)
if (ctxt.fromServer) {
var path = '/mailbox-cancel'
var timeout = Number(ctxt.timeout) || 2000
var interval = Number(ctxt.interval) || 1000
var maxTime = Number(ctxt.maxTime) || 60000
_call_function(_KS.request.make, {
path: path,
params: params,
timeout: timeout,
interval: interval,
maxTime: maxTime
})!
}
_function_return()
},
getDomains: function () {
var ctxt = _arguments()
var domains = ctxt.domains
if (domains !== 'popular' && domains !== 'internal')
_function_return([])
var internalDomains = domains !== 'popular'
var params = internalDomains
? { token: ctxt.token, site: ctxt.site }
: { popular: 1 }
var minPrice = Number(ctxt.minPrice) || 0
var maxPrice = Number(ctxt.maxPrice) || 1000
var timeout = Number(ctxt.timeout) || 5000
var interval = Number(ctxt.interval) || 1000
var maxTime = Number(ctxt.maxTime) || 60000
var path = internalDomains ? '/mailbox-get-domains' : '/mailbox-zones'
var schema = internalDomains
? {
properties: {
domains: {
type: 'array'
}
},
required: ['domains']
}
: {
properties: {
popular: {
type: 'array'
}
},
required: ['popular']
}
_call_function(_KS.request.make, {
path: path,
params: params,
schema: schema,
timeout: timeout,
interval: interval,
maxTime: maxTime
})!
candidate = _result_function()
var response = candidate
? internalDomains
? candidate.domains
: candidate.popular
.filter(function (el) {
return el.cost >= minPrice && el.cost <= maxPrice
})
.map(function (el) {
return el.name
})
: null
_function_return(response)
}
}
_KS.setDefaultSetting(JSON.parse(P('kopeechka', 'config') || '""'))