@@ -60,12 +60,18 @@ const BOT_TOKEN = process.env.TOKEN
60
60
const BOOK_DEPTH = parseInt ( process . env . BOOK_DEPTH || "40" )
61
61
62
62
const drop = false
63
-
63
+
64
+ function logFunc ( ...args ) {
65
+ if ( process . env . DISABLE_MONGO2_LOGS == "true" ) return
66
+
67
+ console . log ( ...args )
68
+ }
69
+
64
70
MongoClient . connect ( MONGODB_URI , { useNewUrlParser : true , useUnifiedTopology : true } , function ( err , setClient ) {
65
71
if ( err ) {
66
- console . log ( "MongoDb connection failed." )
72
+ logFunc ( "MongoDb connection failed." )
67
73
} else {
68
- console . log ( "MongoDb connected." )
74
+ logFunc ( "MongoDb connected." )
69
75
70
76
client = setClient
71
77
@@ -101,7 +107,7 @@ async function processPgn(pgn, resolve){
101
107
let variantKey = variantName2variantKey [ tags . variant ]
102
108
103
109
if ( ! variantKey ) {
104
- console . log ( `unknown variant ${ tags . variant } ` )
110
+ logFunc ( `unknown variant ${ tags . variant } ` )
105
111
106
112
resolve ( false )
107
113
@@ -115,7 +121,7 @@ async function processPgn(pgn, resolve){
115
121
116
122
if ( PGN_URL ) {
117
123
tags . hash = pgn . hashCode ( ) . toString ( )
118
- console . log ( `created hash code ${ tags . hash } ` )
124
+ logFunc ( `created hash code ${ tags . hash } ` )
119
125
}
120
126
121
127
let stored = await gamecoll . findOne ( tags )
@@ -129,7 +135,7 @@ async function processPgn(pgn, resolve){
129
135
}
130
136
131
137
if ( rebuild ) {
132
- console . log ( `${ i } . processing game ${ tags . white } - ${ tags . black } ${ tags . site } ` )
138
+ logFunc ( `${ i } . processing game ${ tags . white } - ${ tags . black } ${ tags . site } ` )
133
139
134
140
let ok = true
135
141
@@ -157,17 +163,17 @@ async function processPgn(pgn, resolve){
157
163
let updateResult = await movecoll . updateOne ( doc , { $set : doc } , { upsert : true } )
158
164
let ok = false
159
165
if ( ( typeof updateResult == "object" ) && ( typeof updateResult . result == "object" ) ) {
160
- console . log ( `${ san } update n ${ updateResult . result . n } ` )
166
+ logFunc ( `${ san } update n ${ updateResult . result . n } ` )
161
167
if ( updateResult . result . n != 1 ) {
162
- console . log ( `move update result n not 1` )
168
+ logFunc ( `move update result n not 1` )
163
169
ok = false
164
170
}
165
171
} else {
166
- console . log ( `no move update result` )
172
+ logFunc ( `no move update result` )
167
173
ok = false
168
174
}
169
175
} else {
170
- console . log ( `to few fen fields` )
176
+ logFunc ( `to few fen fields` )
171
177
}
172
178
}
173
179
}
@@ -177,21 +183,21 @@ async function processPgn(pgn, resolve){
177
183
bookdepth : BOOK_DEPTH
178
184
} }
179
185
180
- console . log ( `updating game ${ tags . site } ` )
186
+ logFunc ( `updating game ${ tags . site } ` )
181
187
let updateResult = await gamecoll . updateOne ( tags , { $set : gameDoc } , { upsert : true } )
182
188
if ( ( typeof updateResult == "object" ) && ( typeof updateResult . result == "object" ) ) {
183
- console . log ( `game update n ${ updateResult . result . n } ` )
189
+ logFunc ( `game update n ${ updateResult . result . n } ` )
184
190
if ( updateResult . result . n != 1 ) {
185
- console . log ( `game update result n not 1` )
191
+ logFunc ( `game update result n not 1` )
186
192
}
187
193
} else {
188
- console . log ( `no game update result` )
194
+ logFunc ( `no game update result` )
189
195
}
190
196
} else {
191
- console . log ( `not updating game ${ tags . site } ` )
197
+ logFunc ( `not updating game ${ tags . site } ` )
192
198
}
193
199
} else {
194
- console . log ( `${ i } . skipping built game ${ tags . white } - ${ tags . black } ${ tags . site } ` )
200
+ logFunc ( `${ i } . skipping built game ${ tags . white } - ${ tags . black } ${ tags . site } ` )
195
201
}
196
202
197
203
resolve ( true )
@@ -217,34 +223,34 @@ async function processPgns(content){
217
223
await processPgnThen ( pgn )
218
224
}
219
225
220
- console . log ( `processing pgns done` )
226
+ logFunc ( `processing pgns done` )
221
227
222
228
client . close ( )
223
229
224
- console . log ( `client closed` )
230
+ logFunc ( `client closed` )
225
231
}
226
232
227
233
function loadGames ( ) {
228
- movecoll . countDocuments ( ) . then ( result => console . log ( "moves" , result ) )
234
+ movecoll . countDocuments ( ) . then ( result => logFunc ( "moves" , result ) )
229
235
230
236
let url = PGN_URL || `https://lichess.org/api/games/user/${ BOT_NAME } ?max=${ MAX_GAMES } `
231
237
232
- console . log ( `downloading pgn games from ${ url } ` )
238
+ logFunc ( `downloading pgn games from ${ url } ` )
233
239
234
240
let headers = { }
235
241
236
242
if ( ! PGN_URL ) headers . Authorization = `Bearer ${ BOT_TOKEN } `
237
243
238
244
if ( PGN_URL && url . match ( / \. 7 z $ / ) ) {
239
- console . log ( `7zip url detected` )
245
+ logFunc ( `7zip url detected` )
240
246
241
247
fetch ( url , {
242
248
headers : headers
243
249
} ) . then ( response => {
244
250
const dest = fs . createWriteStream ( `temp.7z` )
245
251
response . body . pipe ( dest )
246
252
response . body . on ( 'end' , _ => {
247
- console . log ( `7z file written to disk` )
253
+ logFunc ( `7z file written to disk` )
248
254
249
255
const Seven = require ( 'node-7z' )
250
256
@@ -255,7 +261,7 @@ function loadGames(){
255
261
let pgnFile = null
256
262
257
263
myStream . on ( 'data' , function ( data ) {
258
- console . log ( data )
264
+ logFunc ( data )
259
265
260
266
if ( data . status == "extracted" ) {
261
267
let file = data . file
@@ -267,21 +273,21 @@ function loadGames(){
267
273
} )
268
274
269
275
myStream . on ( 'end' , function ( ) {
270
- console . log ( `7z file extraced ok ${ pgnFile } ` )
276
+ logFunc ( `7z file extraced ok ${ pgnFile } ` )
271
277
272
278
let content = fs . readFileSync ( pgnFile ) . toString ( )
273
279
274
280
processPgns ( content )
275
281
} )
276
282
277
283
myStream . on ( 'error' , err => {
278
- console . log ( `an error occured unzipping 7z file` , err )
284
+ logFunc ( `an error occured unzipping 7z file` , err )
279
285
280
286
client . close ( )
281
287
} )
282
288
} )
283
289
dest . on ( 'error' , err => {
284
- console . log ( `an error occured writing 7z file` , err )
290
+ logFunc ( `an error occured writing 7z file` , err )
285
291
} )
286
292
} )
287
293
} else {
0 commit comments