Skip to content

Instantly share code, notes, and snippets.

@nf
Created April 5, 2012 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nf/2307179 to your computer and use it in GitHub Desktop.
Save nf/2307179 to your computer and use it in GitHub Desktop.
whereis app engine app
<html>
<head>
<title>Where's adg?</title>
<style>
th {
text-align: left;
}
td, th {
padding-right: 10px;
}
</style>
</head>
<body>
{{with .Last}}
<h3>Currently at:</h3>
<h1>{{city .To}}</h1>
<p>Since <span class="time">{{.End.Unix}}</span></p>
{{end}}
{{with .Current}}
<h3>Currenly in transit:</h3>
<h2>
{{city .From}} to {{city .To}}
({{.Number}} arr <span class="time">{{.End.Unix}})</span>)
</h2>
{{end}}
{{with .Future}}
<h3>Upcoming journeys:</h3>
<table>
<tr>
<th>From</th>
<th>To</th>
<th>Flight #</th>
<th>Departure Time</th>
<th>Arrival Time</th>
</th>
{{range .}}
<tr>
<td class="from" >{{city .From}}</td>
<td class="to" >{{city .To}}</td>
<td class="flight">{{.Number}}</td>
<td class="dep" ><span class="time">{{.Start.Unix}}</span></td>
<td class="arr" ><span class="time">{{.End.Unix}}</span></td>
</tr>
{{end}}
</table>
{{end}}
<script>
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var times = document.getElementsByClassName("time");
for (var i = 0; i < times.length; i++) {
var t = times[i].innerHTML * 1;
var d = new Date(t*1000);
var ts = d.toLocaleTimeString().split(":").slice(0, 2).join(":");
var dt = d.getDate() + " " + months[d.getMonth()];
times[i].innerHTML = ts + " " + dt;
}
</script>
</body>
</html>
package main
import (
"appengine"
"appengine/memcache"
"appengine/urlfetch"
"net/http"
"regexp"
"text/template"
"time"
"github.com/nf/ics"
)
const calendarURL = "YOUR_TRIPIT_CALENDAR_URL"
const cacheExpiry = time.Hour
var airportCodes = map[string]string{
"AKL": "Auckland",
"BALLINA": "Federal (Ballina)",
"BNK": "Federal (Ballina)",
"CBR": "Canberra",
"JFK": "New York City",
"MEL": "Melbourne",
"OOL": "Federal (Gold Coast)",
"SFO": "San Francisco",
"SYD": "Sydney",
}
func init() {
http.HandleFunc("/", where)
whereTmpl.Funcs(template.FuncMap{
"city": func(code string) string {
if city, ok := airportCodes[code]; ok {
return city
}
return code
},
})
template.Must(whereTmpl.ParseFiles("where/index.tmpl"))
}
var whereTmpl = template.New("index.tmpl")
func where(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
var cal *ics.Calendar
if _, err := memcache.JSON.Get(c, calendarURL, cal); err != nil {
resp, err := urlfetch.Client(c).Get(calendarURL)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer resp.Body.Close()
cal, err = ics.Decode(resp.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
memcache.JSON.Set(c, &memcache.Item{
Key: calendarURL,
Object: cal,
Expiration: cacheExpiry,
})
}
var data struct {
Last, Current *Flight
Future []*Flight
}
f := flights(cal)
now := time.Now()
for len(f) > 0 {
if f[0].End.After(now) {
if f[0].Start.Before(now) {
data.Current = f[0]
data.Last = nil
}
break
}
data.Last = f[0]
f = f[1:]
}
data.Future = f
w.Header().Set("Content-type", "text/html")
err := whereTmpl.Execute(w, data)
if err != nil {
c.Errorf("rendering template: %v", err)
}
}
var flightRe = regexp.MustCompile(`([A-Z0-9]+) ([A-Z]+) to ([A-Z]+)`)
type Flight struct {
Number, From, To string
Start, End time.Time
}
func flights(c *ics.Calendar) (f []*Flight) {
for _, e := range c.Event {
m := flightRe.FindStringSubmatch(e.Summary)
if len(m) == 0 {
continue
}
f = append(f, &Flight{
Number: m[1],
From: m[2],
To: m[3],
Start: e.Start,
End: e.End,
})
}
return f
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment