-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwykop.js
More file actions
68 lines (59 loc) · 1.85 KB
/
Copy pathwykop.js
File metadata and controls
68 lines (59 loc) · 1.85 KB
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
const axios = require('axios')
const qs = require('querystring')
const md5 = require('md5')
class Wykop {
constructor ({ secret, appKey }) {
this.secret = secret
this.appKey = appKey
this.userKey = null
}
createUrl (apiParams, namedParams) {
let joinedNamedParams = ''
const baseUrl = `https://a2.wykop.pl/${apiParams.join('/')}/appkey/${this.appKey}/userkey/${this.userKey}`
if (namedParams) {
joinedNamedParams = Object.entries(namedParams).map(([ key, value ]) => `${key}/${value}/`).join('/')
}
return `${baseUrl}${joinedNamedParams}`
}
createApiSign (requestUrl, postParams) {
const joinedPostParams = Object.keys(postParams).map(key => postParams[key]).join(',')
return md5(`${this.secret}${requestUrl}${joinedPostParams}`)
}
makeRequest ({ requestMethod, url, apiSign, postParams }) {
return axios({
method: requestMethod,
url: url,
headers: {
'apisign': apiSign,
'User-Agent': 'wykop-nodejs',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: qs.stringify(postParams)
})
}
request({ requestMethod, apiParams, namedParams, postParams }) {
return new Promise((resolve, reject) => {
const url = this.createUrl(apiParams, namedParams)
const apiSign = this.createApiSign(url, postParams)
const request = {
requestMethod: requestMethod,
url: url,
apiSign: apiSign,
postParams: postParams
}
this.makeRequest(request)
.then((res) => {
if (res.data.data === null) {
reject(res)
} else if (apiParams[0] === 'login') {
this.userKey = res.data.data.userkey
resolve(res.data)
} else {
resolve(res.data)
}
})
.catch((err) => reject(err))
})
}
}
module.exports = Wykop