This commit is contained in:
parent
8c10770023
commit
3e39c5e5c0
@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const IDFM2_BASEURL = "https://prim.iledefrance-mobilites.fr/marketplace"
|
const IDFM_BASEURL = "https://prim.iledefrance-mobilites.fr/marketplace"
|
||||||
|
|
||||||
var IDFM_TOKEN = ""
|
var IDFM_TOKEN = ""
|
||||||
|
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -63,85 +60,13 @@ type PGDestination struct {
|
|||||||
Way string `json:"way"`
|
Way string `json:"way"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSchedules(code string) (*IDFMSchedule, error) {
|
|
||||||
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "schedules")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
requrl, err := url.Parse(rurl)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
reqquery := url.Values{}
|
|
||||||
reqquery.Add("complete", "false")
|
|
||||||
requrl.RawQuery = reqquery.Encode()
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", requrl.String(), nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Add("Accept", "application/json")
|
|
||||||
req.Header.Add("apikey", IDFM_TOKEN)
|
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
|
|
||||||
if res.StatusCode >= 400 {
|
|
||||||
return nil, fmt.Errorf("Schedule not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
var schedules IDFMSchedule
|
|
||||||
|
|
||||||
dec := json.NewDecoder(res.Body)
|
|
||||||
if err = dec.Decode(&schedules); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &schedules, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDestinations(code string) ([]PGDestination, error) {
|
func getDestinations(code string) ([]PGDestination, error) {
|
||||||
schedule, err := getSchedules(code)
|
return nil, nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var pgd []PGDestination
|
|
||||||
|
|
||||||
destination:
|
|
||||||
for i, s := range schedule.Schedules {
|
|
||||||
for _, d := range pgd {
|
|
||||||
if d.Name == s.To {
|
|
||||||
continue destination
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
way := "R"
|
|
||||||
if i%2 == 0 {
|
|
||||||
way = "A"
|
|
||||||
}
|
|
||||||
|
|
||||||
pgd = append(pgd, PGDestination{
|
|
||||||
Name: s.To,
|
|
||||||
Way: way,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return pgd, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func declareDestinationsRoutes(router *gin.RouterGroup) {
|
func declareDestinationsRoutes(router *gin.RouterGroup) {
|
||||||
router.GET("/destinations/:type/:code", func(c *gin.Context) {
|
router.GET("/destinations/:type/:code", func(c *gin.Context) {
|
||||||
t := convertLineType(string(c.Param("type")))
|
pgd, err := getDestinations(string(c.Param("code")))
|
||||||
code := convertCode(t, string(c.Param("code")))
|
|
||||||
|
|
||||||
pgd, err := getDestinations(code)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
|
31
api/lines.go
31
api/lines.go
@ -97,31 +97,20 @@ func convertLineType(old string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertCode(t, code string) string {
|
|
||||||
if !strings.HasPrefix(code, "line:IDFM:") {
|
|
||||||
if t == "tram" && !strings.HasPrefix(code, "T") {
|
|
||||||
code = "T" + code
|
|
||||||
}
|
|
||||||
if t == "noctilien" && !strings.HasPrefix(code, "N") {
|
|
||||||
code = "N" + code
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(code) != 6 || !strings.HasPrefix(code, "C") {
|
|
||||||
code = searchLine(t, code)
|
|
||||||
}
|
|
||||||
|
|
||||||
code = "line:IDFM:" + code
|
|
||||||
}
|
|
||||||
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func searchLine(t, code string) string {
|
func searchLine(t, code string) string {
|
||||||
|
if t == "rer" {
|
||||||
|
for _, line := range IDFMLines {
|
||||||
|
if strings.HasSuffix(line.Fields.ShortName, code) && (line.Fields.TransportMode == t || strings.ToLower(line.Fields.NetworkName) == t) {
|
||||||
|
return line.Fields.IdLine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for _, line := range IDFMLines {
|
for _, line := range IDFMLines {
|
||||||
if line.Fields.ShortName == code && (line.Fields.TransportMode == t || strings.ToLower(line.Fields.NetworkName) == t) {
|
if line.Fields.ShortName == code && (line.Fields.TransportMode == t || strings.ToLower(line.Fields.NetworkName) == t) {
|
||||||
return line.Fields.IdLine
|
return line.Fields.IdLine
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
@ -165,10 +154,10 @@ func declareLinesRoutes(router *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pgline := PGLine{
|
pgline := PGLine{
|
||||||
Code: line.Fields.ShortName,
|
Code: fmt.Sprintf("STIF:Line::%s:", line.Fields.IdLine),
|
||||||
Name: name,
|
Name: name,
|
||||||
Directions: "",
|
Directions: "",
|
||||||
Id: fmt.Sprintf("STIF:Line::%s:", line.Fields.IdLine),
|
Id: line.Fields.IdLine,
|
||||||
}
|
}
|
||||||
|
|
||||||
lines = append(lines, pgline)
|
lines = append(lines, pgline)
|
||||||
|
204
api/schedules.go
204
api/schedules.go
@ -5,9 +5,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -15,80 +15,79 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const IDFM_BASEURL = "https://api-iv.iledefrance-mobilites.fr/"
|
type IDFMMonitoredStopVisit struct {
|
||||||
|
RecordedAtTime time.Time `json:"RecordedAtTime"`
|
||||||
type IDFMRealTimeData struct {
|
ItemIdentifier string `json:"ItemIdentifier"`
|
||||||
LineId string `json:"lineId"`
|
MonitoringRef struct {
|
||||||
ShortName string `json:"shortName"`
|
Value string `json:"value"`
|
||||||
VehicleName string `json:"vehicleName,omitempty"`
|
} `json:"MonitoringRef"`
|
||||||
LineDirection string `json:"lineDirection"`
|
MonitoredVehicleJourney struct {
|
||||||
Sens string `json:"sens,omitempty"`
|
LineRef struct {
|
||||||
Code string `json:"code,omitempty"`
|
Value string `json:"value"`
|
||||||
Time string `json:"time"`
|
} `json:"LineRef"`
|
||||||
Schedule string `json:"schedule"`
|
OperatorRef struct {
|
||||||
Destination struct {
|
Value string `json:"value"`
|
||||||
StopPointId string `json:"stopPointId"`
|
} `json:"OperatorRef"`
|
||||||
StopAreaId string `json:"stopAreaId"`
|
FramedVehicleJourneyRef struct {
|
||||||
} `json:"destination,omitempty"`
|
DataFrameRef struct {
|
||||||
Source string `json:"source,omitempty"`
|
Value string `json:"value"`
|
||||||
|
} `json:"DataFrameRef"`
|
||||||
|
DatedVehicleJourneyRef string `json:"DatedVehicleJourneyRef"`
|
||||||
|
} `json:"FramedVehicleJourneyRef"`
|
||||||
|
DirectionName []struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"DirectionName"`
|
||||||
|
DestinationRef struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"DestinationRef"`
|
||||||
|
DestinationName []struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"DestinationName"`
|
||||||
|
JourneyNote []struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"JourneyNote"`
|
||||||
|
MonitoredCall struct {
|
||||||
|
StopPointName []struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"StopPointName"`
|
||||||
|
VehicleAtStop bool `json:"VehicleAtStop"`
|
||||||
|
DestinationDisplay []struct {
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"DestinationDisplay"`
|
||||||
|
ExpectedArrivalTime time.Time `json:"ExpectedArrivalTime"`
|
||||||
|
ExpectedDepartureTime time.Time `json:"ExpectedDepartureTime"`
|
||||||
|
DepartureStatus string `json:"DepartureStatus"`
|
||||||
|
} `json:"MonitoredCall"`
|
||||||
|
} `json:"MonitoredVehicleJourney"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IDFMRealTime struct {
|
type IDFMRealTime struct {
|
||||||
NextDepartures struct {
|
Siri struct {
|
||||||
StatusCode int `json:"statusCode"`
|
ServiceDelivery struct {
|
||||||
ErrorMessage string `json:"errorMessage"`
|
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
||||||
Data []IDFMRealTimeData `json:"data"`
|
ProducerRef string `json:"ProducerRef"`
|
||||||
} `json:"nextDepartures"`
|
ResponseMessageIdentifier string `json:"ResponseMessageIdentifier"`
|
||||||
CrowdsourcingReports struct {
|
StopMonitoringDelivery []struct {
|
||||||
congestions []struct {
|
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
||||||
DirectionId string `json:"directionId"`
|
Version string `json:"Version"`
|
||||||
NearTimeReports struct {
|
Status string `json:"Status"`
|
||||||
Rating *string `json:"rating"`
|
MonitoredStopVisit []IDFMMonitoredStopVisit `json:"MonitoredStopVisit"`
|
||||||
} `json:"nearTimeReports"`
|
} `json:"StopMonitoringDelivery"`
|
||||||
} `json:"congestions"`
|
} `json:"ServiceDelivery"`
|
||||||
} `json:"crowdsourcingReports"`
|
} `json:"siri"`
|
||||||
}
|
|
||||||
|
|
||||||
type ByRealTime []IDFMRealTimeData
|
|
||||||
|
|
||||||
func (s ByRealTime) Len() int {
|
|
||||||
return len(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s ByRealTime) Less(i, j int) bool {
|
|
||||||
if s[i].Sens == s[j].Sens {
|
|
||||||
nj, err := strconv.Atoi(s[j].Time)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
ni, err := strconv.Atoi(s[i].Time)
|
|
||||||
if err != nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return ni < nj
|
|
||||||
} else {
|
|
||||||
return s[i].Sens < s[j].Sens
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s ByRealTime) Swap(i, j int) {
|
|
||||||
s[i], s[j] = s[j], s[i]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PGSchedule struct {
|
type PGSchedule struct {
|
||||||
Destination string `json:"destination"`
|
Destination string `json:"destination"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Code string `json:"code,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertLineCode(code string) string {
|
func convertLineCode(code string) string {
|
||||||
return strings.TrimSuffix(strings.Replace(code, "STIF:Line::", "IDFM:", 1), ":")
|
return strings.TrimSuffix(code, ":")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRealTime(code, station string) (*IDFMRealTime, error) {
|
func getRealTime(code string, stations []string) ([]IDFMMonitoredStopVisit, error) {
|
||||||
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "stops", station, "realTime")
|
rurl, err := url.JoinPath(IDFM_BASEURL, "stop-monitoring")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -98,13 +97,16 @@ func getRealTime(code, station string) (*IDFMRealTime, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var stops []IDFMMonitoredStopVisit
|
||||||
|
for _, station := range stations {
|
||||||
reqquery := url.Values{}
|
reqquery := url.Values{}
|
||||||
reqquery.Add("MonitoringRef", station)
|
reqquery.Add("MonitoringRef", station)
|
||||||
|
reqquery.Add("LineRef", "STIF:Line::"+code+":")
|
||||||
requrl.RawQuery = reqquery.Encode()
|
requrl.RawQuery = reqquery.Encode()
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", requrl.String(), nil)
|
req, err := http.NewRequest("GET", requrl.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return stops, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("Accept", "application/json")
|
req.Header.Add("Accept", "application/json")
|
||||||
@ -112,7 +114,7 @@ func getRealTime(code, station string) (*IDFMRealTime, error) {
|
|||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return stops, err
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
@ -123,76 +125,80 @@ func getRealTime(code, station string) (*IDFMRealTime, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var schedules IDFMRealTime
|
var schedules IDFMRealTime
|
||||||
|
|
||||||
dec := json.NewDecoder(res.Body)
|
dec := json.NewDecoder(res.Body)
|
||||||
if err = dec.Decode(&schedules); err != nil {
|
if err = dec.Decode(&schedules); err != nil {
|
||||||
return nil, err
|
return stops, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if schedules.NextDepartures.StatusCode >= 400 {
|
for _, smd := range schedules.Siri.ServiceDelivery.StopMonitoringDelivery {
|
||||||
log.Println("Schedule not found: ", schedules)
|
stops = append(stops, smd.MonitoredStopVisit...)
|
||||||
return nil, fmt.Errorf("Schedule not found: %s", schedules.NextDepartures.ErrorMessage)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &schedules, nil
|
return stops, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func declareSchedulesRoutes(router *gin.RouterGroup) {
|
func declareSchedulesRoutes(router *gin.RouterGroup) {
|
||||||
router.GET("/schedules/:type/:code/:station/:way", func(c *gin.Context) {
|
router.GET("/schedules/:type/:code/:station/:way", func(c *gin.Context) {
|
||||||
t := convertLineType(string(c.Param("type")))
|
t := convertLineType(string(c.Param("type")))
|
||||||
code := convertCode(t, string(c.Param("code")))
|
code := searchLine(t, string(c.Param("code")))
|
||||||
station := string(c.Param("station"))
|
station := string(c.Param("station"))
|
||||||
way := string(c.Param("way"))
|
way := string(c.Param("way"))
|
||||||
|
|
||||||
if !strings.HasPrefix(code, "line:IDFM:") {
|
var stations []string
|
||||||
if len(code) != 6 || !strings.HasPrefix(code, "C") {
|
if !strings.HasPrefix(station, "STIF:Stop") {
|
||||||
code = searchLine(t, code)
|
|
||||||
}
|
|
||||||
|
|
||||||
code = "line:IDFM:" + code
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.HasPrefix(station, "stop_area:IDFM:") {
|
|
||||||
if _, err := strconv.ParseInt(station, 10, 64); err != nil {
|
if _, err := strconv.ParseInt(station, 10, 64); err != nil {
|
||||||
station, err = searchStation(code, station)
|
stations, err = searchStation(code, station)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
station = "stop_area:IDFM:" + station
|
stations = []string{"STIF:StopArea:IDFM:SP:" + station + ":"}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stations = []string{station}
|
||||||
|
}
|
||||||
|
|
||||||
|
if way != "A+R" && len(stations) == 2 {
|
||||||
|
if way == "A" {
|
||||||
|
stations = []string{stations[0]}
|
||||||
|
} else {
|
||||||
|
stations = []string{stations[1]}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("search", code, station)
|
schedules, err := getRealTime(code, stations)
|
||||||
schedules, err := getRealTime(code, station)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(ByRealTime(schedules.NextDepartures.Data))
|
|
||||||
|
|
||||||
pgs := []PGSchedule{}
|
pgs := []PGSchedule{}
|
||||||
for _, vehicule := range schedules.NextDepartures.Data {
|
for _, vehicule := range schedules {
|
||||||
if (way == "A" && vehicule.Sens == "1") || (way == "R" && vehicule.Sens == "-1") {
|
msg := vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime.String()
|
||||||
continue
|
|
||||||
|
if t == "metro" || t == "bus" || t == "noctiliens" || t == "tramway" {
|
||||||
|
if vehicule.MonitoredVehicleJourney.MonitoredCall.VehicleAtStop {
|
||||||
|
if t == "metro" {
|
||||||
|
msg = "Train à quai"
|
||||||
|
} else {
|
||||||
|
msg = "A l'arret"
|
||||||
}
|
}
|
||||||
|
} else if time.Until(vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime) < 0 {
|
||||||
msg := vehicule.Time + " mn"
|
if t == "metro" {
|
||||||
|
msg = "Train retardé"
|
||||||
if vehicule.Code == "message" {
|
} else {
|
||||||
msg = vehicule.Schedule
|
msg = "…"
|
||||||
} else if t == "rail" {
|
}
|
||||||
if n, err := strconv.Atoi(vehicule.Time); err == nil {
|
} else {
|
||||||
msg = time.Now().Add(time.Duration(n) * time.Minute).Format("15:04")
|
msg = fmt.Sprintf("%d mn", int(math.Floor(time.Until(vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime).Minutes())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pgs = append(pgs, PGSchedule{
|
pgs = append(pgs, PGSchedule{
|
||||||
Destination: vehicule.LineDirection,
|
Destination: vehicule.MonitoredVehicleJourney.MonitoredCall.DestinationDisplay[0].Value,
|
||||||
Message: msg,
|
Message: msg,
|
||||||
Code: vehicule.VehicleName,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
127
api/stations.go
127
api/stations.go
@ -2,10 +2,10 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
@ -16,14 +16,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type IDFMStation struct {
|
type IDFMStation struct {
|
||||||
Type string `json:"type"`
|
DatasetID string `json:"datasetid"`
|
||||||
|
RecordIDs string `json:"recordid"`
|
||||||
|
Fields struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
X float64 `json:"x"`
|
PointGeo []float64 `json:"pointgeo"`
|
||||||
Y float64 `json:"y"`
|
StopId string `json:"stop_id"`
|
||||||
Name string `json:"name"`
|
StopName string `json:"stop_name"`
|
||||||
ZipCode string `json:"zipCode"`
|
OperatorName string `json:"operatorname"`
|
||||||
City string `json:"city"`
|
NomCommune string `json:"nom_commune"`
|
||||||
Elevator bool `json:"elevator"`
|
RouteLongName string `json:"route_long_name"`
|
||||||
|
StopLat string `json:"stop_lat"`
|
||||||
|
StopLon string `json:"stop_lon"`
|
||||||
|
CodeINSEE string `json:"code_insee"`
|
||||||
|
} `json:"fields"`
|
||||||
|
Geometry struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Coordinates []float64 `json:"coordinates"`
|
||||||
|
} `json:"geometry"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IDFMStation) ComputeSlug() string {
|
func (s *IDFMStation) ComputeSlug() string {
|
||||||
@ -32,99 +42,70 @@ func (s *IDFMStation) ComputeSlug() string {
|
|||||||
}
|
}
|
||||||
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
|
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
|
||||||
|
|
||||||
r, _ := ioutil.ReadAll(transform.NewReader(strings.NewReader(s.Name), t))
|
r, _ := ioutil.ReadAll(transform.NewReader(strings.NewReader(s.Fields.StopName), t))
|
||||||
|
|
||||||
return strings.ToLower(strings.Replace(strings.Replace(strings.Replace(string(r), " ", "+", -1), "'", "+", -1), "-", "+", -1))
|
return strings.ToLower(strings.Replace(strings.Replace(strings.Replace(string(r), " ", "+", -1), "'", "+", -1), "-", "+", -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
type PGStation struct {
|
type PGStation struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"slug"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Slug string `json:"slug"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var IDFMStations []IDFMStation
|
var IDFMStations []IDFMStation
|
||||||
|
|
||||||
func searchStation(code, station string) (string, error) {
|
func init() {
|
||||||
stations, err := getStations(code)
|
fd, err := os.Open("arrets-lignes.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return station, err
|
log.Fatal("Unable to open `arrets-lignes.json`:", err.Error())
|
||||||
}
|
}
|
||||||
|
defer fd.Close()
|
||||||
|
|
||||||
code = strings.TrimPrefix(code, "line:IDFM:")
|
dec := json.NewDecoder(fd)
|
||||||
|
if err = dec.Decode(&IDFMStations); err != nil {
|
||||||
for _, st := range stations {
|
log.Fatal("Unable to decode `arrets-lignes.json`:", err.Error())
|
||||||
if st.ComputeSlug() == station {
|
|
||||||
return st.Id, nil
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return station, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStations(code string) (stations []IDFMStation, err error) {
|
func searchStation(code, station string) ([]string, error) {
|
||||||
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "stops")
|
code = "IDFM:" + strings.TrimPrefix(code, "line:IDFM:")
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
var res []string
|
||||||
|
for _, st := range IDFMStations {
|
||||||
|
if st.Fields.Id == code && st.ComputeSlug() == station {
|
||||||
|
if strings.HasPrefix(st.Fields.StopId, "IDFM:monomodalStopPlace:") {
|
||||||
|
res = append(res, "STIF:StopArea:SP:"+strings.TrimPrefix(strings.TrimPrefix(st.Fields.StopId, "IDFM:"), "monomodalStopPlace:")+":")
|
||||||
|
} else {
|
||||||
|
res = append(res, "STIF:StopPoint:Q:"+strings.TrimPrefix(st.Fields.StopId, "IDFM:")+":")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
requrl, err := url.Parse(rurl)
|
if len(res) == 0 {
|
||||||
if err != nil {
|
return []string{station}, nil
|
||||||
return nil, err
|
} else {
|
||||||
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
reqquery := url.Values{}
|
|
||||||
reqquery.Add("stopPoints", "false")
|
|
||||||
reqquery.Add("routes", "false")
|
|
||||||
requrl.RawQuery = reqquery.Encode()
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", requrl.String(), nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Add("Accept", "application/json")
|
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
|
|
||||||
if res.StatusCode >= 400 {
|
|
||||||
return nil, fmt.Errorf("Line not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
dec := json.NewDecoder(res.Body)
|
|
||||||
if err = dec.Decode(&stations); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func declareStationsRoutes(router *gin.RouterGroup) {
|
func declareStationsRoutes(router *gin.RouterGroup) {
|
||||||
router.GET("/stations/:type/:code", func(c *gin.Context) {
|
router.GET("/stations/:type/:code", func(c *gin.Context) {
|
||||||
t := convertLineType(string(c.Param("type")))
|
code := convertLineCode(string(c.Param("code")))
|
||||||
code := convertCode(t, string(c.Param("code")))
|
|
||||||
|
|
||||||
stations, err := getStations(code)
|
var stations []PGStation
|
||||||
if err != nil {
|
for _, station := range IDFMStations {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
if station.Fields.Id == "IDFM:"+code {
|
||||||
return
|
pgstation := PGStation{
|
||||||
|
Id: station.ComputeSlug(),
|
||||||
|
Name: station.Fields.StopName,
|
||||||
}
|
}
|
||||||
|
|
||||||
var pgs []PGStation
|
stations = append(stations, pgstation)
|
||||||
for _, station := range stations {
|
}
|
||||||
pgs = append(pgs, PGStation{
|
|
||||||
Id: station.Id,
|
|
||||||
Name: station.Name,
|
|
||||||
Slug: station.ComputeSlug(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, APIResult(c, map[string][]PGStation{
|
c.JSON(http.StatusOK, APIResult(c, map[string][]PGStation{
|
||||||
"stations": pgs,
|
"stations": stations,
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ func declareTrafficRoutes(router *gin.RouterGroup) {
|
|||||||
router.GET("/traffic/:type/:code", func(c *gin.Context) {
|
router.GET("/traffic/:type/:code", func(c *gin.Context) {
|
||||||
code := searchLine(convertLineType(string(c.Param("type"))), string(c.Param("code")))
|
code := searchLine(convertLineType(string(c.Param("type"))), string(c.Param("code")))
|
||||||
|
|
||||||
rurl, err := url.JoinPath(IDFM2_BASEURL, "v2/navitia/lines", "line:IDFM:"+code, "line_reports")
|
rurl, err := url.JoinPath(IDFM_BASEURL, "v2/navitia/lines", "line:IDFM:"+code, "line_reports")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user