2022-10-21 21:54:17 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-10-22 01:12:59 +00:00
|
|
|
"fmt"
|
2022-10-22 22:13:02 +00:00
|
|
|
"io/ioutil"
|
2022-10-21 21:54:17 +00:00
|
|
|
"net/http"
|
2022-10-22 22:13:02 +00:00
|
|
|
"net/url"
|
2022-10-22 01:12:59 +00:00
|
|
|
"strings"
|
2022-10-22 22:13:02 +00:00
|
|
|
"unicode"
|
2022-10-21 21:54:17 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-10-22 22:13:02 +00:00
|
|
|
|
|
|
|
"golang.org/x/text/transform"
|
|
|
|
"golang.org/x/text/unicode/norm"
|
2022-10-21 21:54:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IDFMStation struct {
|
2022-10-22 22:13:02 +00:00
|
|
|
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))
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PGStation struct {
|
2022-10-22 22:13:02 +00:00
|
|
|
Id string `json:"id"`
|
2022-10-21 21:54:17 +00:00
|
|
|
Name string `json:"name"`
|
2022-10-22 22:13:02 +00:00
|
|
|
Slug string `json:"slug"`
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var IDFMStations []IDFMStation
|
|
|
|
|
2022-10-22 22:13:02 +00:00
|
|
|
func searchStation(code, station string) (string, error) {
|
|
|
|
stations, err := getStations(code)
|
2022-10-21 21:54:17 +00:00
|
|
|
if err != nil {
|
2022-10-22 22:13:02 +00:00
|
|
|
return station, err
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 22:13:02 +00:00
|
|
|
code = strings.TrimPrefix(code, "line:IDFM:")
|
|
|
|
|
|
|
|
for _, st := range stations {
|
|
|
|
if st.ComputeSlug() == station {
|
|
|
|
return st.Id, nil
|
|
|
|
}
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|
2022-10-22 22:13:02 +00:00
|
|
|
|
|
|
|
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
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func declareStationsRoutes(router *gin.RouterGroup) {
|
|
|
|
router.GET("/stations/:type/:code", func(c *gin.Context) {
|
2022-10-22 22:13:02 +00:00
|
|
|
t := convertLineType(string(c.Param("type")))
|
2022-10-23 14:45:11 +00:00
|
|
|
code := convertCode(t, string(c.Param("code")))
|
2022-10-22 22:13:02 +00:00
|
|
|
|
|
|
|
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(),
|
|
|
|
})
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, APIResult(c, map[string][]PGStation{
|
2022-10-22 22:13:02 +00:00
|
|
|
"stations": pgs,
|
2022-10-21 21:54:17 +00:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|