Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jp transit gtfs realtime data feed #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module github.com/codefornola/nola-transit-map

go 1.14
go 1.21

toolchain go1.22.6

require (
github.com/MobilityData/gtfs-realtime-bindings/golang/gtfs v1.0.0
github.com/gorilla/websocket v1.5.0
github.com/stretchr/testify v1.8.1 // indirect
google.golang.org/protobuf v1.35.1
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
github.com/MobilityData/gtfs-realtime-bindings/golang/gtfs v1.0.0 h1:f4P+fVYmSIWj4b/jvbMdmrmsx/Xb+5xCpYYtVXOdKoc=
github.com/MobilityData/gtfs-realtime-bindings/golang/gtfs v1.0.0/go.mod h1:nSmbVVQSM4lp9gYvVaaTotnRxSwZXEdFnJARofg5V4g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -12,6 +16,11 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
63 changes: 61 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"net/http"
"os"
"time"
"strconv"

"github.com/gorilla/websocket"
"google.golang.org/protobuf/proto"
"github.com/MobilityData/gtfs-realtime-bindings/golang/gtfs"
)

const (
Expand Down Expand Up @@ -132,6 +135,7 @@ type Config struct {

type Scraper struct {
client *http.Client
client_jp *http.Client
config *Config
}

Expand All @@ -155,17 +159,24 @@ func NewScraper() *Scraper {
TLSHandshakeTimeout: 0 * time.Second,
}
client := &http.Client{Transport: tr}
client_jp := &http.Client{Transport: tr}
return &Scraper{
client,
client_jp,
config,
}
}

func (s *Scraper) Start(vs chan []Vehicle) {
for {
result := s.fetch()
log.Printf("Found %d vehicles\n", len(result.Vehicles))
vs <- result.Vehicles
log.Printf("Found %d RTA vehicles\n", len(result.Vehicles))

result_jp := s.fetch_jp()
log.Printf("Found %d JP vehicles\n", len(result_jp.Vehicles))

vs <- append(result.Vehicles, result_jp.Vehicles...)

time.Sleep(s.config.Interval)
}
}
Expand All @@ -192,8 +203,56 @@ func (v *Scraper) fetch() *BustimeData {
return &result.Data
}

func (v *Scraper) fetch_jp() *BustimeData {
url := "https://jetapp.jptransit.org/gtfsrt/vehicles"
resp, err := v.client_jp.Get(url)
if err != nil {
log.Printf("error fetching jptransit data: %s [firewall blocking VPN address?]", err)
result := &BustimeResponse{}
return &result.Data
}
if resp.Body != nil {
defer resp.Body.Close()
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

feed := gtfs.FeedMessage{}
err = proto.Unmarshal(body, &feed)
if err != nil {
log.Fatal(err)
}

result := &BustimeData{}
result.Vehicles = []Vehicle{}

for _, entity := range feed.Entity {
vehicle := entity.GetVehicle()
position := vehicle.GetPosition()
route := vehicle.GetTrip().GetRouteId()
if (len(route) == 0) {
route = "U"
}

v := Vehicle{}
v.Vid = vehicle.GetVehicle().GetId()
v.Tmstmp = VehicleTimestamp{}
v.Tmstmp.Time = time.Unix(int64(vehicle.GetTimestamp()), 0)
v.Lat = float64(position.GetLatitude())
v.Lon = float64(position.GetLongitude())
v.Hdg = strconv.FormatFloat(float64(position.GetBearing()), 'f', -1, 64)
v.Rt = route
result.Vehicles = append(result.Vehicles, v)
}

return result
}

func (v *Scraper) Close() {
v.client.CloseIdleConnections()
v.client_jp.CloseIdleConnections()
}

type VehicleChannel = chan []Vehicle
Expand Down