-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (76 loc) · 2.58 KB
/
app.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
const request = require('request')
const argv = require('yargs').argv
let userToken = argv.token
let nocollect = !!argv.nocollect
if (userToken === undefined)
throw Error('No valid token provided')
let getActivePoints = () => {
const options = {
url: 'https://api.nasapatrola.com/Points/GetActivePoints',
headers: { Authorization: userToken }
}
return new Promise((resolve, reject) => {
request.get(options, (err, resp, body) => {
if (err) reject(err)
else resolve(body)
})
})
}
let collectPoints = id => {
const options = {
url: 'https://api.nasapatrola.com/Points/CollectPoint',
headers: { Authorization: userToken },
body: JSON.stringify({id})
}
return new Promise((resolve, reject) => {
request.post(options, (err, resp, body) => {
if (err) reject(err)
else resolve(body)
})
})
}
let getCurrentPoints = id => {
const options = {
url: 'https://api.nasapatrola.com/User/GetUserById/' + id,
headers: { Authorization: userToken }
}
return new Promise((resolve, reject) => {
request.get(options, (err, resp, body) => {
if (err) reject(err)
else resolve(body)
})
})
}
const errHandler = (err) => console.error(err)
const tokenExpired = (body) => {
if(body.includes('expired'))
throw new Error('Provided token has expired. Token re-acquirement has not been implemented.')
return body
}
let main = () => {
if (argv.id){
getCurrentPoints(argv.id)
.then(body => tokenExpired(body))
.then(JSON.parse)
.then(({ username, id, points }) => console.log(`User ${username} (${id}) has ${points} points`))
.catch(errHandler)
return
}
console.log(`Iteration ran at ${new Date().toUTCString()}`)
getActivePoints()
.then(body => tokenExpired(body))
.then(JSON.parse, errHandler)
.then(result => {
console.log(`Found ${result.length} active points`, result)
if (nocollect) return
result.map(({ id }) => {
collectPoints(id)
.then(body => tokenExpired(body))
.then(JSON.parse, errHandler)
.then(({ poins }) => console.log(`Collected ${poins} points from active points id ${id}`))
.catch(err => console.error(`Unable to retrieve points for id ${id}`, err))
})
}, errHandler)
}
main()
if (!argv.id && !argv.nointerval) setInterval(main, 1000 * 60 * 5)