-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (118 loc) · 3.97 KB
/
index.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
const express = require('express');
const bodyparser = require('body-parser');
const request = require('request-promise-native').defaults({ jar: true });
const cheerio = require('cheerio');
const config = require('./config');
const app = express();
app.use(express.static('static'));
let toBeRead = [];
let bids = [];
app.use(bodyparser.json());
app.get('/donations', (req, res) => {
res.header().set('Content-Type', 'application/json');
res.header().set('Cache-Control', 'no-cache, no-transform');
res.send(toBeRead);
});
app.get('/bids', (req, res) => {
res.header().set('Content-Type', 'application/json');
res.header().set('Cache-Control', 'no-cache, no-transform');
res.send(bids);
});
app.get('/edit', (req, res) => {
if (!req.query.id && !req.query.action) {
res.status(400);
return;
}
console.log(`marking donation with id ${req.query.id} with status ${req.query.action}`);
let readstate, commentstate;
if (req.query.action === 'read') {
readstate = 'READ';
commentstate = 'APPROVED';
} else if (req.query.action === 'approve') {
readstate = 'PENDING';
commentstate = 'APPROVED';
} else if (req.query.action === 'deny') {
readstate = 'IGNORED';
commentstate = 'DENIED';
} else if (req.query.action === 'revert') {
readstate = 'PENDING';
commentstate = 'PENDING';
} else {
res.sendStatus(400);
}
// http://localhost:8000/admin/edit_object?type=donation&id=585&readstate=READ&commentstate=APPROVED&
// http://localhost:8000/admin/edit_object?type=donation&id=585&readstate=IGNORED&commentstate=DENIED&
// http://localhost:8000/admin/edit_object?type=donation&id=585&readstate=READY&commentstate=APPROVED&
request({
method: 'GET',
uri: config.tracker.editurl,
qs: {
type: 'donation',
id: req.query.id,
readstate,
commentstate
}
})
.then(() => {
res.sendStatus(204);
console.log('ok');
})
.catch(e => {
res.sendStatus(500).send(e);
console.log(e.message);
});
});
console.log('スタート');
function trackerLogin() {
return new Promise((resolve, reject) => {
request({
uri: config.tracker.loginurl,
transform(body) {
return cheerio.load(body);
}
}).then($ => request({
method: 'POST',
uri: config.tracker.loginurl,
form: {
username: config.tracker.username,
password: config.tracker.password,
csrfmiddlewaretoken: $('#login-form > input[name="csrfmiddlewaretoken"]').val()
},
headers: {
Referer: config.tracker.loginurl
},
resolveWithFullResponse: true,
simple: false
})).then(() => {
resolve();
}).catch(e => reject(e));
});
}
function getDonations() {
return request('http://localhost:8000/search/?event=5&type=donation&transactionstate=COMPLETED&readstate=PENDING')
.then(body => {
const jsonbody = JSON.parse(body);
jsonbody.reverse();
console.log(`${jsonbody.length} ${jsonbody.length === 1 ? 'donation' : 'donations'} to be read`);
toBeRead = jsonbody;
})
.catch(e => e);
}
function getBids() {
return request('http://localhost:8000/search/?event=5&type=bidtarget&state=PENDING')
.then(body => {
const jsonbody = JSON.parse(body);
bids = jsonbody;
});
}
(async () => {
console.log('logging into tracker');
await trackerLogin();
console.info(`Logged into tracker as ${config.tracker.username}`);
console.log('starting refresh interval');
getDonations();
setInterval(getDonations, 5000);
setInterval(getBids, 5000);
console.log('Webserver started on :8080');
app.listen(8080);
})();