Use IDFM website instead of official API
This commit is contained in:
parent
259b78ebd3
commit
c458a32d7b
14
api/lines.go
14
api/lines.go
@ -82,6 +82,16 @@ func convertLineType(old string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func searchLine(t, code string) string {
|
||||||
|
for _, line := range IDFMLines {
|
||||||
|
if line.Fields.ShortName == code && (line.Fields.TransportMode == t || line.Fields.NetworkName == t) {
|
||||||
|
return line.Fields.IdLine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
func declareLinesRoutes(router *gin.RouterGroup) {
|
func declareLinesRoutes(router *gin.RouterGroup) {
|
||||||
router.GET("/lines", func(c *gin.Context) {
|
router.GET("/lines", func(c *gin.Context) {
|
||||||
var modes []string
|
var modes []string
|
||||||
@ -121,10 +131,10 @@ func declareLinesRoutes(router *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pgline := PGLine{
|
pgline := PGLine{
|
||||||
Code: fmt.Sprintf("STIF:Line::%s:", line.Fields.IdLine),
|
Code: line.Fields.ShortName,
|
||||||
Name: name,
|
Name: name,
|
||||||
Directions: "",
|
Directions: "",
|
||||||
Id: line.Fields.IdLine,
|
Id: fmt.Sprintf("STIF:Line::%s:", line.Fields.IdLine),
|
||||||
}
|
}
|
||||||
|
|
||||||
lines = append(lines, pgline)
|
lines = append(lines, pgline)
|
||||||
|
159
api/schedules.go
159
api/schedules.go
@ -2,88 +2,50 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const IDFM_BASEURL = "https://prim.iledefrance-mobilites.fr/marketplace"
|
const IDFM_BASEURL = "https://api-iv.iledefrance-mobilites.fr/"
|
||||||
|
|
||||||
var IDFM_TOKEN = ""
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
flag.StringVar(&IDFM_TOKEN, "token-IDFM", IDFM_TOKEN, "Token to access IDFM API")
|
|
||||||
}
|
|
||||||
|
|
||||||
type IDFMSchedule struct {
|
type IDFMSchedule struct {
|
||||||
Siri struct {
|
NextDepartures struct {
|
||||||
ServiceDelivery struct {
|
StatusCode int `json:"statusCode"`
|
||||||
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
Data []struct {
|
||||||
ProducerRef string `json:"ProducerRef"`
|
LineId string `json:"lineId"`
|
||||||
ResponseMessageIdentifier string `json:"ResponseMessageIdentifier"`
|
ShortName string `json:"shortName"`
|
||||||
StopMonitoringDelivery []struct {
|
VehicleName string `json:"vehicleName,omitempty"`
|
||||||
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
LineDirection string `json:"lineDirection"`
|
||||||
Version string `json:"Version"`
|
Sens string `json:"sens,omitempty"`
|
||||||
Status string `json:"Status"`
|
Code string `json:"code,omitempty"`
|
||||||
MonitoredStopVisit []struct {
|
Time string `json:"time"`
|
||||||
RecordedAtTime time.Time `json:"RecordedAtTime"`
|
Schedule string `json:"schedule"`
|
||||||
ItemIdentifier string `json:"ItemIdentifier"`
|
Destination struct {
|
||||||
MonitoringRef struct {
|
StopPointId string `json:"stopPointId"`
|
||||||
Value string `json:"value"`
|
StopAreaId string `json:"stopAreaId"`
|
||||||
} `json:"MonitoringRef"`
|
} `json:"destination,omitempty"`
|
||||||
MonitoredVehicleJourney struct {
|
Source string `json:"source,omitempty"`
|
||||||
LineRef struct {
|
} `json:"data"`
|
||||||
Value string `json:"value"`
|
} `json:"nextDepartures"`
|
||||||
} `json:"LineRef"`
|
CrowdsourcingReports struct {
|
||||||
OperatorRef struct {
|
congestions []struct {
|
||||||
Value string `json:"value"`
|
DirectionId string `json:"directionId"`
|
||||||
} `json:"OperatorRef"`
|
NearTimeReports struct {
|
||||||
FramedVehicleJourneyRef struct {
|
Rating *string `json:"rating"`
|
||||||
DataFrameRef struct {
|
} `json:"nearTimeReports"`
|
||||||
Value string `json:"value"`
|
} `json:"congestions"`
|
||||||
} `json:"DataFrameRef"`
|
} `json:"crowdsourcingReports"`
|
||||||
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"`
|
|
||||||
} `json:"MonitoredStopVisit"`
|
|
||||||
} `json:"StopMonitoringDelivery"`
|
|
||||||
} `json:"ServiceDelivery"`
|
|
||||||
} `json:"siri"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
@ -92,11 +54,32 @@ func convertLineCode(code string) string {
|
|||||||
|
|
||||||
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 := string(c.Param("type"))
|
t := convertLineType(string(c.Param("type")))
|
||||||
line := string(c.Param("code"))
|
code := convertLineType(string(c.Param("code")))
|
||||||
station := convertLineType(string(c.Param("station")))
|
station := string(c.Param("station"))
|
||||||
|
way := string(c.Param("way"))
|
||||||
|
|
||||||
rurl, err := url.JoinPath(IDFM_BASEURL, "stop-monitoring")
|
if !strings.HasPrefix(code, "line:IDFM:") {
|
||||||
|
if len(code) != 6 || !strings.HasPrefix(code, "C") {
|
||||||
|
code = searchLine(t, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
code = "line:IDFM:" + code
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(station, "stop_area:IDFM:") {
|
||||||
|
if _, err := strconv.ParseInt(station, 10, 64); err != nil {
|
||||||
|
station, err = searchStation(code, station)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
station = "stop_area:IDFM:" + station
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "stops", station, "realTime")
|
||||||
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
|
||||||
@ -128,6 +111,11 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
if res.StatusCode >= 400 {
|
||||||
|
c.AbortWithStatusJSON(res.StatusCode, APIResult(c, nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var schedules IDFMSchedule
|
var schedules IDFMSchedule
|
||||||
|
|
||||||
dec := json.NewDecoder(res.Body)
|
dec := json.NewDecoder(res.Body)
|
||||||
@ -137,34 +125,25 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pgs := []PGSchedule{}
|
pgs := []PGSchedule{}
|
||||||
for _, vehicule := range schedules.Siri.ServiceDelivery.StopMonitoringDelivery[0].MonitoredStopVisit {
|
for _, vehicule := range schedules.NextDepartures.Data {
|
||||||
if len(line) > 0 && vehicule.MonitoredVehicleJourney.LineRef.Value != line {
|
if (way == "A" && vehicule.Sens == "-1") || (way == "R" && vehicule.Sens == "1") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime.String()
|
msg := vehicule.Time + " mn"
|
||||||
|
|
||||||
if t == "metros" || t == "buses" || t == "noctiliens" || t == "tramways" {
|
if vehicule.Code == "message" {
|
||||||
if vehicule.MonitoredVehicleJourney.MonitoredCall.VehicleAtStop {
|
msg = vehicule.Schedule
|
||||||
if t == "metros" {
|
} else if t == "rail" {
|
||||||
msg = "Train à quai"
|
if n, err := strconv.Atoi(vehicule.Time); err == nil {
|
||||||
} else {
|
msg = time.Now().Add(time.Duration(n) * time.Minute).Format("15:04")
|
||||||
msg = "A l'arret"
|
|
||||||
}
|
|
||||||
} else if time.Until(vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime) < 0 {
|
|
||||||
if t == "metros" {
|
|
||||||
msg = "Train retardé"
|
|
||||||
} else {
|
|
||||||
msg = "…"
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
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.MonitoredVehicleJourney.MonitoredCall.DestinationDisplay[0].Value,
|
Destination: vehicule.LineDirection,
|
||||||
Message: msg,
|
Message: msg,
|
||||||
|
Code: vehicule.VehicleName,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
135
api/stations.go
135
api/stations.go
@ -3,73 +3,136 @@ package api
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"golang.org/x/text/transform"
|
||||||
|
"golang.org/x/text/unicode/norm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IDFMStation struct {
|
type IDFMStation struct {
|
||||||
DatasetID string `json:"datasetid"`
|
|
||||||
RecordIDs string `json:"recordid"`
|
|
||||||
Fields struct {
|
|
||||||
Id string `json:"id"`
|
|
||||||
PointGeo []float64 `json:"pointgeo"`
|
|
||||||
StopId string `json:"stop_id"`
|
|
||||||
StopName string `json:"stop_name"`
|
|
||||||
OperatorName string `json:"operatorname"`
|
|
||||||
NomCommune string `json:"nom_commune"`
|
|
||||||
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"`
|
Type string `json:"type"`
|
||||||
Coordinates []float64 `json:"coordinates"`
|
Id string `json:"id"`
|
||||||
} `json:"geometry"`
|
X float64 `json:"x"`
|
||||||
|
Y float64 `json:"y"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
ZipCode string `json:"zipCode"`
|
||||||
|
City string `json:"city"`
|
||||||
|
Elevator bool `json:"elevator"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *IDFMStation) ComputeSlug() string {
|
||||||
|
isMn := func(r rune) bool {
|
||||||
|
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
|
||||||
|
}
|
||||||
|
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
|
||||||
|
|
||||||
|
r, _ := ioutil.ReadAll(transform.NewReader(strings.NewReader(s.Name), t))
|
||||||
|
|
||||||
|
return strings.ToLower(strings.Replace(strings.Replace(strings.Replace(string(r), " ", "+", -1), "'", "+", -1), "-", "+", -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
type PGStation struct {
|
type PGStation struct {
|
||||||
Id string `json:"slug"`
|
Id string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Slug string `json:"slug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var IDFMStations []IDFMStation
|
var IDFMStations []IDFMStation
|
||||||
|
|
||||||
func init() {
|
func searchStation(code, station string) (string, error) {
|
||||||
fd, err := os.Open("arrets-lignes.json")
|
stations, err := getStations(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Unable to open `arrets-lignes.json`:", err.Error())
|
return station, err
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
|
||||||
|
|
||||||
dec := json.NewDecoder(fd)
|
code = strings.TrimPrefix(code, "line:IDFM:")
|
||||||
if err = dec.Decode(&IDFMStations); err != nil {
|
|
||||||
log.Fatal("Unable to decode `arrets-lignes.json`:", err.Error())
|
for _, st := range stations {
|
||||||
|
if st.ComputeSlug() == station {
|
||||||
|
return st.Id, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return station, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStations(code string) (stations []IDFMStation, err error) {
|
||||||
|
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "stops")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
requrl, err := url.Parse(rurl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
code := convertLineCode(string(c.Param("code")))
|
t := convertLineType(string(c.Param("type")))
|
||||||
|
code := string(c.Param("code"))
|
||||||
|
|
||||||
var stations []PGStation
|
if !strings.HasPrefix(code, "line:IDFM:") {
|
||||||
for _, station := range IDFMStations {
|
if len(code) != 6 || !strings.HasPrefix(code, "C") {
|
||||||
if station.Fields.Id == code {
|
code = searchLine(t, code)
|
||||||
pgstation := PGStation{
|
|
||||||
Id: fmt.Sprintf("STIF:StopPoint:Q:%s:", strings.TrimPrefix(station.Fields.StopId, "IDFM:")),
|
|
||||||
Name: station.Fields.StopName,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stations = append(stations, pgstation)
|
code = "line:IDFM:" + code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stations, err := getStations(code)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var pgs []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": stations,
|
"stations": pgs,
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
@ -9,6 +10,14 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const IDFM2_BASEURL = "https://prim.iledefrance-mobilites.fr/marketplace"
|
||||||
|
|
||||||
|
var IDFM_TOKEN = ""
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&IDFM_TOKEN, "token-IDFM", IDFM_TOKEN, "Token to access IDFM API")
|
||||||
|
}
|
||||||
|
|
||||||
type IDFMTraffic struct {
|
type IDFMTraffic struct {
|
||||||
Siri struct {
|
Siri struct {
|
||||||
ServiceDelivery struct {
|
ServiceDelivery struct {
|
||||||
@ -78,7 +87,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 := convertLineType(string(c.Param("code")))
|
code := convertLineType(string(c.Param("code")))
|
||||||
|
|
||||||
rurl, err := url.JoinPath(IDFM_BASEURL, "general-message")
|
rurl, err := url.JoinPath(IDFM2_BASEURL, "general-message")
|
||||||
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
|
||||||
@ -91,7 +100,7 @@ func declareTrafficRoutes(router *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reqquery := url.Values{}
|
reqquery := url.Values{}
|
||||||
reqquery.Add("LineRef", code)
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user