-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposters.js
131 lines (111 loc) · 3.87 KB
/
posters.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
#!/bin/env node
// Node.js single file Module to allow making price and news updates to Campaign Treasurer Companion
// import the required modules
var http = require('http');
var querystring = require('querystring');
// constants to use with URL
const hostname = process.env.CTC_UPDATER_HOST || 'localhost';
const authorisationToken = process.env.CTC_ADMIN_TOKEN || 'someuser:somepassword';
// paths to use URL(s) in HTTP requests
const pathForNewsUpdate = '/updater/addnews.php';
const pathForPriceOf = '/updater/getprice.php';
const pathForPriceUpdate = '/updater/updateprice.php';
var descriptor = {
hostname: hostname,
// path: somePath,
// method: 'POST' || 'GET',
auth: authorisationToken,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length' : 0
}
}
exports.differenceNewsAndPriceUpdate = 60000
// function to chain update (postNews, wait for difference time, get priceOf, then updatePrice)
exports.chainUpdate = function(updateItem, callback) {
exports.postNews(updateItem.news, () => {
setTimeout(() => {
exports.priceOf(updateItem.item, (price) => {
var updatedPrice = parseFloat((price + (price * updateItem.percentage * 0.01)).toFixed(2))
exports.updatePrice(updateItem.item, updatedPrice, () => {
callback()
})
})
}, exports.differenceNewsAndPriceUpdate)
})
}
// function to post news content and run a callback
exports.postNews = function(content, callback) {
// post data contains the news content
var postData = querystring.stringify({
content: content
});
// descriptor to make a HTTP request
var options = descriptor;
options.path = pathForNewsUpdate
options.method = 'POST'
options.headers['Content-Length'] = postData.length
// make the HTTP request to send news update
var request = http.request(options, (response) => {
var data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
// perform callback
callback(data);
});
});
// write the post data to the request and end the request
request.write(postData);
request.end();
};
// function to find price of an item and run a callback with it
exports.priceOf = function(item, callback) {
// descriptor to make a HTTP request
var options = descriptor;
options.path = pathForPriceOf + '?' + querystring.stringify({name: item})
options.method = 'GET'
options.headers = {}
// make the HTTP request to get price of item
var request = http.request(options, (response) => {
var data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
// perform callback with price
var price = parseFloat(data.split('\n')[1]);
callback(price);
});
});
// end the request
request.end();
};
// function to post price update and run a callback
exports.updatePrice = function(item, price, callback) {
// post data contains the item name and new price
var postData = querystring.stringify({
item: item,
current: price
});
// descriptor to make a HTTP request
var options = descriptor;
options.path = pathForPriceUpdate
options.method = 'POST'
options.headers['Content-Length'] = postData.length
// make the HTTP request to send news update
var request = http.request(options, (response) => {
var data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
// perform callback
callback(data);
});
});
// write the post data to the request and end the request
request.write(postData);
request.end();
};