Clean up + add webserver

This commit is contained in:
William Bouzourène 2026-02-15 20:50:58 +01:00
parent 2eed16376f
commit 67e2e4d306
Signed by: bouzoure
GPG key ID: 423440D735B56BE2
7 changed files with 216 additions and 68 deletions

View file

@ -0,0 +1,84 @@
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
}