-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateJsonFiles.mjs
287 lines (260 loc) · 9.54 KB
/
generateJsonFiles.mjs
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
import { writeFile } from "fs/promises";
import Database from 'better-sqlite3';
// Params
const databasePath = 'GamesPassionFR.db';
const FILES = {
"BACKLOG": "src/app/api/backlog/backlog.json",
"GAMES": "src/app/api/games/games.json",
"SERIES": "src/app/api/series/series.json",
"TESTS": "src/app/api/tests/tests.json",
"PLATFORMS": "src/app/api/platforms/platforms.json",
"GENRES": "src/app/api/genres/genres.json",
"PLANNING": "src/app/api/planning/planning.json",
"STATS": "src/app/api/stats/stats.json",
"PAST_GAMES": "src/app/api/planning/past-planning.json",
"DLCS": "src/app/api/dlcs/dlcs.json",
"IDENTIFIERS": "src/app/api/random/identifiers.json"
}
const db = new Database(databasePath, {
readonly: true
});
//db.pragma('journal_mode = WAL');
// Helper Functions
function stringifyJSON(payload) {
function parseIfJsonString(value) {
if (typeof value === 'string' && (value.startsWith('{') || value.startsWith('['))) {
try {
return JSON.parse(value);
} catch (e) {
return value;
}
}
return value;
}
return JSON.stringify(payload, function(key, value) {
if (value === null) {
// Exclude null values
return undefined;
}
return parseIfJsonString(value);
}, "\t");
}
function normaliazeDuration(duration) {
// Turn it into seconds
let totalInSeconds = [
duration.hours * 3600,
duration.minutes * 60,
duration.seconds
].reduce( (acc, total) => acc + total, 0);
// Time to normalize the result
let new_hours = Math.floor(totalInSeconds / 3600);
totalInSeconds %= 3600;
let new_minutes = Math.floor(totalInSeconds / 60);
let new_seconds = totalInSeconds % 60;
return {
hours: new_hours,
minutes: new_minutes,
seconds : new_seconds
}
}
/**
* Extracts platforms from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSavePlatforms(db) {
const extractPlatformsStmt = db.prepare("SELECT id, name FROM platforms");
const platforms = extractPlatformsStmt.all();
await writeFile(
FILES.PLATFORMS,
stringifyJSON(platforms),
"utf-8"
);
console.log(`${FILES.PLATFORMS} successfully written`);
}
/**
* Extracts genres from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSaveGenres(db) {
const extractGenresStmt = db.prepare("SELECT id, name FROM genres");
const genres = extractGenresStmt.all();
await writeFile(
FILES.GENRES,
stringifyJSON(genres),
"utf-8"
);
console.log(`${FILES.GENRES} successfully written`);
}
/**
* Extracts backlog from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSaveBacklog(db) {
const extractBacklogStmt = db.prepare("SELECT id, title, platform, notes FROM backlog");
const backlog = extractBacklogStmt.all();
await writeFile(
FILES.BACKLOG,
stringifyJSON(backlog),
"utf-8"
);
console.log(`${FILES.BACKLOG} successfully written`);
}
/**
* Extracts planning from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSavePlanning(db) {
const extractPlanningStmt = db.prepare("SELECT * FROM games_in_future gif INNER JOIN games g ON g.id == gif.id");
const planning = extractPlanningStmt.all();
await writeFile(
FILES.PLANNING,
stringifyJSON(planning),
"utf-8"
);
console.log(`${FILES.PLANNING} successfully written`);
}
/**
* Extracts games from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSaveGames(db) {
// WHERE condition needed to not display DLCS and games together
const extractGamesList = db.prepare("SELECT * FROM games_in_present WHERE id NOT IN (SELECT dlc FROM games_dlcs)");
const gamesList = extractGamesList.all();
await writeFile(
FILES.GAMES,
stringifyJSON(gamesList),
"utf-8"
);
console.log(`${FILES.GAMES} successfully written`);
}
/**
* Extracts series from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSaveSeries(db) {
const extractSeriesStmt = db.prepare("SELECT * FROM series_as_json");
const series = extractSeriesStmt.all();
await writeFile(
FILES.SERIES,
stringifyJSON(series),
"utf-8"
);
console.log(`${FILES.SERIES} successfully written`);
}
/**
* Extracts tests from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSaveTests(db) {
const extractTestsStmt = db.prepare("SELECT title, videoId, playlistId, platform FROM tests");
const tests = extractTestsStmt.all();
await writeFile(
FILES.TESTS,
stringifyJSON(tests),
"utf-8"
);
console.log(`${FILES.TESTS} successfully written`);
}
/**
* Extracts stats from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSaveStats(db) {
const genresStats = db.prepare("SELECT * FROM genres_stats").all();
const platformStats = db.prepare("SELECT * FROM platforms_stats").all();
const games_total_time = db.prepare("SELECT * FROM games_total_time").get();
const games_total_time_available = db.prepare("SELECT * FROM games_available_time").get();
const games_total_time_unavailable = db.prepare("SELECT * FROM games_unavailable_time").get();
// where condition needed to exclude dlc from game resultset
const total_games = db.prepare("SELECT COUNT(*) FROM games WHERE id NOT IN (SELECT dlc FROM games_dlcs)").pluck().get();
const total_game_available = db.prepare("SELECT COUNT(*) FROM games_in_present WHERE id NOT IN (SELECT dlc FROM games_dlcs)").pluck().get();
const total_game_unavailable = db.prepare("SELECT COUNT(*) FROM games_in_future WHERE id NOT IN (SELECT dlc FROM games_dlcs)").pluck().get();
// Whereas counting dlcs is easy
const total_dlcs = db.prepare("SELECT COUNT(*) FROM games_dlcs").pluck().get();
const total_dlcs_available = db.prepare("SELECT COUNT(*) FROM games_in_present WHERE id IN (SELECT dlc FROM games_dlcs)").pluck().get();
const total_dlcs_unavailable = db.prepare("SELECT COUNT(*) FROM games_in_future WHERE id IN (SELECT dlc FROM games_dlcs)").pluck().get();
const result = {
"platforms": platformStats,
"genres": genresStats,
"general": {
"channel_start_date": "2014-04-15T17:35:16+00:00",
"games": {
"total": total_games,
"total_available": total_game_available,
"total_unavailable": total_game_unavailable,
},
"dlcs": {
"total": total_dlcs,
"total_available": total_dlcs_available,
"total_unavailable": total_dlcs_unavailable,
},
"duration": {
"total": normaliazeDuration(games_total_time),
"total_available": normaliazeDuration(games_total_time_available),
"total_unavailable": normaliazeDuration(games_total_time_unavailable)
}
}
}
await writeFile(
FILES.STATS,
stringifyJSON(result),
"utf-8"
);
console.log(`${FILES.STATS} successfully written`);
}
/**
* Extracts games from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSavePastGames(db) {
const extractGamesList = db.prepare("SELECT * FROM games_in_past");
const gamesList = extractGamesList.all();
await writeFile(
FILES.PAST_GAMES,
stringifyJSON(gamesList),
"utf-8"
);
console.log(`${FILES.PAST_GAMES} successfully written`);
}
/**
* Extracts dlcs from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSaveDLCS(db) {
const extractDLCSStmt = db.prepare("SELECT * FROM dlcs_as_json");
const dlcs = extractDLCSStmt.all();
await writeFile(
FILES.DLCS,
stringifyJSON(dlcs),
"utf-8"
);
console.log(`${FILES.DLCS} successfully written`);
}
/**
* Extracts game identifiers from the database and saves them to a file.
* @param {import('better-sqlite3').Database} db - The database instance
*/
async function extractAndSaveRandomList(db) {
// We can pick up also DLC to have a complete list
const extractGamesList = db.prepare("SELECT videoId, playlistId FROM games_in_present");
const games = extractGamesList.all();
await writeFile(
FILES.IDENTIFIERS,
stringifyJSON(games),
"utf-8"
);
console.log(`${FILES.IDENTIFIERS} successfully written`);
}
// Operations time
await extractAndSavePlatforms(db)
await extractAndSaveGenres(db)
await extractAndSaveBacklog(db)
await extractAndSavePlanning(db)
await extractAndSaveGames(db)
await extractAndSaveSeries(db)
await extractAndSaveTests(db)
await extractAndSaveStats(db)
await extractAndSavePastGames(db);
await extractAndSaveDLCS(db);
await extractAndSaveRandomList(db);