-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
300 lines (246 loc) · 9.03 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
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
294
295
296
297
298
const express = require('express')
const bodyParser = require('body-parser')
const fetch = require('node-fetch');
const app = express();
const {draw, getIPFSFile} = require('./src/render_svg');
const {ethers} = require("ethers");
const {db, getTwitterUsers} = require("./src/twitter");
let port = process.env.PORT || 3000;
// let bearer = "AAAAAAAAAAAAAAAAAAAAAMGxPQEAAAAAn2J6b%2BvyI8t8qKryhfZUisZE94Q%3DcfOS71mRCVynHctv4q910Emh4Ezh0409XQHgjZ4UwDDciQhJ78";
let rooms = [
{
"type": "Deluxe Room",
"price": 110.3,
"frequency": "per night",
// "discountPrice": "1",
"image": "./hotel_3.jpg"
},
{
"type": "King Suite",
"price": 89.3,
"frequency": "per night",
// "discountPrice": "1.5",
"image": "./hotel_2.png"
},
{
"type": "Superior Deluxe Suite",
"price": 55.1,
"frequency": "per night",
// "discountPrice": "4",
"image": "./hotel_1.jpg"
},
{
"type": "Super Econom",
"price": 11,
"frequency": "per night",
// "discountPrice": "4",
"members_only": 1,
"image": "./hotel_4.jpg"
}
];
function useTokens(initProducts, tokens){
let hasDiscount = 0;
let hasFreeShiping = 0;
let showExtraProducts = 0;
tokens.forEach(ticket=>{
if (token.hasOwnProperty('ticketClass')) {
switch (token.ticketClass) {
case "0n":
hasDiscount++;
break;
case "1n":
hasFreeShiping++;
break;
case "2n":
showExtraProducts++;
break;
}
}
})
let activeRooms = [];
initProducts.forEach(room=>{
showExtraProducts && (room.members_only = 0);
hasFreeShiping && (room.free_shipping = 1);
hasDiscount && (room.discountPrice = parseFloat(room.price) - 2);
room.members_only || activeRooms.push(room)
});
return activeRooms;
}
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
// res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8888');
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
// res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.use(bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.get('/svgipfs/', async (request, response) => {
try {
{
// test GET param contract
if (!request.query || !request.query.ipfshash || !request.query.ipfshash.length){
throw new Error("correct ipfshash required");
}
let ipfshashRe = /^[a-zA-Z0-9]{46}$/;
let ipfshashTest = ipfshashRe.test(String(request.query.ipfshash));
if (!ipfshashTest){
throw new Error("wrong ipfshash");
}
}
{
// test GET param autograph
if (!request.query || !request.query.autograph || !request.query.autograph.length){
throw new Error("correct autograph required");
}
}
let ipfsFileBuf = await getIPFSFile(request.query.ipfshash);
let svg = await draw(ipfsFileBuf, request.query.autograph);
// Express 4:
response.status(200)
.attachment(`autograph.svg`)
.send(svg);
// console.log(json);
// response.json( json );
} catch(err) {
// console.log(err);
response.json( {res: "request failed. "+err} );
}
})
app.get('/svg/', async (request, response) => {
let contractAddress = '',
tokenId = '',
autograph = '';
try {
{
// test GET param contract
if (!request.query || !request.query.contract || !request.query.contract.length){
throw new Error("correct contract required");
}
contractAddress = request.query.contract;
if (!/^0x[a-zA-Z0-9]{40}$/.test(String(contractAddress))){
throw new Error("wrong contract");
}
}
{
// test GET param tokenId
if (!request.query || !request.query.tokenId || !request.query.tokenId.length){
throw new Error("correct tokenId required");
}
tokenId = request.query.tokenId;
if (!/^[a-zA-Z0-9_]+$/.test(String(tokenId))){
throw new Error("wrong tokenId format");
}
}
{
// test GET param autograph
if (!request.query || !request.query.autograph || !request.query.autograph.length){
throw new Error("correct autograph required");
}
autograph = request.query.autograph;
}
let provider = new ethers.providers.InfuraProvider("rinkeby", "a3babf9f8bf64e5ca6b4caa070169744");
let tokenUriAbi = '[{"constant": true,"inputs":[{"name":"_tokenId","type":"uint256"}],"name":"tokenURI","outputs": [{"name": "url", "type": "string"}],"payable": false,"stateMutability": "view","type": "function"}]';
// let stormbirdSART = "0xa567f5A165545Fa2639bBdA79991F105EADF8522";
// let tokenID = "284";
let contract = new ethers.Contract( contractAddress , tokenUriAbi , provider );
let tokenMetaObj;
try {
console.log('request tokenMetaObj');
tokenMetaObj = await contract.callStatic.tokenURI(tokenId);
// console.log(tokenMetaObj);
} catch(e){
console.log(e);
throw new Error(`cant fetch tokenURI for contract "${contractAddress}" and tokenID "${tokenId}"`);
}
console.log('fetch '+tokenMetaObj);
let responce = await fetch(tokenMetaObj);
let dataJson = await responce.json();
if (dataJson && dataJson.image){
console.log('request file');
let responce = await fetch(dataJson.image);
let imageData = await responce.buffer();
console.log('draw svg');
let svg = await draw(imageData, autograph);
response.status(200)
.attachment(`autograph.svg`)
.send(svg);
} else {
response.json( {res: "cant generate image."} );
}
} catch(err) {
// console.log(err);
response.json( {res: "request failed. "+err} );
}
})
app.get('/twitter/', async (request, response) => {
let time = Date.now();
let name;
try {
if (!request.query || !request.query.name || !request.query.name.length){
throw new Error("please dont forget to send username");
}
name = request.query.name;
if (!/^[a-zA-Z0-9_]{3,15}$/.test(String(name))){
throw new Error("wrong username");
}
response.json( {data: await getTwitterUsers(name) });
// console.log(getTwitterUsers(name));
console.log('used time: ' + (Date.now() - time));
} catch(err) {
// console.log(err);
response.json( {res: "request failed. "+err} );
}
});
app.get('/twitter2/', async (request, response) => {
const letters = 'ABCDEFGHIKLMNOPQRSTVXYZ_1234567890'.toLowerCase();
// const letters = 'ABCDEFGHIKLMNOPQRSTQRSTVXYZ'.toLowerCase();
// const letters = 'AB'.toLowerCase();
let time;
try {
for (let i1 = 0; i1 < letters.length; i1++ ){
for (let i2 = 0; i2 < letters.length; i2++ ){
time = Date.now();
let res = await getTwitterUsers(letters[i1] + letters[i2]);
console.log('q = ' + letters[i1] + letters[i2] +' ;used time: ' + (Date.now() - time));
}
}
response.json({res:'all ok'});
} catch(err) {
// console.log(err);
response.json( {res: "request failed. "+err} );
}
});
app.get('/', (request, response) => {
// response.send('Service works with POST only.');
// response.json(rooms);
// const params = new URLSearchParams('foo=bar&foo=baz');
// for (const name of params.keys()) {
// console.log(name);
// }
response.json(useTokens(rooms, []) );
});
app.post('/', function (request, response) {
if (!request.body || !request.body.tickets || !request.body.tickets.length){
response.json(rooms);
} else {
let tickets = JSON.parse(request.body.tickets);
response.json(useTokens(rooms, tickets) );
}
});
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})