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
141
api/stations.go
141
api/stations.go
|
|
@ -3,73 +3,136 @@ package api
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"net/url"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"golang.org/x/text/transform"
|
||||
"golang.org/x/text/unicode/norm"
|
||||
)
|
||||
|
||||
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"`
|
||||
Coordinates []float64 `json:"coordinates"`
|
||||
} `json:"geometry"`
|
||||
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"`
|
||||
}
|
||||
|
||||
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 {
|
||||
Id string `json:"slug"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
var IDFMStations []IDFMStation
|
||||
|
||||
func init() {
|
||||
fd, err := os.Open("arrets-lignes.json")
|
||||
func searchStation(code, station string) (string, error) {
|
||||
stations, err := getStations(code)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to open `arrets-lignes.json`:", err.Error())
|
||||
return station, err
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
dec := json.NewDecoder(fd)
|
||||
if err = dec.Decode(&IDFMStations); err != nil {
|
||||
log.Fatal("Unable to decode `arrets-lignes.json`:", err.Error())
|
||||
code = strings.TrimPrefix(code, "line:IDFM:")
|
||||
|
||||
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) {
|
||||
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
|
||||
for _, station := range IDFMStations {
|
||||
if station.Fields.Id == code {
|
||||
pgstation := PGStation{
|
||||
Id: fmt.Sprintf("STIF:StopPoint:Q:%s:", strings.TrimPrefix(station.Fields.StopId, "IDFM:")),
|
||||
Name: station.Fields.StopName,
|
||||
}
|
||||
|
||||
stations = append(stations, pgstation)
|
||||
if !strings.HasPrefix(code, "line:IDFM:") {
|
||||
if len(code) != 6 || !strings.HasPrefix(code, "C") {
|
||||
code = searchLine(t, code)
|
||||
}
|
||||
|
||||
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{
|
||||
"stations": stations,
|
||||
"stations": pgs,
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue