Use navitia line_reports instead of non-working general-message
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2023-07-14 12:23:51 +02:00
parent 24b1102761
commit 030ec3004e
2 changed files with 83 additions and 96 deletions

View File

@ -1,12 +1,21 @@
package api package api
import ( import (
"flag"
"fmt" "fmt"
"time" "time"
"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 apiMetadata struct { type apiMetadata struct {
Call string `json:"call"` Call string `json:"call"`
Date time.Time `json:"date"` Date time.Time `json:"date"`

View File

@ -2,67 +2,14 @@ package api
import ( import (
"encoding/json" "encoding/json"
"flag"
"net/http" "net/http"
"net/url" "net/url"
"time" "strings"
navitia "git.nemunai.re/nemunaire/idfm-api/types"
"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 {
Siri struct {
ServiceDelivery struct {
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
ProducerRef string `json:"ProducerRef"`
ResponseMessageIdentifier string `json:"ResponseMessageIdentifier"`
GeneralMessageDelivery []struct {
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
Version string `json:"Version"`
Status string `json:"Status"`
InfoMessage []struct {
FormatRef string `json:"FormatRef"`
RecordedAtTime time.Time `json:"RecordedAtTime"`
ItemIdentifier string `json:"ItemIdentifier"`
InfoMessageIdentifier struct {
Value string `json:"value"`
} `json:"InfoMessageIdentifier"`
InfoMessageVersion int `json:"InfoMessageVersion"`
InfoChannelRef struct {
Value string `json:"value"`
} `json:"InfoChannelRef"`
ValidUntilTime time.Time `json:"ValidUntilTime"`
SituationRef struct {
SituationSimpleRef struct {
Value string `json:"value"`
} `json:"SituationSimpleRef"`
} `json:"SituationRef"`
Content struct {
LineRef []struct {
Value string `json:"value"`
} `json:"LineRef"`
Message []struct {
MessageType string `json:"MessageType"`
MessageText struct {
Value string `json:"value"`
Lang string `json:"lang"`
} `json:"MessageText"`
} `json:"Message"`
} `json:"Content"`
} `json:"InfoMessage"`
} `json:"GeneralMessageDelivery"`
} `json:"ServiceDelivery"`
} `json:"Siri"`
}
type PGTraffic struct { type PGTraffic struct {
Line string `json:"line"` Line string `json:"line"`
Slug string `json:"slug"` Slug string `json:"slug"`
@ -70,14 +17,24 @@ type PGTraffic struct {
Message string `json:"message"` Message string `json:"message"`
} }
func infoChannelRef2Slug(icr string) int { func effectGradation(effect navitia.Effect) int {
switch icr { switch effect {
case "Information": case navitia.JourneyStatusUnknownEffect:
return 1 return 9
case "Perturbation": case navitia.EffectNoService:
return 2 return 7
case "Commercial": case navitia.JourneyStatusReducedService:
return 6
case navitia.JourneyStatusSignificantDelay:
return 5
case navitia.JourneyStatusDetour:
return 4
case navitia.JourneyStatusAdditionalService:
return 3 return 3
case navitia.JourneyStatusOtherEffect:
return 2
case navitia.JourneyStatusStopMoved:
return 1
default: default:
return 0 return 0
} }
@ -85,9 +42,9 @@ func infoChannelRef2Slug(icr string) int {
func declareTrafficRoutes(router *gin.RouterGroup) { 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 := searchLine(convertLineType(string(c.Param("type"))), string(c.Param("code")))
rurl, err := url.JoinPath(IDFM2_BASEURL, "general-message") rurl, err := url.JoinPath(IDFM2_BASEURL, "navitia/line_reports/coverage/fr-idf/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
@ -99,10 +56,6 @@ func declareTrafficRoutes(router *gin.RouterGroup) {
return return
} }
reqquery := url.Values{}
reqquery.Add("LineRef", "STIF:Line::"+code+":")
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 {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
@ -119,8 +72,12 @@ func declareTrafficRoutes(router *gin.RouterGroup) {
} }
defer res.Body.Close() defer res.Body.Close()
var traffic IDFMTraffic if res.StatusCode != http.StatusOK {
c.DataFromReader(res.StatusCode, res.ContentLength, res.Header.Get("content-type"), res.Body, nil)
return
}
var traffic navitia.LineReports
dec := json.NewDecoder(res.Body) dec := json.NewDecoder(res.Body)
if err = dec.Decode(&traffic); err != nil { if err = dec.Decode(&traffic); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
@ -130,47 +87,68 @@ func declareTrafficRoutes(router *gin.RouterGroup) {
pgt := PGTraffic{ pgt := PGTraffic{
Line: code, Line: code,
Slug: "normal", // normal_trav alerte critique Slug: "normal", // normal_trav alerte critique
Title: "Trafic normal", Title: "",
Message: "Trafic normal sur l'ensemble de la ligne.", Message: "",
} }
message := ""
slug := 0 slug := 0
for _, msg := range traffic.Siri.ServiceDelivery.GeneralMessageDelivery[0].InfoMessage { for _, disruption := range traffic.Disruptions {
mm := "" // Ignore past or future disruptions
if disruption.Status != navitia.StatusActive {
continue
}
for _, m := range msg.Content.Message { // Ignore lift issues
if m.MessageType == "TEXT_ONLY" { if strings.Contains(strings.Join(disruption.Tags, ","), "Ascenseur") {
mm = m.MessageText.Value continue
} else if mm == "" { }
mm = m.MessageText.Value
for _, m := range disruption.Messages {
if strings.Contains(strings.Join(m.Channel.Types, ","), "title") {
if pgt.Title != "" {
pgt.Title += ", "
}
pgt.Title += m.Text
} else if strings.Contains(strings.Join(m.Channel.Types, ","), "web") {
if pgt.Message != "" {
pgt.Message += "<hr>"
}
pgt.Message += m.Text
} }
} }
message += mm icr := effectGradation(disruption.Severity.Effect)
icr := infoChannelRef2Slug(msg.InfoChannelRef.Value)
if icr > slug { if icr > slug {
slug = icr slug = icr
} switch disruption.Severity.Effect {
} case navitia.JourneyStatusUnknownEffect:
switch slug {
case 1:
pgt.Slug = "normal_trav"
pgt.Slug = "Informations"
case 2:
pgt.Slug = "alerte"
pgt.Title = "Perturbations"
case 3:
pgt.Slug = "critique" pgt.Slug = "critique"
pgt.Title = "Commercial" case navitia.EffectNoService:
pgt.Slug = "critique"
case navitia.JourneyStatusReducedService:
pgt.Slug = "alerte"
case navitia.JourneyStatusSignificantDelay:
pgt.Slug = "alerte"
case navitia.JourneyStatusDetour:
pgt.Slug = "normal_trav"
case navitia.JourneyStatusAdditionalService:
pgt.Slug = "normal_trav"
case navitia.JourneyStatusOtherEffect:
pgt.Slug = "normal_trav"
case navitia.JourneyStatusStopMoved:
pgt.Slug = "normal_trav"
default: default:
pgt.Slug = "normal" pgt.Slug = "normal"
} }
if message != "" { }
pgt.Message = message }
if pgt.Title == "" {
pgt.Title = "Trafic normal"
}
if pgt.Message == "" {
pgt.Message = "Trafic normal sur l'ensemble de la ligne."
} }
c.JSON(http.StatusOK, APIResult(c, pgt)) c.JSON(http.StatusOK, APIResult(c, pgt))