· 5 years ago · Aug 03, 2020, 03:58 PM
1'use strict'
2
3const axios = require('axios');
4
5var rcsSetting = {
6 language: "fr",
7 location: "fr",
8 server: "http://telefoot.wtvision.cloud:3000"
9}
10
11var instance = axios.create({
12 baseURL: 'http://telefoot.wtvision.cloud:3000',
13 timeout: 5000,
14 headers: {}
15});
16
17var getData_direct = async url => {
18 try {
19 var response = await instance.get(url);
20 return response.data;
21 } catch (error) {
22 //console.log(error);
23 var statusmsg = ''
24 var statustextmsg = ''
25 if (error.message != "Request failed with status code 404") {
26 console.log(error);
27 }
28 if (error.message == "Request failed with status code 404") {
29 console.log("404---error")
30 }
31 if (error.response) {
32 statusmsg = error.response.status;
33 statustextmsg = error.response.statusText;
34 }
35 return {
36 error: "error calling api",
37 details: error.message,
38 status: statusmsg,
39 statustext: statustextmsg,
40 url: url
41 }
42 }
43};
44
45async function getData(url, payload) {
46 var data = await getData_direct(url)
47
48 if (data.error || data.name == "Error") {
49 if (data.details != "Request failed with status code 404") {
50 console.log("\nERROR(1)\n")
51 // payload["Error"] = data.details.toString()
52 payload["Error"] = data.message
53 return [false, data];
54 } else {
55 console.log("\nERROR(2):", data.details, "\n")
56 payload["Error"] = "CODE 404"
57 return [false, data];
58 }
59 }
60 if (data == "" || data["status"] == 404) {
61 payload["Error"] = "CODE 404"
62 return [false, data];
63 }
64
65 return [true, data];
66}
67
68async function fillStandings(params, payload) {
69
70 if (params.hasOwnProperty('league') != true) {
71 payload["Error"] = "Select a League first"
72 return false;
73 }
74 if (!params.league) {
75 payload["Error"] = "Select a League first"
76 return false;
77 }
78
79 var lang = '';
80 if (params.hasOwnProperty("language") && params.language != '') {
81 lang = params.language;
82 } else {
83 lang = rcsSetting.language
84 }//takes the language sent as parameter, if any. Otherwise, set default
85
86 let url = "/api/soccer/leagues/" + params.league + "/standings?format=json&target=" + params.template + "&translate=" + rcsSetting.location + lang;
87 //var url = "/bein/soccer/leagues/" + params.league + "/standings?format=json&target="+params.template+"&translate="+rcsSetting.location+lang;
88
89 if (params.hasOwnProperty("itemsperpage") == true && params.itemsperpage != "") {
90 url += "&itemsperpage=" + params.itemsperpage;
91 } else {
92 payload["Error"] = "Select Items per Page"
93 return false;
94 }
95
96 if (params.hasOwnProperty("page") == true && params.page != "") {
97 url += "&page=" + params.page;
98 } else {
99 payload["Error"] = "Select a Page"
100 return false;
101 }
102
103 if (params.hasOwnProperty("fillMatchweek") == true && params.fillMatchweek == "true") {
104 url += "&fillMatchweek=true";
105 }
106
107 var [valid, data] = await getData(url, payload)
108 if (valid === false) {
109 return false;
110 }
111
112 console.log(url);
113
114 data.template.Setters.forEach(
115 function (setter) {
116 payload[setter.TabId] = setter.Value
117 });
118 /*if (data.hasOwnProperty('Standings') != true){
119 payload["Error"] = "Data has no Standings Available"
120 return false;
121 }
122 if (!data.Standings){
123 payload["Error"] = "Data has no Standings Available"
124 return false;
125 }
126
127 // INSERT KEY: VALUE INSIDE PAYLOAD DICTIONARY
128 data.Standings.forEach(
129 function(item, index, arr) {
130 payload["tStandingName0" + (index+1)] = item.TeamName.toUpperCase()
131 payload["tStandingRank0" + (index+1)] = item.Rank
132 payload["tStandingPlayed0" + (index+1)] = item.Played
133 payload["tStandingPoints0" + (index+1)] = item.Points
134 });*/
135
136 payload["Error"] = "Data Imported"
137
138 return true;
139}
140
141async function fillResults(params, payload) {
142
143 if (params.hasOwnProperty('league') != true) {
144 payload["Error"] = "Select a League"
145 return false;
146 }
147 if (!params.league) {
148 payload["Error"] = "Select a League"
149 return false;
150 }
151
152 var lang = '';
153 if (params.hasOwnProperty("language") && params.language != '') {
154 lang = params.language;
155 } else {
156 lang = rcsSetting.language
157 }//takes the language sent as parameter, if any. Otherwise, set default
158
159 let url = "/api/soccer/leagues/" + params.league + "/fixtures?format=json&target=" + params.template + "&translate=" + rcsSetting.location + lang;
160
161 // this section adds extraparameters to the url if passed
162 if (params.hasOwnProperty("season") == true && params.season != "") {
163 url += "&season=" + params.season;
164 }
165 if (params.hasOwnProperty("matchweek") == true && params.matchweek != "") {
166 url += "&matchweek=" + params.matchweek;
167 } else
168 {
169 payload["Error"] = "Select a Matchweek"
170 return false;
171 }
172 if (params.hasOwnProperty("days") == true && params.days != "") {
173 url += "&days=" + params.days;
174 } else
175 {
176 payload["Error"] = "Select a number of Days per Page"
177 return false;
178 }
179 if (params.hasOwnProperty("page") == true && params.page != "") {
180 url += "&page=" + params.page;
181 } else
182 {
183 payload["Error"] = "Select a Page"
184 return false;
185 }
186 //end of modification of url
187
188 console.log(url);
189
190 var [valid, data] = await getData(url, payload)
191 if (valid === false) {
192 return false;
193 }
194
195 console.log(data.result);
196
197 data.result.Setters.forEach(
198 function (setter) {
199 payload[setter.TabId] = setter.Value
200 });
201
202 payload["lgCompetitionBadge"] = "Data Imported"
203 payload["Error"] = "Data Imported"
204
205 return true;
206}
207
208async function getSeasonStats(params, payload) {
209
210 if (params.hasOwnProperty('league') != true) {
211 payload["Error"] = "Select a League first"
212 return false;
213 }
214 if (!params.league) {
215 payload["Error"] = "Select a League first"
216 return false;
217 }
218
219 let url = "/api/soccer/seasonstats/" + params.league + "/121le8unjfzug3iu9pgkqa1c7";// + params.team;
220
221 console.log(url)
222 var [valid, data] = await getData(url, payload)
223 if (valid === false) {
224 return false;
225 }
226
227 //console.log(data)
228 return data;
229}
230
231async function readData_OLD(params) {
232 console.log("params", params);
233 return { "status": "olha o status fresquinho" };
234 var dict = { "3x4x1x2": "ooj" };
235
236 //Tactics
237 if (params.hasOwnProperty('tactical') == true) {
238 if (params.tactical == "3x4x1x2") {
239 console.log("Sending Tactic 3x4x1x2")
240 return {
241 "2200position": "156 -40 0",
242 "2300position": "1 -40 0",
243 "2400position": "-160 -40 0",
244 "2500position": "170 13 0",
245 "2600position": "73 -10 0",
246 "2700position": "-71 -10 0",
247 "2800position": "-170 13 0",
248 "2900position": "0 43 0",
249 "3000position": "76 75 0",
250 "3100position": "-77 75 0",
251 "status": "Loaded 3x4x1x2"
252 }
253
254 } else if (params.tactical == "3x4x2x1") {
255 console.log("Sending Tactic 3x4x2x1")
256 return {
257 "2200position": "156 -40 0",
258 "2300position": "1 -40 0",
259 "2400position": "-160 -40 0",
260 "2500position": "170 13 0",
261 "2600position": "73 -10 0",
262 "2700position": "-71 -10 0",
263 "2800position": "-170 13 0",
264 "2900position": "73 50 0",
265 "3000position": "-76 50 0",
266 "3100position": "0 75 0",
267 "status": "Loaded 3x4x2x1"
268 }
269
270 } else if (params.tactical == "3x4x3") {
271 console.log("Sending Tactic 3x4x3")
272 return {
273 "2200position": "156 -40 0",
274 "2300position": "-1 -40 0",
275 "2400position": "-160 -40 0",
276 "2500position": "170 30 0",
277 "2600position": "73 5 0",
278 "2700position": "-71 5 0",
279 "2800position": "-170 30 0",
280 "2900position": "148 75 0",
281 "3000position": "1 75 0",
282 "3100position": "-148 75 0",
283 "status": "Loaded 3x4x3"
284 }
285
286 } else if (params.tactical == "3x5x1x1") {
287 console.log("Sending Tactic 3x5x1x1")
288 return {
289 "2200position": "150 -45 0",
290 "2300position": "0 -45 0",
291 "2400position": "-150 -45 0",
292 "2500position": "180 10 0",
293 "2600position": "125 -10 0",
294 "2700position": "0 -10 0",
295 "2800position": "-125 -10 0",
296 "2900position": "-180 10 0",
297 "3000position": "0 40 0",
298 "3100position": "0 75 0",
299 "status": "Loaded 3x5x1x1"
300 }
301
302 } else if (params.tactical == "3x5x2") {
303 console.log("Sending Tactic 3x5x2")
304 return {
305 "2200position": "156 -40 0",
306 "2300position": "-1 -40 0",
307 "2400position": "-160 -40 0",
308 "2700position": "-1 -9 0",
309 "2600position": "95 15 0",
310 "2800position": "-100 15 0",
311 "2500position": "168 40 0",
312 "2900position": "-168 40 0",
313 "3000position": "78 75 0",
314 "3100position": "-81 75 0",
315 "status": "Loaded 3x5x2"
316 }
317
318 } else if (params.tactical == "4x1x4x1") {
319 console.log("Sending Tactic 4x1x4x1")
320 return {
321 "2200position": "185 -40 0",
322 "2300position": "62 -40 0",
323 "2400position": "-62 -40 0",
324 "2500position": "-185 -40 0",
325 "2600position": "0 5 0",
326 "2700position": "178 40 0",
327 "2800position": "59 40 0",
328 "2900position": "-59 40 0",
329 "3000position": "-178 40 0",
330 "3100position": "0 75 0",
331 "status": "Loaded 4x1x4x1"
332 }
333
334 } else if (params.tactical == "4x2x2x2") {
335 console.log("Sending Tactic 4x2x2x2")
336 return {
337 "2200position": "185 -40 0",
338 "2300position": "62 -40 0",
339 "2400position": "-62 -40 0",
340 "2500position": "-185 -40 0",
341 "2600position": "80 5 0",
342 "2700position": "-80 5 0",
343 "2800position": "80 40 0",
344 "2900position": "-80 40 0",
345 "3000position": "80 75 0",
346 "3100position": "-80 75 0",
347 "status": "Loaded 4x2x2x2"
348 }
349
350 } else if (params.tactical == "4x2x3x1") {
351 console.log("Sending Tactic 4x2x3x1")
352 return {
353 "2200position": "185 -40 0",
354 "2300position": "62 -40 0",
355 "2400position": "-62 -40 0",
356 "2500position": "-185 -40 0",
357 "2600position": "100 5 0",
358 "2700position": "-100 5 0",
359 "2800position": "150 40 0",
360 "2900position": "0 40 0",
361 "3000position": "-150 40 0",
362 "3100position": "0 75 0",
363 "status": "Loaded 4x2x3x1"
364 }
365
366 } else if (params.tactical == "4x3x2x1") {
367 console.log("Sending Tactic 4x3x2x1")
368 return {
369 "2200position": "185 -40 0",
370 "2300position": "62 -40 0",
371 "2400position": "-62 -40 0",
372 "2500position": "-185 -40 0",
373 "2600position": "146 5 0",
374 "2700position": "0 5 0",
375 "2800position": "-149 5 0",
376 "2900position": "82 40 0",
377 "3000position": "-90 40 0",
378 "3100position": "0 75 0",
379 "status": "Loaded 4x3x2x1"
380 }
381
382 }
383 else if (params.tactical == "4x3x1x2") {
384 console.log("Sending Tactic 4x3x1x2")
385 return {
386 "2200position": "185 -40 0",
387 "2300position": "62 -40 0",
388 "2400position": "-62 -40 0",
389 "2500position": "-185 -40 0",
390 "2600position": "146 5 0",
391 "2700position": "0 5 0",
392 "2800position": "-149 5 0",
393 "2900position": "0 43 0",
394 "3000position": "76 75 0",
395 "3100position": "-77 75 0",
396 "status": "Loaded 4x3x2x1"
397 }
398 }
399 else if (params.tactical == "4x3x3") {
400 console.log("Sending Tactic 4x3x3")
401 return {
402 "2200position": "185 -40 0",
403 "2300position": "62 -40 0",
404 "2400position": "-62 -40 0",
405 "2500position": "-185 -40 0",
406 "2600position": "146 17 0",
407 "2700position": "0 17 0",
408 "2800position": "-149 17 0",
409 "2900position": "139 75 0",
410 "3000position": "-1 75 0",
411 "3100position": "-142 75 0",
412 "status": "Loaded 4x3x3"
413 }
414
415 } else if (params.tactical == "4x4x1x1") {
416 console.log("Sending Tactic 4x4x1x1")
417 return {
418 "2200position": "185 -40 0",
419 "2300position": "62 -40 0",
420 "2400position": "-62 -40 0",
421 "2500position": "-185 -40 0",
422 "2600position": "180 5 0",
423 "2700position": "60 5 0",
424 "2800position": "-60 5 0",
425 "2900position": "-180 5 0",
426 "3000position": "0 40 0",
427 "3100position": "0 75 0",
428 "status": "Loaded 4x4x1x1"
429 }
430
431 } else if (params.tactical == "4x4x2_diamond") {
432 console.log("Sending Tactic 4x4x2_diamond")
433 return {
434 "2200position": "185 -40 0",
435 "2300position": "62 -40 0",
436 "2400position": "-62 -40 0",
437 "2500position": "-185 -40 0",
438 "2600position": "-1 -9 0",
439 "2700position": "94 15 0",
440 "2800position": "-99 15 0",
441 "2900position": "-1 40 0",
442 "3000position": "78 75 0",
443 "3100position": "-81 75 0",
444 "status": "Loaded 4x4x2_diamond"
445 }
446
447 } else if (params.tactical == "4x4x2_line") {
448 console.log("Sending Tactic 4x4x2_line")
449 return {
450 "2200position": "185 -40 0",
451 "2300position": "62 -40 0",
452 "2400position": "-62 -40 0",
453 "2500position": "-185 -40 0",
454 "2600position": "168 31 0",
455 "2700position": "63 10 0",
456 "2800position": "-66 10 0",
457 "2900position": "-166 31 0",
458 "3000position": "78 75 0",
459 "3100position": "-81 75 0",
460 "status": "Loaded 4x4x2_line"
461 }
462
463 } else if (params.tactical == "5x3x2") {
464 console.log("Sending Tactic 5x3x2")
465 return {
466 "2200position": "180 -10 0",
467 "2300position": "125 -30 0",
468 "2400position": "-0 -30 0",
469 "2500position": "-125 -30 0",
470 "2600position": "-180 -10 0",
471 "2700position": "125 30 0",
472 "2800position": "0 30 0",
473 "2900position": "-125 30 0",
474 "3000position": "65 70 0",
475 "3100position": "-65 70 0",
476 "status": "Loaded 5x3x2"
477 }
478
479 } else if (params.tactical == "5x4x1") {
480 console.log("Sending Tactic 5x4x1")
481 return {
482 "2200position": "180 -10 0",
483 "2300position": "125 -30 0",
484 "2400position": "-0 -30 0",
485 "2500position": "-125 -30 0",
486 "2600position": "-180 -10 0",
487 "2700position": "178 30 0",
488 "2800position": "59 30 0",
489 "2900position": "-59 30 0",
490 "3000position": "-178 30 0",
491 "3100position": "0 70 0",
492 "status": "Loaded 5x4x1"
493 }
494 } else if (params.tactical == "4x5x1") {
495 console.log("Sending Tactic 4x5x1")
496 return {
497 "2200position": "185 -40 0",
498 "2300position": "62 -40 0",
499 "2400position": "-62 -40 0",
500 "2500position": "-185 -40 0",
501 "2600position": "-1 -9 0",
502 "2700position": "95 15 0",
503 "2800position": "-100 15 0",
504 "2900position": "168 40 0",
505 "3000position": "-168 40 0",
506 "3100position": "0 70 0",
507 "status": "Loaded 4x5x1"
508 }
509 } else {
510 console.log("Missing Tactic sending - 4x4x2")
511 return {
512 "2200position": "185 -40 0",
513 "2300position": "62 -40 0",
514 "2400position": "-62 -40 0",
515 "2500position": "-185 -40 0",
516 "2600position": "168 31 0",
517 "2700position": "63 10 0",
518 "2800position": "-66 10 0",
519 "2900position": "-166 31 0",
520 "3000position": "78 75 0",
521 "3100position": "-81 75 0",
522 "status": "!!!MISSING!!! - " + params.tactical
523 }
524 }
525 console.log("here");
526 }
527
528
529 if (params.hasOwnProperty('league') != true) {
530 return "";
531 }
532 if (!params.league) {
533 return "";
534 }
535
536 //Standings
537 var standings = ["98700", "98701", "98702", "98703", "98704", "98705", "98710", "98711"];
538 if (standings.includes(params.template) == true) {
539 console.log("Standings")
540
541 var lang = '';
542
543 if (params.hasOwnProperty("language") && params.language != '') {
544 lang = params.language;
545 } else {
546 lang = rcsSetting.language
547 }//takes the language sent as parameter, if any. Otherwise, set default
548
549 var url = "/bein/soccer/leagues/" + params.league + "/standings?format=json&target=" + params.template + "&translate=" + rcsSetting.location + lang;
550
551 // this section adds extraparameters to the url if passed
552 if (params.hasOwnProperty("season") == true && params.season != "") {
553 url += "&season=" + params.season;
554 }
555
556 if (params.hasOwnProperty("matchday") == true && params.season != "") {
557 url += "&matchday=" + params.matchday;
558 }
559
560 var paged = ["98701", "98703"];
561 if (paged.includes(params.template) == true) {
562 url += "&Start=1";
563 url += "&Rows=10";
564 }
565 paged = ["98705", "98711"];
566 if (paged.includes(params.template) == true) {
567 url += "&Start=1";
568 url += "&Rows=12";
569 }
570 //end of modification of url
571
572 console.log(url);
573 var data = await getData_direct(url)
574
575 if (data.error || data.name == "Error") {
576 if (data.details != "Request failed with status code 404") {
577 console.log("\nERROR(1)\n")
578 //return JSON.parse("{}")
579 return {
580 "1010": data.details.toString(),
581 "1011": "ERROR!"
582 }
583 } else {
584 console.log("\nERROR(2):", data.details, "\n")
585 return { "1010": "CODE 404" }
586 }
587 }
588
589 var payload = {}
590 //This will not iterate if the result is empty or if the response was 'not found'
591 if (data != "" && data["status"] != 404) {
592
593 data.template.Setters.forEach(function (setter) {
594 payload[setter.TabId] = setter.Value
595 });
596 console.log("READ SUCCESSFUL")
597 } else {
598 return { "1010": "ERROR" }
599 }
600
601 console.log(payload)
602 return payload;
603 }
604
605 //Fixtures
606 var fixtures = ["98610", "98611", "98612", "98613", "98614", "98615", "98616", "98617"];
607 if (fixtures.includes(params.template) == true) {
608 console.log("Fixtures");
609
610
611 var lang = '';
612
613 if (params.hasOwnProperty("language") && params.language != '') {
614 lang = params.language;
615 } else {
616 lang = rcsSetting.language
617 }//takes the language sent as parameter, if any. Otherwise, set default
618
619 var url = "/bein/soccer/leagues/" + params.league + "/fixtures?format=json&target=" + params.template + "&translate=" + rcsSetting.location + lang;
620
621 // this section adds extraparameters to the url if passed
622 if (params.hasOwnProperty("season") == true && params.season != "") {
623 url += "&season=" + params.season;
624 }
625 if (params.hasOwnProperty("matchday") == true && params.matchday != "") {
626 url += "&matchday=" + params.matchday;
627 }
628 //end of modification of url
629
630 console.log(url);
631 var data = await getData_direct(url)
632 //console.log(data)
633 //console.log(data.name)
634 if (data.name == "Error" || data.error) {
635 console.log(data)
636 if (data.message != "Request failed with status code 404") {
637 console.log("\nERROR(1)\n")
638 return JSON.parse("{}")
639 } else {
640 console.log("\nERROR(2):", data.message, "\n")
641 return { "1010": "CODE 404" }
642 }
643 }
644
645 var payload = {}
646
647 //This will not iterate if the result is empty or if the response was 'not found'
648 if (data != "" && data["status"] != 404) {
649 data.result.Setters.forEach(function (setter) {
650 payload[setter.TabId] = setter.Value
651 });
652 console.log("READ SUCCESSFUL!")
653 } else {
654 return { "1010": "ERROR" }
655 }
656
657 return payload;
658
659 }
660
661
662 return {};
663
664}
665
666async function getTeams(league) {
667
668 let url = "/api/soccer/teams/" + league;
669 let payload = {}
670
671 var [valid, data] = await getData(url, payload)
672 if (valid === false) {
673 return false;
674 }
675
676 return data;
677}
678
679async function getPlayers(team) {
680
681 let url = "/api/soccer/squads/" + team;
682 let payload = {}
683
684 var [valid, data] = await getData(url, payload)
685 if (valid === false) {
686 return false;
687 }
688
689 return data.person;
690}
691
692function compareValues(key, order = 'asc') {
693 return function innerSort(a, b) {
694 if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
695 return 0;
696 }
697
698 const varA = (typeof a[key] === 'string')
699 ? a[key].toUpperCase() : a[key];
700 const varB = (typeof b[key] === 'string')
701 ? b[key].toUpperCase() : b[key];
702
703 let comparison = 0;
704 if (varA > varB) {
705 comparison = 1;
706 } else if (varA < varB) {
707 comparison = -1;
708 }
709 return (
710 (order === 'desc') ? (comparison * -1) : comparison
711 );
712 };
713 }
714
715module.exports = {
716
717 fillStandings,
718 getSeasonStats,
719 fillResults,
720 getTeams,
721 getPlayers,
722 compareValues
723}