Use IDFM website instead of official API
This commit is contained in:
parent
259b78ebd3
commit
c458a32d7b
4 changed files with 195 additions and 134 deletions
159
api/schedules.go
159
api/schedules.go
|
|
@ -2,88 +2,50 @@ package api
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const IDFM_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")
|
||||
}
|
||||
const IDFM_BASEURL = "https://api-iv.iledefrance-mobilites.fr/"
|
||||
|
||||
type IDFMSchedule struct {
|
||||
Siri struct {
|
||||
ServiceDelivery struct {
|
||||
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
||||
ProducerRef string `json:"ProducerRef"`
|
||||
ResponseMessageIdentifier string `json:"ResponseMessageIdentifier"`
|
||||
StopMonitoringDelivery []struct {
|
||||
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
||||
Version string `json:"Version"`
|
||||
Status string `json:"Status"`
|
||||
MonitoredStopVisit []struct {
|
||||
RecordedAtTime time.Time `json:"RecordedAtTime"`
|
||||
ItemIdentifier string `json:"ItemIdentifier"`
|
||||
MonitoringRef struct {
|
||||
Value string `json:"value"`
|
||||
} `json:"MonitoringRef"`
|
||||
MonitoredVehicleJourney struct {
|
||||
LineRef struct {
|
||||
Value string `json:"value"`
|
||||
} `json:"LineRef"`
|
||||
OperatorRef struct {
|
||||
Value string `json:"value"`
|
||||
} `json:"OperatorRef"`
|
||||
FramedVehicleJourneyRef struct {
|
||||
DataFrameRef struct {
|
||||
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"`
|
||||
} `json:"MonitoredStopVisit"`
|
||||
} `json:"StopMonitoringDelivery"`
|
||||
} `json:"ServiceDelivery"`
|
||||
} `json:"siri"`
|
||||
NextDepartures struct {
|
||||
StatusCode int `json:"statusCode"`
|
||||
Data []struct {
|
||||
LineId string `json:"lineId"`
|
||||
ShortName string `json:"shortName"`
|
||||
VehicleName string `json:"vehicleName,omitempty"`
|
||||
LineDirection string `json:"lineDirection"`
|
||||
Sens string `json:"sens,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
Time string `json:"time"`
|
||||
Schedule string `json:"schedule"`
|
||||
Destination struct {
|
||||
StopPointId string `json:"stopPointId"`
|
||||
StopAreaId string `json:"stopAreaId"`
|
||||
} `json:"destination,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
} `json:"data"`
|
||||
} `json:"nextDepartures"`
|
||||
CrowdsourcingReports struct {
|
||||
congestions []struct {
|
||||
DirectionId string `json:"directionId"`
|
||||
NearTimeReports struct {
|
||||
Rating *string `json:"rating"`
|
||||
} `json:"nearTimeReports"`
|
||||
} `json:"congestions"`
|
||||
} `json:"crowdsourcingReports"`
|
||||
}
|
||||
|
||||
type PGSchedule struct {
|
||||
Destination string `json:"destination"`
|
||||
Message string `json:"message"`
|
||||
Code string `json:"code,omitempty"`
|
||||
}
|
||||
|
||||
func convertLineCode(code string) string {
|
||||
|
|
@ -92,11 +54,32 @@ func convertLineCode(code string) string {
|
|||
|
||||
func declareSchedulesRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/schedules/:type/:code/:station/:way", func(c *gin.Context) {
|
||||
t := string(c.Param("type"))
|
||||
line := string(c.Param("code"))
|
||||
station := convertLineType(string(c.Param("station")))
|
||||
t := convertLineType(string(c.Param("type")))
|
||||
code := convertLineType(string(c.Param("code")))
|
||||
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 {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
|
|
@ -128,6 +111,11 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
|
|||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode >= 400 {
|
||||
c.AbortWithStatusJSON(res.StatusCode, APIResult(c, nil))
|
||||
return
|
||||
}
|
||||
|
||||
var schedules IDFMSchedule
|
||||
|
||||
dec := json.NewDecoder(res.Body)
|
||||
|
|
@ -137,34 +125,25 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
|
|||
}
|
||||
|
||||
pgs := []PGSchedule{}
|
||||
for _, vehicule := range schedules.Siri.ServiceDelivery.StopMonitoringDelivery[0].MonitoredStopVisit {
|
||||
if len(line) > 0 && vehicule.MonitoredVehicleJourney.LineRef.Value != line {
|
||||
for _, vehicule := range schedules.NextDepartures.Data {
|
||||
if (way == "A" && vehicule.Sens == "-1") || (way == "R" && vehicule.Sens == "1") {
|
||||
continue
|
||||
}
|
||||
|
||||
msg := vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime.String()
|
||||
msg := vehicule.Time + " mn"
|
||||
|
||||
if t == "metros" || t == "buses" || t == "noctiliens" || t == "tramways" {
|
||||
if vehicule.MonitoredVehicleJourney.MonitoredCall.VehicleAtStop {
|
||||
if t == "metros" {
|
||||
msg = "Train à quai"
|
||||
} else {
|
||||
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())))
|
||||
if vehicule.Code == "message" {
|
||||
msg = vehicule.Schedule
|
||||
} else if t == "rail" {
|
||||
if n, err := strconv.Atoi(vehicule.Time); err == nil {
|
||||
msg = time.Now().Add(time.Duration(n) * time.Minute).Format("15:04")
|
||||
}
|
||||
}
|
||||
|
||||
pgs = append(pgs, PGSchedule{
|
||||
Destination: vehicule.MonitoredVehicleJourney.MonitoredCall.DestinationDisplay[0].Value,
|
||||
Destination: vehicule.LineDirection,
|
||||
Message: msg,
|
||||
Code: vehicule.VehicleName,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue