-
Notifications
You must be signed in to change notification settings - Fork 7
/
fetch-spreadsheet.js
123 lines (110 loc) · 3.51 KB
/
fetch-spreadsheet.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
require('isomorphic-fetch');
const fs = require('fs');
const gsheets = require('gsheets');
const path = require('path');
const addInfoToCuriosities = function(curiosities) {
// agregarmos la foto de perfil a las curiosidad
return curiosities.map((curiosity) =>
Object.assign({}, curiosity, {
Foto: getPlayerImage(curiosity.Foto)
})
);
};
const addInfoToPositions = function(positions) {
// devolvemos un nuevo array compuesto de:
// - la información de los jugadores que ya tenemos
// - la posición calculada
// - la foto de perfil
return positions.map((position) =>
Object.assign({}, position, {
Foto: getPlayerImage(position.Foto),
Posicion: getPositionNumberFromName(positions, position['Jugador/a'])
})
);
};
// generamos la URL de la foto del jugador
const getPlayerImage = function(pictureURL) {
return pictureURL
? `https://avatars.io/${pictureURL}/large`
: 'https://placehold.it/32x32/bbbbbb/bbbbbb;';
};
// obtenemos la posición del jugador especificado por nombre
const getPositionNumberFromName = function(positions, name) {
let currentIndex = 1;
let previousPosition = null;
// `positions` es un array de jugadores ordenados por:
// - Puntos (DESC)
// - Ganados (DESC)
// - Jugados (DESC)
// - Nombre (ASC)
// esto nos sirve para poder usar `some` y comparar si jugadores consecutivos en el array
// tienen los mismos puntos, partidos ganados y jugados, en ese caso, uno va a estar abajo del
// otro en las posiciones por el orden alfabético. En ese caso, queremos que la posición sea
// la misma
positions.some((currentPosition, index) => {
if (previousPosition) {
if (
currentPosition.Puntos !== previousPosition.Puntos ||
currentPosition.Ganados !== previousPosition.Ganados ||
currentPosition.Jugados !== previousPosition.Jugados
) {
currentIndex = currentIndex + 1;
}
}
previousPosition = currentPosition;
return currentPosition['Jugador/a'] === name;
});
return currentIndex;
};
async function generateData() {
const dataPromises = [
gsheets
.getWorksheetById(
process.env.REACT_APP_SPREADSHEET_ID,
process.env.REACT_APP_POSITIONS_WORKSHEET_ID
)
.then((worksheet) => worksheet.data),
gsheets
.getWorksheetById(
process.env.REACT_APP_SPREADSHEET_ID,
process.env.REACT_APP_COVERS_WORKSHEET_ID
)
.then((worksheet) => worksheet.data),
gsheets
.getWorksheetById(
process.env.REACT_APP_SPREADSHEET_ID,
process.env.REACT_APP_CURIOSITIES_WORKSHEET_ID
)
.then((worksheet) => worksheet.data)
];
if (process.env.REACT_APP_SHIRTS_WORKSHEET_ID) {
dataPromises.push(
gsheets
.getWorksheetById(
process.env.REACT_APP_SPREADSHEET_ID,
process.env.REACT_APP_SHIRTS_WORKSHEET_ID
)
.then((worksheet) => worksheet.data)
);
}
Promise.all(dataPromises)
.then(([positions, covers, curiosities, shirts]) => {
// guardo la información en disco para ser usado desde la app
fs.writeFileSync(
path.resolve('./', 'src', 'dataset.json'),
JSON.stringify(
{
positions: addInfoToPositions(positions),
covers,
curiosities: addInfoToCuriosities(curiosities),
shirts
},
null,
2
)
);
console.log('spreadsheet generated successfully 😎');
})
.catch(console.error);
}
generateData();