-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive.js
686 lines (487 loc) · 25.8 KB
/
receive.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
var amqplib = require('amqplib');
const axios = require('axios')
const fs = require('fs');
const log4js = require("log4js");
var aauser = require('./aauser.js')
const queueName = "task";
const consumeTask = async () =>{
try {
const connection = await amqplib.connect('amqp://localhost')
const channel = await connection.createChannel();
await channel.assertQueue(queueName,{durable:true});
channel.prefetch(1);
console.log('Waiting for messages in queue' + queueName);
channel.consume(queueName, msg => {
console.log("[X] Received:", msg.content.toString());
var firstGet = msg.content.toString()
var element = JSON.parse(firstGet)
if(element.type == "picture"){
log4js.configure({
appenders: { cheese: { type: "file", filename: "./Logs/Picture/Picture.log", maxLogSize: 10485760, backups: 3, compress: true } },
categories: { default: { appenders: ["cheese"], level: "trace" } }
});
const pictureLogger = log4js.getLogger("cheese");
var newsIsDownloaded = false
var pictureIsDownloaded = false
var imagestryCounter = 0;
var dir = element.group_id.replace(/:/g, '_');
var downloadId = element.id
var idsplit = downloadId.replace(/:/g, '_');
var groupid = element.group_id
var CreateDirectory = '.\\Picture\\'+ dir + '\\'
var directory = './Picture/' + dir + '/'
var picturePath = directory + idsplit + '.jpg'
if (!fs.existsSync(CreateDirectory)){
fs.mkdirSync('./Picture/'+dir, { recursive: true });
pictureLogger.trace('Created Directory for Picture');
console.log('Created Directory for Picture')
}
downloadImage(newsDownload)
function downloadImage(callback){
if (fs.existsSync(picturePath)) {
var stats = fs.statSync(picturePath)
var fileSizeInBytes = stats.size;
// Convert the file size to megabytes (optional)
pictureLogger.trace(fileSizeInBytes);
console.log(fileSizeInBytes)
if(fileSizeInBytes < 150){
fs.unlink(picturePath, function (err) {
if (err) throw err;
// if no error, file has been deleted successfully
pictureLogger.trace("The picture file size is less 1 KB. The picture file deleted.");
console.log("The picture file size is less 1 KB. The picture file deleted.");
});
setTimeout(() => {
downloadImage(newsDownload)
}, 7500);
}else{
pictureLogger.trace('Image file was previously downloaeded: ' + picturePath);
console.log('Image file was previously downloaeded.')
pictureIsDownloaded = true
newsDownload(taskIsSuccess)
}
}else{
axios.get('https://api.aa.com.tr/abone/document/' + downloadId + '/print', {responseType: "stream",auth: {
username: aauser.aaUserName,
password: aauser.aaPassword
}} )
.then(async response => {
if(response.status == 200){
// Saving file to working directory
var picture = fs.createWriteStream(CreateDirectory + idsplit + '.jpg')
const p = response.data.pipe(picture);
p.on('finish', () => {
pictureLogger.trace('Download is successful. ' + idsplit + '.jpg');
console.log('Download is successful.')
pictureIsDownloaded = true
setTimeout(() => {
callback(taskIsSuccess)
}, 3000);
});
}else{
setTimeout(() => {
imagestryCounter = imagestryCounter + 1
if(imagestryCounter == 10){
taskIsSuccess()
}else{
downloadImage(newsDownload)
}
}, 7500);
}
})
.catch(error => {
pictureLogger.error(error);
console.log(error)
if(error){
setTimeout(() => {
imagestryCounter = imagestryCounter + 1
if(imagestryCounter == 10){
taskIsSuccess()
}else{
setTimeout(() => {
downloadImage(newsDownload)
}, 4750);
}
}, 2000);
}
});
}
}
function newsDownload(callback){
if (!fs.existsSync('./AA/' + dir + '.xml')){
axios.get('https://api.aa.com.tr/abone/document/'+element.group_id+'/newsml29',
{responseType: "stream",
auth: {
username: aauser.aaUserName,
password: aauser.aaPassword
},
})
.then(response => {
response.data.pipe(fs.createWriteStream('./NewAA/' + dir + '.xml'));
response.data.pipe(fs.createWriteStream('./AA/' + dir + '.xml'));
pictureLogger.trace('File name for the picture news: ' + dir + '.xml');
console.log('File name for the picture news: ' + dir + '.xml')
newsIsDownloaded = true
callback()
})
.catch(error => {
pictureLogger.error(error);
console.log(error);
if(error){
setTimeout(() => {
imagestryCounter = imagestryCounter + 1
newsDownload(taskIsSuccess)
}, 2000);
}
});
}else{
pictureLogger.trace("News was previously downloaded. "+ dir + '.xml');
console.log("News was previously downloaded. "+ dir + '.xml')
newsIsDownloaded = true
callback()
}
}
function taskIsSuccess(){
if(newsIsDownloaded == true && pictureIsDownloaded == true){
pictureLogger.trace('It was successfully downloaded after ' + imagestryCounter + 'attemps.');
console.log('It was succcessfully downloaded after ' + imagestryCounter + 'attemps.')
channel.ack(msg)
}
else if(imagestryCounter == 10){
pictureLogger.warn('The process tried after ten times. The Proccess is unsuccessful.');
pictureLogger.warn('Picture ' + pictureIsDownloaded)
pictureLogger.warn("News XML " + newsIsDownloaded)
console.log('The process tried after ten times. The Proccess is unsuccessful.')
console.log('Picture ' + pictureIsDownloaded)
console.log('News XML ' + newsIsDownloaded)
channel.ack(msg)
}
else{
setTimeout(() => {
downloadImage(newsDownload)
}, 7500);
}
}
}
else if(element.type == "video"){
log4js.configure({
appenders: { cheese: { type: "file", filename: "./Logs/Video/Video.log", maxLogSize: 10485760, backups: 3, compress: true } },
categories: { default: { appenders: ["cheese"], level: "trace" } }
});
const videoLogger = log4js.getLogger("cheese");
var videoNewsIsDownloaded = false
var videoeIsDownloaded = false
var videoTryCounter = 0;
var downloadId = element.id
var idsplit = downloadId.replace(/:/g, '_');
var videoXmlPath = './AA/' + idsplit + '.xml'
var videoPath = './Video/' + idsplit + '.mp4'
var fileUrl = 'https://api.aa.com.tr/abone/document/'+downloadId+'/sd'
if (fs.existsSync(videoPath)) {
videoeIsDownloaded = true
videoLogger.trace('Video is true. Check XML News. ' + videoeIsDownloaded);
console.log('Video is true. Check XML News. ' + videoeIsDownloaded)
newsVideoXmlDownload(videoTaskIsSuccess)
}else{
newsVideoDownload(newsVideoXmlDownload)
}
function newsVideoDownload(callback){
axios.get(fileUrl, {responseType: "stream",auth: {
username: aauser.aaUserName,
password: aauser.aaPassword
}} )
.then(async response => {
// Saving file to working directory
var video = fs.createWriteStream('./Video/' + idsplit + '.mp4')
const p = response.data.pipe(video);
p.on('finish', () => {
console.log('Video download is successful.')
videoLogger.trace('Video download is successful: ' + idsplit + '.mp4');
videoeIsDownloaded = true
setTimeout(() => {
callback(videoTaskIsSuccess)
}, 2000);
});
})
.catch(error => {
videoLogger.error(error);
console.log(error)
if(error){
setTimeout(() => {
videoTryCounter = videoTryCounter + 1
if(videoTryCounter == 10){
videoTaskIsSuccess()
}else{
videoLogger.error('It was unsuccessful after' + videoTryCounter + 'attemps. Try again download video.');
console.log('It was unsuccessful after' + videoTryCounter + 'attemps. Try again download video.')
newsVideoDownload(newsVideoXmlDownload)
}
}, 2000);
}
});
}
function newsVideoXmlDownload(callback){
if (fs.existsSync(videoXmlPath)) {
videoNewsIsDownloaded = true
videoTaskIsSuccess()
}
else{
axios.get('https://api.aa.com.tr/abone/document/'+downloadId+'/newsml29',
{responseType: "stream",auth: {
username: aauser.aaUserName,
password: aauser.aaPassword
}} )
.then(response => {
// Saving file to working directory
response.data.pipe(fs.createWriteStream('./NewAA/' + idsplit + '.xml'));
response.data.pipe(fs.createWriteStream('./AA/' + idsplit + '.xml'));
videoLogger.trace('Video XML is successful.' + idsplit + '.xml');
console.log('Video XML is successful.')
videoNewsIsDownloaded = true
callback(videoTaskIsSuccess)
})
.catch(error => {
console.log(error)
});
}
}
function videoTaskIsSuccess(){
if(videoNewsIsDownloaded == true && videoeIsDownloaded == true){
videoLogger.trace('It was unsuccessful after' + videoTryCounter + 'attemps. Try again download video.');
console.log('It was unsuccessful after' + videoTryCounter + 'attemps. Try again download video.')
channel.ack(msg)
}
else if(videoTryCounter == 10){
videoLogger.warn('Video download ');
videoLogger.warn('Video download attemps: ' + videoeIsDownloaded)
videoLogger.warn('Video XML download attemps: ' + videoNewsIsDownloaded)
console.log('Video dowlnload tried after 10 attemps.')
console.log('Video download attemps: ' + videoeIsDownloaded)
console.log('Video XML download attemps: ' + videoNewsIsDownloaded)
channel.ack(msg)
}
else{
setTimeout(() => {
newsVideoDownload(newsVideoXmlDownload)
}, 2000);
}
}
}
else if(element.type == "text"){
log4js.configure({
appenders: { cheese: { type: "file", filename: "./Logs/Text/Text.log", maxLogSize: 10485760, backups: 3, compress: true } },
categories: { default: { appenders: ["cheese"], level: "trace" } }
});
const textLogger = log4js.getLogger("cheese");
var newsTextIsDownloaded = false
var newstryDownload = 0
var downloadId = element.id
var idsplit = downloadId.replace(/:/g, '_');
var newsTextFile = './AA/' + idsplit + '.xml'
try {
if (!fs.existsSync(newsTextFile)) {
setTimeout(() => {
newsTextDownload(newsTextDownloadController)
}, 1500);
}else{
newsTextIsDownloaded = true
newsTextDownloadController()
}
} catch(err) {
textLogger.error(err);
console.log(err)
}
function newsTextDownload(callback){
axios.get('https://api.aa.com.tr/abone/document/'+downloadId+'/newsml29', {responseType: "stream",auth: {
username: aauser.aaUserName,
password: aauser.aaPassword
}} )
.then(response => {
// Saving file to working directory
response.data.pipe(fs.createWriteStream('./NewAA/' + idsplit + '.xml'));
response.data.pipe(fs.createWriteStream(newsTextFile));
textLogger.trace("Text download is successful: " + idsplit + '.xml');
newsTextIsDownloaded = true;
callback()
})
.catch(error => {
textLogger.error(error);
console.log(error);
if(error){
if(newstryDownload == 10){
newsTextDownloadController()
}else{
setTimeout(() => {
newsTextDownload(newsTextDownloadController)
}, 1500);
}
}
});
}
function newsTextDownloadController(){
if(newsTextIsDownloaded == true){
console.log('News was downloaded after' + newstryDownload + 'attemps.')
channel.ack(msg)
}else{
if(newstryDownload == 10){
textLogger.warn('text XML download failed after 10 attempts. ');
console.log('text XML download failed after 10 attempts.')
channel.ack(msg)
}else{
setTimeout(() => {
newsTextDownload(newsTextDownloadController)
}, 1500);
}
}
}
}
else if(element.type == "graphic"){
log4js.configure({
appenders: { cheese: { type: "file", filename: "./Logs/Graphic/graphic.log", maxLogSize: 10485760, backups: 3, compress: true } },
categories: { default: { appenders: ["cheese"], level: "trace" } }
});
const graphicLogger = log4js.getLogger("cheese");
var graphicIsDownloaded = false
var graphicNewsIsDownloaded = false
var fileFormat, queryFileFormat
var graphicTryDownload = 0
var downloadId = element.id
var idsplit = downloadId.replace(/:/g, '_');
var path = './AA/' + idsplit + '.xml'
fileFormat = "jpeg"
queryFileFormat = "raster"
var dir = element.group_id.replace(/:/g, '_');
var CreateDirectory = '.\\Graphic\\'+ dir + '\\'
if (!fs.existsSync(CreateDirectory)){
fs.mkdirSync(CreateDirectory, { recursive: true });
graphicLogger.trace("Graphic folder created." + CreateDirectory);
console.log('Graphic folder created.' + CreateDirectory)
}
if (fs.existsSync('./Graphic/' + dir + '/' +idsplit + '.' + fileFormat)) {
var stats = fs.statSync('./Graphic/' + dir + '/' +idsplit + '.' + fileFormat)
var fileSizeInBytes = stats.size;
// Convert the file size to megabytes (optional)
graphicLogger.trace(fileSizeInBytes);
console.log(fileSizeInBytes)
if(fileSizeInBytes < 450){
fs.unlink('./Graphic/' + dir + '/' +idsplit + '.' + fileFormat, function (err) {
if (err) throw err;
// if no error, file has been deleted successfully
graphicLogger.trace("Graphic file is less 1 KB. Graphic deleted.");
console.log("Graphic file is less 1 KB. Graphic deleted.");
});
setTimeout(() => {
graphicDownload(graphicNewsDownload)
}, 7500);
}else{
graphicIsDownloaded = true
graphicLogger.trace('Graphic downloaded. Check News XML ' + graphicIsDownloaded);
console.log('Graphic downloaded. Check News XML ' + graphicIsDownloaded)
graphicNewsDownload(graphicNewsIsSuccess)
}
}else{
graphicDownload(graphicNewsDownload)
}
function graphicDownload(callback){
axios.get('https://api.aa.com.tr/abone/document/' + downloadId + '/' + queryFileFormat, {responseType: "stream",auth: {
username: aauser.aaUserName,
password: aauser.aaPassword
}} )
.then(async response => {
var graphic = fs.createWriteStream('./Graphic/' + dir + '/' +idsplit + '.' + fileFormat)
const p = response.data.pipe(graphic);
p.on('finish', () => {
graphicLogger.trace('Grafik başarılı bir şekilde indirildi.' +idsplit + '.' + fileFormat);
console.log('grafik basarili indirildi.')
graphicIsDownloaded = true
setTimeout(() => {
callback(graphicNewsDownload)
}, 2000);
});
})
.catch(error => {
graphicLogger.error(error);
console.log(error)
if(error){
graphicTryDownload = graphicTryDownload + 1
if(graphicTryDownload == 10){
graphicNewsIsSuccess()
}else{
setTimeout(() => {
graphicLogger.warn('Graphic download is unsuccessful.' + graphicTryDownload);
console.log('Graphic download is unsuccessful.' + graphicTryDownload)
graphicDownload(graphicNewsDownload)
}, 3000);
}
}
});
}
function graphicNewsDownload(callback){
if(fs.existsSync('./AA/' + idsplit + '.xml')){
graphicLogger.trace("Don't download again, There is graphic XML: " + idsplit + '.xml');
console.log("Don't download again, There is graphic XML: ")
graphicNewsIsDownloaded = true
graphicNewsIsSuccess()
}else{
axios.get('https://api.aa.com.tr/abone/document/'+downloadId+'/newsml29',
{responseType: "stream",auth: {
username: aauser.aaUserName,
password: aauser.aaPassword
}} )
.then(response => {
if(response.success == false){
graphicLogger.warn('Graphic news download is unsuccessful after ' + graphicTryDownload + 'attemps.');
console.log('Graphic news download is unsuccessful.')
graphicTryDownload = graphicTryDownload + 1
setTimeout(() => {
graphicNewsDownload(graphicNewsIsSuccess)
}, 2000);
}else{
// Saving file to working directory
response.data.pipe(fs.createWriteStream('./NewAA/' + idsplit + '.xml'));
response.data.pipe(fs.createWriteStream('./AA/' + idsplit + '.xml'));
graphicLogger.trace('Graphic News XML is downloaded: ' + idsplit + '.xml');
console.log('Graphic News XML is downloaded.')
graphicNewsIsDownloaded = true
callback(graphicNewsIsSuccess)
}
})
.catch(error => {
graphicLogger.error(error);
console.log(error)
if(error){
graphicTryDownload = graphicTryDownload + 1
if(graphicTryDownload == 10){
graphicNewsIsSuccess()
}else{
setTimeout(() => {
graphicLogger.warn('Graphics download is unsuccessful. ' + graphicTryDownload);
console.log('Graphics download is unsuccessful. ' + graphicTryDownload)
graphicNewsDownload(graphicNewsIsSuccess)
}, 3000);
}
}
});
}
}
function graphicNewsIsSuccess(){
if(graphicNewsIsDownloaded == true && graphicIsDownloaded == true){
graphicLogger.trace('Graphic and News download is successful. ' + 'graphicNewsIsDownloaded: ' + graphicIsDownloaded + ' graphicNewsIsDownloaded: ' + graphicNewsIsDownloaded);
console.log('Graphic and News download is successful. ' + 'graphicNewsIsDownloaded: ' + graphicIsDownloaded + ' graphicNewsIsDownloaded: ' + graphicNewsIsDownloaded )
channel.ack(msg)
}else if(graphicTryDownload == 10){
graphicLogger.warn('The video download is unsuccessful after 10 attemps. ' + 'graphicNewsIsDownloaded: ' + graphicIsDownloaded + ' graphicNewsIsDownloaded: ' + graphicNewsIsDownloaded)
console.log('The video download is unsuccessful after 10 attemps. ' + 'graphicNewsIsDownloaded: ' + graphicIsDownloaded + ' graphicNewsIsDownloaded: ' + graphicNewsIsDownloaded)
channel.ack(msg)
}else{
graphicDownload(graphicNewsDownload)
}
}
}
}, {noAck:false})
}
catch (error) {
console.log(error)
}
}
consumeTask()