-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timetable.html
139 lines (120 loc) · 4.7 KB
/
Timetable.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
<html>
<head>
<title>Transport API</title>
</head>
<body>
<div id="authorize-div" style="display: true">
<span>Authorize access to Google Calendar API</span>
<!--Button for the user to click to initiate auth sequence -->
<button id="authorize-button" onclick="handleAuthClick(event)">
Authorize
</button>
</div>
<h1>Timetable</h1>
Start:
<input type="text" name="start" maxlength="50" value="Amsoldingen, Kreuz"><br>
<!--Ziel:
<input type="text" name="destination" maxlength="50" value="Biel, Höhenweg"><br>-->
<button onclick="doStuff()">Suche</button>
<div name="results"></div>
<script src="https://apis.google.com/js/client.js?onload=checkAuth"></script>
<script>
var CLIENT_ID = '843749328717-5rj5c0iuvaddlricoshtghnoq1290d1k.apps.googleusercontent.com';
var SCOPES = ["https://www.googleapis.com/auth/calendar"];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
gapi.client.load('calendar', 'v3');
}
/**
* Initiate auth flow in response to user clicking authorize button.
*
* @param {Event} event Button click event.
*/
function handleAuthClick(event) {
gapi.auth.authorize(
{client_id: CLIENT_ID, scope: SCOPES, immediate: false},
handleAuthResult);
return false;
}
function upcomingEvent(cb) {
gapi.client.calendar.events.list({
'calendarId': 'primary',
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 10,
'orderBy': 'startTime'
}).then(function(response) {
var events = response.result.items;
if (events.length > 0) {
cb(events[0]);
} else {
cb(false);
}
});
}
function getConnection (start, destination, datetime, callback) {
var request = new XMLHttpRequest();
request.open('GET', 'http://transport.opendata.ch/v1/connections?from='+start+'&to='+destination+'&time='+datetime.getHours()+':'+datetime.getMinutes()+'&isArrivalTime=1', true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
// Success!
var data = JSON.parse(this.response);
callback(data.connections[1]);
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
}
function doStuff() {
upcomingEvent(calcTime);
}
function calcTime(event) {
var start = document.getElementsByName("start")[0].value;
var destination = event.location;
var zeitpunkt = new Date (event.start.dateTime)
var datetime = new Date (zeitpunkt);
datetime.setMinutes ( zeitpunkt.getMinutes () - 10 );
function processResult(value) {
var departure = new Date (value.from.departure);
var alertTime = new Date (departure);
alertTime.setMinutes (departure.getMinutes() - 60);
console.log("AlertTime", alertTime);
var arrival = new Date (value.to.arrival);
var res = document.getElementsByName('results')[0];
var add0 = function(t){return t < 10 ? '0' + t : t}
res.insertAdjacentHTML('beforeend', '<h1>'+add0(alertTime.getHours())+':'+add0(alertTime.getMinutes())+' Weckzeit</h1><br><h1>'+add0(departure.getHours())+':'+add0(departure.getMinutes())+' Abfahrtszeit ('+value.from.location.name+')</h1><br><h1>'+add0(arrival.getHours())+':'+add0(arrival.getMinutes())+' Ankunftszeit ('+value.to.location.name+')</h1><br><h1>'+add0(zeitpunkt.getHours())+':'+add0(zeitpunkt.getMinutes())+' '+event.summary)
}
getConnection(start, destination, datetime, processResult)
}
</script>
</body>
</html>