package providers import ( "context" "encoding/json" "fmt" "regexp" "strconv" "github.com/golang-io/requests" "github.com/tkrajina/gpxgo/gpx" ) func SuisseMobileCheckURL(url string) int { var id int re := regexp.MustCompile(`https?://(?:www\.)?schweizmobil\.ch/[^/]+/tour/(\d+)`) matches := re.FindStringSubmatch(url) if len(matches) > 1 { id, _ = strconv.Atoi(matches[1]) } return id } type SuisseMobileGeometry struct { Segments [][][]float64 `json:"coordinates"` } type SuisseMobileProperties struct { Name string `json:"name"` } type SuisseMobileResponse struct { Properties SuisseMobileProperties `json:"properties"` Geometry SuisseMobileGeometry `json:"geometry"` } func SuisseMobileFetch(id int) (gpx.GPX, error) { var gpxFile gpx.GPX var gpxTrack gpx.GPXTrack sess := requests.New( requests.URL("https://schweizmobil.ch"), ) resp, err := sess.DoRequest( context.Background(), requests.Path(fmt.Sprintf( "/api/6/tracks/%d", id, )), ) if err != nil { return gpxFile, err } var response SuisseMobileResponse err = json.Unmarshal(resp.Content.Bytes(), &response) if err != nil { return gpxFile, err } gpxFile.Name = response.Properties.Name for _, segments := range response.Geometry.Segments { var gpxTrackSegment gpx.GPXTrackSegment for _, point := range segments { var gpxPoint gpx.GPXPoint gpxPoint.Longitude = point[0] gpxPoint.Latitude = point[1] gpxPoint.Elevation = *gpx.NewNullableFloat64(point[2]) gpxTrackSegment.AppendPoint(&gpxPoint) } gpxTrack.AppendSegment(&gpxTrackSegment) } gpxFile.AppendTrack(&gpxTrack) return gpxFile, nil }