-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrips.html
369 lines (343 loc) · 13.4 KB
/
trips.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Travel Data</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<style>
.container {
margin: 20px;
padding: 20px;
}
.columns {
margin-top: 20px;
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.column {
padding: 20px;
flex: 1;
min-width: 300px;
}
h2 {
margin-bottom: 10px;
}
.notification {
margin: 20px;
}
table {
width: 100%;
margin: 20px 0;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
.form-container {
margin-bottom: 20px;
}
.form-container .field {
display: inline-block;
margin-right: 10px;
}
.form-container .field .label {
margin-bottom: 5px;
}
.form-container .control {
margin-right: 10px;
}
.flag {
margin-left: 5px;
}
.highlight-blue {
background-color: #ccf2ff;
}
.highlight-green {
background-color: #ccffcc;
}
.highlight-yellow {
background-color: #ffffcc;
}
.highlight-red {
background-color: #ffcccc;
}
.highlight-orange {
background-color: #fff2cc;
}
.legend {
display: flex;
justify-content: center;
flex-wrap: wrap;
margin-top: 20px;
}
.legend-item {
display: flex;
align-items: center;
margin: 5px;
padding: 5px 10px;
border-radius: 4px;
font-size: 0.9em;
}
.legend-blue {
background-color: #ccf2ff;
}
.legend-green {
background-color: #ccffcc;
}
.legend-yellow {
background-color: #ffffcc;
}
.legend-orange {
background-color: #fff2cc;
}
.legend-red {
background-color: #ffcccc;
}
</style>
</head>
<body>
<div class="container has-text-centered">
<h1 id="main-title" class="title">Travel Data</h1>
<div class="form-container">
<div class="field">
<label class="label" for="login">Nomadlist Login*</label>
<div class="control">
<input id="login" class="input" type="text" placeholder="Your login" required>
</div>
</div>
<div class="field">
<label class="label" for="home">Country to Exclude from Stats</label>
<div class="control">
<input id="home" class="input" type="text" placeholder="E.g. your home country">
</div>
</div>
<div class="field">
<label class="label" for="length">Minimum Length of Stay to Consider</label>
<div class="control">
<input id="length" class="input" type="number" placeholder="In days, default 10" min="1">
</div>
</div>
<div class="control">
<button id="fetch-data" class="button is-primary">Fetch Data</button>
</div>
</div>
<div id="content" class="columns">
<div id="country-list" class="column"></div>
<div id="city-list" class="column"></div>
</div>
<div class="legend">
<div class="legend-item legend-red">> 1 year</div>
<div class="legend-item legend-orange">> 6 months</div>
<div class="legend-item legend-yellow">> 3 months</div>
<div class="legend-item legend-green">> 1 month</div>
<div class="legend-item legend-blue">> 1 week</div>
</div>
</div>
<script>
async function fetchTravelData(url) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error('Network response was not ok');
const data = await response.json();
return data.trips;
} catch (error) {
return null;
}
}
function calculateStayDuration(dateStart, dateEnd) {
const startDate = new Date(dateStart);
const endDate = new Date(dateEnd);
const timeDiff = endDate - startDate;
const daysDiff = timeDiff / (1000 * 3600 * 24);
return Math.ceil(daysDiff); // Ensure that 1-day trips are counted as 1 day
}
function formatDuration(days) {
const weeks = (days / 7).toFixed(1);
const months = (days / 30.4375).toFixed(1);
const years = (days / 365.25).toFixed(1);
let result = `${days} days`;
if (days >= 365) {
result += ` (${years} years)`;
} else if (days >= 30) {
result += ` (${months} months)`;
} else if (days >= 7) {
result += ` (${weeks} weeks)`;
}
return result;
}
function getHighlightClass(days) {
if (days > 365) {
return 'highlight-red';
} else if (days >= 180) {
return 'highlight-orange';
} else if (days >= 90) {
return 'highlight-yellow';
} else if (days >= 30) {
return 'highlight-green';
} else if (days >= 7) {
return 'highlight-blue';
} else {
return '';
}
}
function processTravelData(trips, homeCountry, minLength) {
const countryStay = {};
const cityStay = {};
const countryCities = {};
trips.forEach(trip => {
const duration = calculateStayDuration(trip.date_start, trip.date_end) + 1; // Add 1 to include end day
const country = trip.country.trim();
const city = trip.place.trim();
const countryCode = trip.country_code ? trip.country_code.trim().toUpperCase() : '';
// Accumulate country stay regardless of minLength
if (!countryStay[country]) {
countryStay[country] = 0;
countryCities[country] = new Set();
}
countryStay[country] += duration;
countryCities[country].add(city);
// Accumulate city stay only if it meets the minLength criteria
if (duration >= minLength && (!homeCountry || (country.toLowerCase() !== homeCountry.toLowerCase() && countryCode !== homeCountry.toLowerCase()))) {
if (!cityStay[city]) {
cityStay[city] = 0;
}
cityStay[city] += duration;
}
});
// Filter out home country from country stay
if (homeCountry) {
Object.keys(countryStay).forEach(country => {
if (country.toLowerCase() === homeCountry.toLowerCase()) {
delete countryStay[country];
delete countryCities[country];
}
});
}
// Filter countries that don't meet the minLength criteria
Object.keys(countryStay).forEach(country => {
if (countryStay[country] < minLength) {
delete countryStay[country];
}
});
const sortedCountries = Object.entries(countryStay)
.sort((a, b) => b[1] - a[1])
.map(([country, duration]) => {
let countryCode = trips.find(trip => trip.country.trim() === country).country_code.trim().toUpperCase();
if (countryCode === 'UK') {
countryCode = 'GB';
}
return { country, duration, cities: countryCities[country].size, countryCode };
});
const sortedCities = Object.entries(cityStay)
.sort((a, b) => b[1] - a[1])
.map(([city, duration]) => {
let countryCode = trips.find(trip => trip.place.trim() === city).country_code.trim().toUpperCase();
if (countryCode === 'UK') {
countryCode = 'GB';
}
return { city, duration, countryCode };
});
return { sortedCountries, sortedCities };
}
function getFlagImageUrl(countryCode) {
return `https://flagsapi.com/${countryCode}/shiny/16.png`;
}
function displayData(sortedCountries, sortedCities) {
const countryList = document.getElementById('country-list');
const cityList = document.getElementById('city-list');
const countryTable = `
<table class="centered">
<tr><th>Country</th><th>Cities</th><th>Period of Stay</th></tr>
${sortedCountries.map(item => `
<tr class="${getHighlightClass(item.duration)}">
<td>${item.country} <img src="${getFlagImageUrl(item.countryCode)}" alt="Flag" class="flag"></td>
<td>${item.cities}</td>
<td>${formatDuration(item.duration)}</td>
</tr>`).join('')}
</table>`;
const cityTable = `
<table class="centered">
<tr><th>City</th><th>Period of Stay</th></tr>
${sortedCities.map(item => `
<tr class="${getHighlightClass(item.duration)}">
<td>${item.city} <img src="${getFlagImageUrl(item.countryCode)}" alt="Flag" class="flag"></td>
<td>${formatDuration(item.duration)}</td>
</tr>`).join('')}
</table>`;
countryList.innerHTML = countryTable;
cityList.innerHTML = cityTable;
}
async function main() {
const urlParams = new URLSearchParams(window.location.search);
const forParam = urlParams.get('for');
const homeParam = urlParams.get('home');
const lengthParam = parseInt(urlParams.get('length'), 10) || 10;
if (forParam) {
const url = `https://nomadlist.com/@${forParam}.json`;
const trips = await fetchTravelData(url);
if (trips) {
document.getElementById('main-title').innerText = `Travel Data for ${forParam}`;
const { sortedCountries, sortedCities } = processTravelData(trips, homeParam, lengthParam);
displayData(sortedCountries, sortedCities);
} else {
document.getElementById('content').innerHTML = '<p class="notification is-danger">Please enter a valid username from the nomadlist website.</p>';
}
} else {
document.getElementById('content').innerHTML = '';
}
}
document.getElementById('fetch-data').addEventListener('click', () => {
const login = document.getElementById('login').value.trim();
const home = document.getElementById('home').value.trim();
const length = document.getElementById('length').value.trim();
if (login) {
const url = new URL(window.location.href);
url.searchParams.set('for', login);
if (home) {
url.searchParams.set('home', home);
} else {
url.searchParams.delete('home');
}
if (length) {
url.searchParams.set('length', length);
} else {
url.searchParams.delete('length');
}
window.location.href = url.toString();
}
});
window.addEventListener('load', () => {
const urlParams = new URLSearchParams(window.location.search);
const forParam = urlParams.get('for');
const homeParam = urlParams.get('home');
const lengthParam = urlParams.get('length');
if (forParam) {
document.getElementById('login').value = forParam;
}
if (homeParam) {
document.getElementById('home').value = homeParam;
}
if (lengthParam) {
document.getElementById('length').value = lengthParam;
}
// Добавление обработчика события keydown
['login', 'home', 'length'].forEach(id => {
document.getElementById(id).addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
document.getElementById('fetch-data').click();
}
});
});
});
main();
</script>
</body>
</html>