/
mtaapi.go
90 lines (77 loc) · 1.8 KB
/
mtaapi.go
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
package main
import (
"bytes"
"encoding/json"
"github.com/codegangsta/martini"
"github.com/martini-contrib/cors"
"io/ioutil"
"net/http"
"strings"
)
type MTAResponse struct {
BT struct {
Line []struct {
Name string `json:"name"`
Status string `json:"status"`
} `json:"line"`
} `json:"BT"`
LIRR struct {
Line []struct {
Name string `json:"name"`
Status string `json:"status"`
} `json:"line"`
} `json:"LIRR"`
MetroNorth struct {
Line []struct {
Name string `json:"name"`
Status string `json:"status"`
} `json:"line"`
} `json:"MetroNorth"`
Bus struct {
Line []struct {
Name string `json:"name"`
Status string `json:"status"`
} `json:"line"`
} `json:"bus"`
Responsecode string `json:"responsecode"`
Subway struct {
Line []struct {
Name string `json:"name"`
Status string `json:"status"`
} `json:"line"`
} `json:"subway"`
}
func main() {
server := martini.Classic()
server.Use(cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
}))
server.Get("/mta-api/subway", func() string {
var buffer bytes.Buffer
var r MTAResponse
var bytes []byte
resp, err := http.Get("http://www.mta.info/service_status_json/23458509")
if err != nil {
buffer.WriteString(err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
buffer.WriteString(err.Error())
}
stringed := string(body[:])
stringed = strings.Replace(stringed, "\\u0022", "\"", -1)
stringed = stringed[3 : len(stringed)-1]
bytes = []byte(stringed)
e := json.Unmarshal(bytes, &r)
if e != nil {
buffer.WriteString(e.Error())
}
b, err := json.Marshal(r.Subway)
return string(b[:])
})
server.Run()
}