Initial commit
This commit is contained in:
commit
0a854f708a
17 changed files with 796 additions and 0 deletions
153
api/schedules.go
Normal file
153
api/schedules.go
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"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")
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
type PGSchedule struct {
|
||||
Destination string `json:"destionation"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func declareSchedulesRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/schedules/:type/:code/:station/:way", func(c *gin.Context) {
|
||||
t := string(c.Param("type"))
|
||||
station := convertLineType(string(c.Param("station")))
|
||||
|
||||
rurl, err := url.JoinPath(IDFM_BASEURL, "stop-monitoring")
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
requrl, err := url.Parse(rurl)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
reqquery := url.Values{}
|
||||
reqquery.Add("MonitoringRef", station)
|
||||
requrl.RawQuery = reqquery.Encode()
|
||||
|
||||
req, err := http.NewRequest("GET", requrl.String(), nil)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("apikey", IDFM_TOKEN)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var schedules IDFMSchedule
|
||||
|
||||
dec := json.NewDecoder(res.Body)
|
||||
if err = dec.Decode(&schedules); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
pgs := []PGSchedule{}
|
||||
for _, vehicule := range schedules.Siri.ServiceDelivery.StopMonitoringDelivery[0].MonitoredStopVisit {
|
||||
msg := vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime.String()
|
||||
|
||||
if t == "metros" || t == "buses" || t == "tramways" {
|
||||
if vehicule.MonitoredVehicleJourney.MonitoredCall.VehicleAtStop {
|
||||
msg = "À quai"
|
||||
} else {
|
||||
msg = fmt.Sprintf("%d mn", int(math.Floor(time.Until(vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime).Minutes())))
|
||||
}
|
||||
}
|
||||
|
||||
pgs = append(pgs, PGSchedule{
|
||||
Destination: vehicule.MonitoredVehicleJourney.MonitoredCall.DestinationDisplay[0].Value,
|
||||
Message: msg,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, pgs)
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue