-
Notifications
You must be signed in to change notification settings - Fork 0
/
velibrairie.js
515 lines (472 loc) · 15.2 KB
/
velibrairie.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
/******************************************************/
/****** Vélib'rairie : analyse de trajets Vélib *******/
/******************************************************/
//
// repository: https://github.com/lecfab/velibrairie
// utilisation: https://www.velib-metropole.fr/private/account#/my-runs
// text/plain: https://raw.githubusercontent.com/lecfab/velibrairie/main/
// text/javascript: https://cdn.jsdelivr.net/gh/lecfab/velibrairie/
// plotting: https://cdn.plot.ly/plotly-2.9.0.min.js
//
// Fabrice Lécuyer, 2022
//
//
//
/******************************************************/
/********** Data format to use it in charts ***********/
/******************************************************/
class Trip {
/**
* Transform the parsed trip into a trip object
* @constructor
* @param {object} l - Format [(year,month,day,hour,minut), elec, number, speed, (hours,minuts,seconds), price]
*/
constructor(l) {
this.date = this.formatDate(l[0]);
this.elec = this.formatVelo(l[1]);
this.number = parseInt(l[2]);
this.speed = parseFloat(l[3]);
this.duration = this.formatDuration(l[4]);
this.price = parseInt(l[5]);
}
get year() {
return this.date.getFullYear();
}
get month() {
return this.date.getMonth() + 1;
}
get day() {
return this.date.getDate();
}
get weekday() {
return (this.date.getDay() + 6) % 7;
}
get hour() {
return this.date.getHours();
}
get minute() {
return this.date.getMinutes();
}
get durMinutes() {
return this.duration[0] * 60 + this.duration[1] + this.duration[2] / 60;
}
get distance() {
return this.speed * this.durMinutes / 60;
}
/**
* Give the correct format to a duration string
* @param {string} d - Duration with Velib format: 1h 54min 32sec
*/
formatDuration(d) {
d = d.replace(/h /, "-").replace(/min /, "-").replace(/sec /, "").split("-")
if (d.length == 1) return [0, 0, parseInt(d[0])];
else if (d.length == 2) return [0, parseInt(d[0]), parseInt(d[1])];
else return [parseInt(d[0]), parseInt(d[1]), parseInt(d[2])];
}
/**
* Give the correct format to a date string
* @param {string} d - Date with Velib format: 08/02/2022 - 19:47
*/
formatDate(d) {
d = d.split("-");
let day = d[0].split("/");
return new Date(`${day[2]}/${day[1]}/${day[0]} ${d[1]}`);
// let time = d[1].split(":");
// return [parseInt(day[2]), parseInt(day[1]), parseInt(day[0]), parseInt(time[0]), parseInt(time[1])];
}
/**
* Give the correct format to a velo string
* @param {string} v - Velo with Velib format: vélo méca
* @return {number} - 0 for a normal bike, 1 for electric
*/
formatVelo(v) {
return v != "vélo méca";
}
}
//
//
/******************************************************/
/******* Extracting the data from the webpage *********/
/******************************************************/
let allTrips = [];
/**
* Navigation in the website
*/
function goToPageRuns() {
$(".navbar-item").each(function() {
if ($(this).html() == "Trajets") $(this).click();
});
}
function goToPage1() {
$(".page-item a").each(function() {
if (parseInt($(this).html()) == 1) this.click(); // no idea it fails with $(this)
});
}
function goToNextPage() {
$(".page-link span").each(function() {
if ($(this).html() == "»") $(this).click();
});
}
/**
* Find the total number of pages
*/
function getTotalPages() {
let items = $(".page-item");
let n = parseInt($(items[items.length - 3]).find("a").html())
return n;
}
/**
* Get the information of the i-th trip of the page
* @param {number} i - ID of trip in the page
* @return {object} l - Format [(year,month,day,hour,minut), elec, number, speed, (hours,minuts,seconds), price]
*/
function getTrip(i) {
let run = $(".runs")[i];
let date = $(run).find(".operation-date").html();
let type = $(run).find(".runs-item img").attr("alt");
let bike = $(run).find(".bike-number span").html().slice(2);
let speed = $(run).find(".speed").html();
let duration = $(run).find(".duration").html()
let price = $(run).find(".runs-item-content .text-center:contains(€)").html().replace(/[^0-9]*([0-9]+)[^0-9]*€.*/, '$1');
$($(".runs")[i]).html(" ");
return [date, type, bike, speed, duration, price];
}
/**
* Get the information of all trips in current page
*/
function getTripInPage() {
let result = []
let runs = $(".runs")
for (let i = 0; i < runs.length; i++) {
if ($($(".runs")[i]).html() == " " || $($(".runs")[i]).is("#loader")) continue;
result.push(new Trip(getTrip(i)));
}
return result
}
/**
* Get the information of all trips
*/
function getTripAllPages(p) {
loaderUpdate(p);
if (p == 0) return;
allTrips = allTrips.concat(getTripInPage());
goToNextPage();
// Optional: update the plot after each scrapped page
velibrairiePlot();
window.setTimeout(getTripAllPages, 1000, p - 1);
}
function getTripAll() {
allTrips = [];
getTripAllPages(getTotalPages());
}
/*
* Loading message for users
*/
function loaderCreate() {
// $(".runs").hide(100);
$(".race-tab").parent().prepend($("<div>", {
id: "loader",
class: "runs",
style: "padding: 30px; background: var(--warning)"
}).html(`Patience : le script doit encore lire <span>les informations</span> de vos trajets.`))
$(".race-tab").hide(100);
}
function loaderUpdate(p) {
if (p == 0) loaderStop();
else $("#loader span").html(p + (p > 1 ? " pages" : " page"));
}
function loaderStop() {
goToPage1();
$("#loader").hide(100);
$(".race-tab").show(100);
velibrairiePlot();
}
//
//
/******************************************************/
/***************** Plotting functions *****************/
/******************************************************/
var zone;
var params = {
color1: "#1669A9",
color2: "#619C2F",
config: { responsive: true },
linewidth: 3,
titlesize: 20,
marginTop: 100,
}
function createPlot(name) {
let id = "#" + name;
if ($(id).length == 0) {
$(".race-tab-synthesis").append($("<div>", {
id: name,
class: "col-12 plot"
}));
$(id).append($("<div class='chart'></div>"));
$(id).append($("<div class='comment'></div>"));
}
return $(id);
}
function mn2str(mn) {
let str = "";
if (mn >= 60) {
let h = parseInt(mn / 60);
str += h + "h ";
mn -= 60 * h;
}
str += parseInt(mn) + "min";
mn = mn - parseInt(mn);
if (mn > 0) {
str += " " + parseInt(mn * 60) + "sec"
}
return str;
}
function capitalFirst(str) {
return str[0].toUpperCase() + str.slice(1);
}
function plural(x, singular, plural = singular + "s") {
return x + " " + ((x > 1) ? plural : singular);
}
/*
* Shows for each weekday the number of trips and time on the bike
*/
function plotTripsPerWeekday() {
let div = createPlot("plotTripsPerWeekday");
let x = Array();
for (let i = 0; i < 7; i++) {
let date = new Date("2000-01-" + (3 + i)); // this was a Monday
x.push(capitalFirst(date.toLocaleDateString("fr-FR", {
weekday: "long"
})));
}
let ts = Array(7).fill(0);
for (t of allTrips) {
ts[t.weekday]++;
}
let ds = Array(7).fill(0);
let ds_nice = Array(7).fill(0);
for (t of allTrips) {
ds[t.weekday] += (t.durMinutes);
ds_nice[t.weekday] = mn2str(ds[t.weekday]);
}
let comment_id = 0;
div.find(".comment").html(
`Lecture : le ${x[comment_id]}, ` +
`vous avez fait ${plural(ts[comment_id], "trajet")} ` +
`pour une durée totale de ${ds_nice[comment_id]}.`);
Plotly.newPlot(div.find(".chart")[0], [{
name: "Trajets",
x: x,
y: ts,
line: { color: params.color1, width: params.linewidth, },
// hovertemplate: "%{yaxis.title.text}: %{y: }<extra></extra>",
showlegend: false
}, {
name: "Durée",
x: x,
y: ds,
text: ds_nice,
yaxis: 'y2',
line: { color: params.color2, width: params.linewidth, },
hovertemplate: "%{text}",
showlegend: false
}], {
title: {
text: 'Utilisation par jour de la semaine',
font: { size: params.titlesize }
},
margin: { t: params.marginTop },
xaxis: {
title: 'Jour de la semaine (prise du vélo)',
// showgrid: false,
zeroline: false
},
yaxis: {
title: 'Nombre de trajets',
rangemode: "tozero",
titlefont: { color: params.color1 },
tickfont: { color: params.color1 },
},
yaxis2: {
title: 'Durée des trajets (minutes)',
rangemode: "tozero",
overlaying: 'y',
side: 'right',
titlefont: { color: params.color2 },
tickfont: { color: params.color2 },
},
hovermode: "x",
}, params.config);
}
/*
* Shows for each hour of the day the number of trips and time on the bike
*/
function plotTripsPerHour() {
let div = createPlot("plotTripsPerHour");
let ts = Array(25).fill(0);
for (t of allTrips) {
ts[t.hour]++;
}
let ds = Array(25).fill(0);
let ds_nice = Array(25).fill(0);
for (t of allTrips) {
ds[t.hour] += (t.durMinutes);
ds_nice[t.hour] = mn2str(ds[t.hour]);
}
ts[24] = ts[0];
ds[24] = ds[0];
ds_nice[24] = ds_nice[0];
let comment_id = 18;
div.find(".comment").html(
`Lecture : entre ${comment_id}h et ${comment_id+1}h, ` +
`vous avez fait ${plural(ts[comment_id], "trajet")} ` +
`pour une durée totale de ${ds_nice[comment_id]}.`);
Plotly.newPlot(div.find(".chart")[0], [{
name: "Trajets",
x: [...ts.keys()],
y: ts,
line: { color: params.color1, width: params.linewidth, },
showlegend: false
}, {
name: "Durée",
x: [...ts.keys()],
y: ds,
text: ds_nice,
yaxis: 'y2',
line: { color: params.color2, width: params.linewidth, },
hovertemplate: "%{text}",
showlegend: false
}], {
title: {
text: 'Utilisation par heure de la journée',
font: { size: params.titlesize }
},
margin: { t: params.marginTop },
xaxis: {
title: 'Heure de la journée (prise du vélo)',
zeroline: false
},
yaxis: {
title: 'Nombre de trajets',
rangemode: "tozero",
titlefont: { color: params.color1 },
tickfont: { color: params.color1 },
},
yaxis2: {
title: 'Durée des trajets (minutes)',
rangemode: "tozero",
overlaying: 'y',
side: 'right',
titlefont: { color: params.color2 },
tickfont: { color: params.color2 },
},
hovermode: "x",
}, params.config);
}
/*
* Shows the distribution of duration, distance or average speed of a trip
*/
function plotDistribSpeed() {
let div = createPlot("plotDistribSpeed");
let frames = [
{ name: "speed", binsize: 1, data: [{ hovertemplate: "Trajets à %{text: } km/h : %{y: }<extra></extra>" }] },
{ name: "durMinutes", binsize: 1, data: [{ hovertemplate: "Trajets d'environ %{text: } min : %{y: }<extra></extra>", }] },
{ name: "distance", binsize: .5, data: [{ hovertemplate: "Trajets d'environ %{text: } km : %{y: }<extra></extra>" }] }
];
for (let i = 0; i < frames.length; i++) {
let max = 0;
for (t of allTrips) {
if (t[frames[i].name] > max) max = t[frames[i].name];
}
frames[i].data[0].y = Array(parseInt(max / frames[i].binsize) + 1).fill(0);
for (t of allTrips) {
frames[i].data[0].y[parseInt(t[frames[i].name] / frames[i].binsize)]++;
}
frames[i].data[0].x = [...frames[0].data[0].y.keys()];
frames[i].data[0].text = Array();
for (let x of frames[i].data[0].x)
frames[i].data[0].text.push(x * frames[i].binsize);
}
// let comment_id = parseInt(speeds.length / 2);
// div.find(".comment").html(
// `Lecture : vous avez fait ${plural(speeds[comment_id], "trajet")} ` +
// `à une vitesse moyenne comprise entre ${comment_id}h et ${comment_id+1}km/h.`);
Plotly.newPlot(div.find(".chart")[0], [{
name: "Trajets",
x: frames[0].data[0].text,
y: frames[0].data[0].y,
text: frames[0].data[0].text,
hovertemplate: frames[0].data[0].hovertemplate,
line: { color: params.color1, width: params.linewidth, },
showlegend: false
}], {
title: {
text: 'Distribution de vitesse, durée et distance',
font: { size: params.titlesize }
},
margin: { t: params.marginTop },
xaxis: {
title: 'Valeur', // 'Vitesse moyenne du trajet (km/h)',
zeroline: false
},
yaxis: {
title: 'Nombre de trajets',
rangemode: "tozero",
},
hovermode: "x",
updatemenus: [{
type: "buttons", // "dropdown"
buttons: [{
method: 'animate',
label: 'Vitesse',
args: [
['speed'], { 'frame': { 'duration': 0, 'redraw': true }, 'transition': { 'duration': 0 } }
],
},
{
method: 'animate',
label: 'Durée',
args: [
['durMinutes'], { 'frame': { 'duration': 0, 'redraw': true }, 'transition': { 'duration': 0 } }
],
},
{
method: 'animate',
label: 'Distance',
args: [
['distance'], { 'frame': { 'duration': 0, 'redraw': true }, 'transition': { 'duration': 0 } }
],
}
]
}]
}, params.config).then(function() {
Plotly.addFrames(div.find(".chart")[0], frames);
});
div.on("plotly_animated", function(e, t) { // find(".updatemenu-container").click( // plotly_buttonclicked
Plotly.relayout(div.find(".chart")[0], {
'xaxis.autorange': true,
'yaxis.autorange': true,
// find a way to update x-axis and title according to current frame
});
});
}
function velibrairiePlot() {
plotTripsPerWeekday();
plotTripsPerHour();
plotDistribSpeed();
}
//
//
/******************************************************/
/****************** Execute everything ****************/
/******************************************************/
// Optional: silence errors and warnings in console
console.error = function() {}
console.warn = function() {}
goToPageRuns();
window.setTimeout(loaderCreate, 800);
window.setTimeout(goToPage1, 1000);
// Load the plotting library (https://plotly.com)
$.getScript("https://cdn.plot.ly/plotly-2.9.0.min.js").done(function() {
window.setTimeout(getTripAll, 2000);
});