-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
338 lines (226 loc) · 8.29 KB
/
server.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const express = require('express')
const app = express();
const PORT = process.env.PORT || 3001;
const request = require('request')
const bodyParser = require('body-parser');
const { response } = require('express');
const cors = require('cors')
const path = require('path')
const { Client } = require('@notionhq/client')
const fetch = require('node-fetch'); //npm install node-fetch
const jsonParser = bodyParser.json()
const router = express.Router()
const corsOptions = {
origin:'*',
credentials:true, //access-control-allow-credentials:true
optionSuccessStatus:200,
}
app.use(cors(corsOptions)) // Use this after the variable declaration
app.use('/', router)
app.use(express.static(path.join(__dirname, '/')));
router.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'))
})
app.listen(PORT, function name() {
console.log("Starting proxy at: " + PORT);
})
let usernames
var globalArray = []
const notion = new Client({auth: "secret_Q9yioL3FNmSl7AsFL8JKwkeoUoUnoV8jsIJHfRxlZIM"});
var databaseid
const currentDate = new Date();
function subtractHours(date, hours){
date.setHours(date.getHours() - hours);
return date;
}
app.get('/info', async function (req, res) {
cleanedArray = arrUnique(globalArray)
res.status(200).json({info: cleanedArray})
console.log(cleanedArray);
globalArray = []
})
app.post('/databaseID', jsonParser, async function(req, res) {
const {parcel, isHighElo} = req.body
if(!parcel){
res.status(200).send({status: 'failed'})
}else{
res.status(200).send({status: 'recived'})
databaseid = parcel
if (isHighElo == true){
usernames = ['sami181', 'LDGZCH', 'JMGZCH', 'wilkachimbo']
}else{
usernames = ['Zeratul2022', 'JFyoyu777', 'Luligamer1', 'Samueljanu']
}
console.log("GROUP HIGH", isHighElo, usernames);
}
})
app.post('/info', jsonParser, async function (req, res){
const {parcel} = req.body
if(!parcel)
{
res.status(200).send({status: 'failed'})
}
res.status(200).send({status: 'recived'})
chessQuery(parcel)
})
function chessQuery(chessURL) {
request({url: chessURL}, (error, response, body) =>{
if(error || response.statusCode !== 200)
{
return console.log(error)
}
var chessUser = JSON.parse(body)
gamesFilter(chessUser)
})
}
async function gamesFilter(userJson) {
let gamesArray = []
let chessGames = userJson.games;
for (let i = 0; i < chessGames.length; i++) {
const currentGame = chessGames[i];
const black = currentGame.black
const white = currentGame.white
let pgnArray = currentGame.pgn.split('\n')
let id = currentGame.url.substring(32, currentGame.url.length)
let date = pgnArray[2].substring(7, pgnArray[2].length - 2)
let time = pgnArray[19].substring(10, pgnArray[19].length - 2)
var termination = pgnArray[16].substring(14, pgnArray[16].length - 2)
let terminationNoUser = termination.split(" ");
let userOfTermination = terminationNoUser.shift()
const uppercaseWords = str => str.replace(/^(.)|\s+(.)/g, c => c.toUpperCase());
termination = uppercaseWords(terminationNoUser.join(" ").toString())
var gmt5Date = subtractHours(new Date(date + ' ' + time + ' GMT-5'), 10)
if(isNaN(gmt5Date) == false){
const gameJson = {
gameId: id,
gameTitle: '🏆',
winnerPlayer:'',
defeatedPlayer: '',
date: gmt5Date.toISOString(), //no sobreescribir en notion las que ya estan hechas
termination: termination, // eliminar la primera palabra de la razón de la victoria ej: 'won by checkmate'
url: currentGame.url,
whitePlayer: '',
blackPlayer: ''
}
if(usernames.indexOf(black.username) != -1){
if(usernames.indexOf(white.username) != -1){
console.log(usernames, black.username, white.username, gmt5Date);
if(gmt5Date.getHours() == 3 && gmt5Date.getMinutes() <= 50 && gmt5Date.getDay() >= 1 && gmt5Date.getDay() <= 5){
gameJson.whitePlayer = "♞ " + white.username
gameJson.blackPlayer = "♞ " + black.username
if (white.result == 'win' ) {
gameJson.winnerPlayer = "♞ " + white.username
gameJson.defeatedPlayer = "♞ " + black.username
}
if (black.result == 'win') {
gameJson.winnerPlayer = "♞ " + black.username
gameJson.defeatedPlayer = "♞ " + white.username
}
if(white.result == 'stalemate' || black.result == 'stalemate' || white.result == 'insufficient' || black.result == 'insufficient' || white.result == 'agreed' || black.result == 'agreed' || white.result == 'repetition' || black.result == 'repetition'){
gameJson.winnerPlayer = '❌';
gameJson.defeatedPlayer = '❌'
}
//console.log(gameJson);
gamesArray.push(gameJson)
}
}
}
}
}
globalArray.push(...gamesArray)
}
function arrUnique(arr) {
var clean = arr.filter((itm, index, self) =>
index === self.findIndex((t) => (t.gameId === itm.gameId && t.gameUrl === itm.gameUrl)))
return clean;
}
async function queryDB(gameUrl) {
const response = await notion.databases.query({
database_id: databaseid,
filter: {
or: [
{
property: 'Link',
url: {
contains: gameUrl,
},
}
],
}
});
return response
}
async function postInDB(game) {
const response = await notion.pages.create({
parent: {database_id: databaseid},
properties:{
"Game":{
title:[
{
text:{
content: game.gameTitle
}
}
]
},
"Winner":{
select:{
name: game.winnerPlayer
}
},
"Defeated":{
select:{
name: game.defeatedPlayer
}
},
"Date":{
date:{
start: game.date, //"2022-10-29T11:00:00.000-04:00"
time_zone: "America/Guayaquil"
}
},
"Termination":{
select:{
name: game.termination
}
},
"Link":{
url: game.url
},
"White Player":{
select:{
name: game.whitePlayer
}
},
"Black Player":{
select:{
name: game.blackPlayer
}
}
}
})
}
async function getUsernames() {
}
app.post('/notion', jsonParser, async function (req, res) {
const game = req.body
if (!game) {
return res.status(400).send({status: 'failed'})
}
res.status(200).send({status: 'recived'})
try {
var notionQuery = queryDB(game.url)
if(notionQuery != undefined)
{
if((await notionQuery).results.length == 0)
{
postInDB(game)
console.log("SUCCESS")
}else{
console.log("UNABLE TO POST BY DUPLICATE")
}
}
} catch (error) {
console.log(error)
}
})