Compare commits

...

3 Commits

4 changed files with 68 additions and 14 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
config.toml config.toml
.DS_Store

55
bus.go
View File

@ -6,25 +6,42 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"time" "time"
) )
type Departure struct { type Departure struct {
TripID int TripID int
RealtimeTripId string RealtimeTripId string
StopHeadsign string StopHeadsign string
ArrivalTime time.Time ArrivalTime time.Time
DepartureTime time.Time DepartureTime time.Time
RouteShortName string RealtimeArrivalTime time.Time
RealtimeDepartureTime time.Time
RouteShortName string
}
func (d Departure) HasRealtimeDepartureTime() bool {
return !d.RealtimeDepartureTime.IsZero()
}
func (d Departure) IsDelayed() bool {
if !d.HasRealtimeDepartureTime() {
return false
}
return d.RealtimeDepartureTime.After(d.DepartureTime)
} }
type ResponseDeparture struct { type ResponseDeparture struct {
TripID int `json:"trip_id"` TripID int `json:"trip_id"`
RealtimeTripId string `json:"realtime_trip_id"` RealtimeTripId string `json:"realtime_trip_id"`
StopHeadsign string `json:"stop_headsign"` StopHeadsign string `json:"stop_headsign"`
ArrivalTime string `json:"arrival_time"` ArrivalTime string `json:"arrival_time"`
DepartureTime string `json:"departure_time"` DepartureTime string `json:"departure_time"`
Tripdata struct { RealtimeArrivalTime int `json:"realtime_arrival_time"`
RealtimeDepartureTime int `json:"realtime_departure_time"`
Tripdata struct {
Route struct { Route struct {
RouteShortName string `json:"route_short_name"` RouteShortName string `json:"route_short_name"`
} `json:"route"` } `json:"route"`
@ -52,6 +69,20 @@ func (rd ResponseDeparture) ToDeparture() (Departure, error) {
} }
departure.DepartureTime = _time departure.DepartureTime = _time
if rd.RealtimeArrivalTime != 0 {
_time, err = parseUnixTimestamp(strconv.Itoa(rd.RealtimeArrivalTime))
if err == nil {
departure.RealtimeArrivalTime = _time
}
}
if rd.RealtimeDepartureTime != 0 {
_time, err = parseUnixTimestamp(strconv.Itoa(rd.RealtimeDepartureTime))
if err == nil {
departure.RealtimeDepartureTime = _time
}
}
return departure, nil return departure, nil
} }

View File

@ -30,7 +30,11 @@
<tr> <tr>
<td>{{ $val.RouteShortName }}</td> <td>{{ $val.RouteShortName }}</td>
<td>{{ $val.StopHeadsign }}</td> <td>{{ $val.StopHeadsign }}</td>
{{ if $val.HasRealtimeDepartureTime }}
<td><time class="{{ if $val.IsDelayed }}delayed{{ else }}on-time{{ end }}">{{ formatTime $val.RealtimeDepartureTime }}</time></td>
{{ else }}
<td><time>{{ formatTime $val.DepartureTime }}</time></td> <td><time>{{ formatTime $val.DepartureTime }}</time></td>
{{ end }}
</tr> </tr>
{{ end }} {{ end }}
{{ end }} {{ end }}
@ -40,4 +44,4 @@
{{ end }} {{ end }}
</main> </main>
</body> </body>
</html> </html>

View File

@ -43,4 +43,22 @@ td:first-child {
td:last-child { td:last-child {
padding-right: 0; padding-right: 0;
} }
time {
background: black;
color: white;
display: inline-block;
font-size: .8rem;
padding: .2em .2em .1em;
}
.on-time {
background: green;
color: white;
}
.delayed {
background: red;
color: white;
}