Back to official API
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2024-07-26 12:47:08 +02:00
commit 3e39c5e5c0
6 changed files with 202 additions and 301 deletions

View file

@ -2,10 +2,10 @@ package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"unicode"
@ -16,14 +16,24 @@ import (
)
type IDFMStation struct {
Type string `json:"type"`
Id string `json:"id"`
X float64 `json:"x"`
Y float64 `json:"y"`
Name string `json:"name"`
ZipCode string `json:"zipCode"`
City string `json:"city"`
Elevator bool `json:"elevator"`
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"`
Coordinates []float64 `json:"coordinates"`
} `json:"geometry"`
}
func (s *IDFMStation) ComputeSlug() string {
@ -32,99 +42,70 @@ func (s *IDFMStation) ComputeSlug() string {
}
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))
}
type PGStation struct {
Id string `json:"id"`
Id string `json:"slug"`
Name string `json:"name"`
Slug string `json:"slug"`
}
var IDFMStations []IDFMStation
func searchStation(code, station string) (string, error) {
stations, err := getStations(code)
func init() {
fd, err := os.Open("arrets-lignes.json")
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 {
log.Fatal("Unable to decode `arrets-lignes.json`:", err.Error())
}
}
for _, st := range stations {
if st.ComputeSlug() == station {
return st.Id, nil
func searchStation(code, station string) ([]string, error) {
code = "IDFM:" + strings.TrimPrefix(code, "line:IDFM:")
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:")+":")
}
}
}
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
if len(res) == 0 {
return []string{station}, nil
} else {
return res, nil
}
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) {
router.GET("/stations/:type/:code", func(c *gin.Context) {
t := convertLineType(string(c.Param("type")))
code := convertCode(t, string(c.Param("code")))
code := convertLineCode(string(c.Param("code")))
stations, err := getStations(code)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
var stations []PGStation
for _, station := range IDFMStations {
if station.Fields.Id == "IDFM:"+code {
pgstation := PGStation{
Id: station.ComputeSlug(),
Name: station.Fields.StopName,
}
var pgs []PGStation
for _, station := range stations {
pgs = append(pgs, PGStation{
Id: station.Id,
Name: station.Name,
Slug: station.ComputeSlug(),
})
stations = append(stations, pgstation)
}
}
c.JSON(http.StatusOK, APIResult(c, map[string][]PGStation{
"stations": pgs,
"stations": stations,
}))
})
}