Skip to content

Commit

Permalink
Merge pull request #12 from bzimmer/strava-export
Browse files Browse the repository at this point in the history
strava implements export
  • Loading branch information
bzimmer authored Oct 20, 2021
2 parents 5a068db + 4daa54a commit 7c5e7e7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
30 changes: 30 additions & 0 deletions strava/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"mime/multipart"
"net/http"
"net/url"
"regexp"
"strings"
"time"

Expand All @@ -22,6 +23,9 @@ type ActivityService service
// ActivityIterFunc is called for each activity in the results
type ActivityIterFunc func(*Activity) (bool, error)

// fileNameRE allowable characters
var fileNameRE = regexp.MustCompile("[A-Za-z0-9-]+")

// WithDateRange sets the before and after date range
func WithDateRange(before, after time.Time) APIOption {
return func(v url.Values) error {
Expand Down Expand Up @@ -281,3 +285,29 @@ func (s *ActivityService) Photos(ctx context.Context, activityID int64, size int
}
return photos, nil
}

// Export exports an activity in the GPX format
func (s *ActivityService) Export(ctx context.Context, activityID int64) (*activity.Export, error) {
act, err := s.Activity(ctx, activityID, "latlng", "time", "altitude")
if err != nil {
return nil, err
}
x, err := act.GPX()
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err := x.Write(&buf); err != nil {
return nil, err
}
name := strings.Join(fileNameRE.FindAllString(act.Name, -1), "_")
exp := &activity.Export{
File: &activity.File{
Reader: &buf,
Name: name,
Format: activity.FormatGPX,
},
ID: activityID,
}
return exp, nil
}
42 changes: 42 additions & 0 deletions strava/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,45 @@ func TestUpload(t *testing.T) {
})
}
}

func TestExporter(t *testing.T) {
a := assert.New(t)

newMux := func() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/activities/6099369285", func(w http.ResponseWriter, r *http.Request) {
a.NoError(copyFile(w, "testdata/activity.json"))
})
mux.HandleFunc("/activities/6099369285/streams/", func(w http.ResponseWriter, r *http.Request) {
a.NoError(copyFile(w, "testdata/streams_export.json"))
})
return mux
}

tests := []struct {
id int64
name string
}{
{
id: 6099369285,
name: "export activity",
},
}

for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

svr := httptest.NewServer(newMux())
defer svr.Close()

client, err := newTestClient(strava.WithBaseURL(svr.URL))
a.NoError(err)
exporter := client.Exporter()
export, err := exporter.Export(context.Background(), tt.id)
a.NoError(err)
a.NotNil(export)
})
}
}
5 changes: 5 additions & 0 deletions strava/strava.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func (c *Client) Uploader() activity.Uploader {
return newUploader(c.Activity)
}

// Exporter returns an Exporter for this client
func (c *Client) Exporter() activity.Exporter {
return c.Activity
}

// WithBaseURL specifies the base url
func WithBaseURL(baseURL string) Option {
return func(c *Client) error {
Expand Down
1 change: 1 addition & 0 deletions strava/testdata/streams_export.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"latlng":{"data":[[47.597291,-122.515114],[47.597291,-122.515114],[47.597291,-122.515114],[47.597291,-122.515114],[47.597291,-122.515114],[47.597291,-122.515114],[47.597132,-122.51532],[47.597126,-122.515498],[47.59716,-122.515661],[47.597192,-122.5158],[47.597185,-122.51581],[47.597108,-122.515821],[47.597014,-122.5159],[47.597026,-122.51597],[47.597009,-122.515979],[47.597017,-122.51598],[47.597017,-122.515997],[47.597024,-122.516006],[47.597041,-122.516025],[47.597048,-122.516024],[47.59705,-122.516019],[47.59705,-122.516019],[47.59705,-122.516019],[47.59705,-122.516025],[47.597076,-122.516027],[47.597078,-122.516029],[47.597114,-122.51609],[47.597132,-122.516114],[47.597136,-122.516117],[47.597151,-122.516127],[47.597158,-122.516127],[47.597158,-122.516124],[47.597158,-122.516122],[47.597157,-122.516112],[47.597149,-122.51609],[47.597148,-122.51609],[47.597132,-122.516077],[47.59712,-122.516076],[47.597098,-122.516116],[47.597092,-122.516117],[47.597027,-122.516122],[47.597002,-122.516131],[47.596944,-122.516135],[47.596933,-122.516186],[47.596924,-122.516249],[47.596915,-122.516293],[47.596904,-122.516295],[47.596904,-122.516295],[47.596904,-122.516295],[47.596904,-122.516294],[47.5969,-122.516291],[47.59688,-122.516227],[47.596867,-122.516219],[47.596869,-122.516233],[47.596875,-122.516241],[47.596912,-122.516319],[47.596909,-122.516316],[47.596907,-122.516314],[47.596906,-122.516313],[47.596908,-122.516304],[47.596899,-122.51628],[47.596897,-122.516279],[47.596901,-122.516275],[47.596913,-122.516281],[47.596913,-122.516284],[47.596947,-122.516299],[47.596931,-122.516236],[47.596927,-122.516216],[47.596904,-122.51617],[47.596888,-122.516135],[47.596848,-122.516099],[47.596831,-122.516108],[47.596825,-122.516125],[47.596822,-122.516125],[47.596823,-122.516104],[47.596837,-122.516107],[47.596869,-122.516095],[47.596872,-122.516083],[47.596872,-122.516083],[47.596872,-122.516083],[47.596865,-122.516083],[47.59693,-122.516079],[47.597017,-122.516081],[47.59703,-122.516081],[47.597065,-122.516089],[47.597077,-122.516103],[47.597061,-122.516122],[47.597064,-122.516131],[47.597083,-122.516146],[47.597086,-122.516147],[47.59709,-122.516149],[47.597091,-122.516141],[47.597091,-122.516144],[47.597091,-122.516144],[47.597115,-122.516137],[47.597121,-122.516137],[47.597132,-122.516138],[47.597146,-122.51614],[47.597146,-122.51614],[47.597144,-122.516124],[47.597113,-122.516102],[47.5971,-122.516095],[47.596967,-122.516048],[47.596899,-122.516046],[47.596805,-122.516054],[47.596791,-122.516059],[47.596653,-122.51615],[47.596536,-122.516158],[47.596429,-122.516178],[47.596387,-122.516229],[47.596368,-122.516289],[47.596365,-122.516299],[47.59638,-122.516297],[47.596418,-122.516313],[47.596411,-122.516293],[47.596411,-122.516293],[47.596414,-122.516293],[47.596415,-122.516294],[47.596395,-122.516296],[47.596385,-122.516301],[47.596356,-122.516249],[47.59636,-122.516231],[47.596367,-122.51621],[47.596367,-122.516211],[47.596364,-122.516217],[47.596365,-122.516215],[47.596365,-122.516215],[47.596365,-122.51621],[47.596367,-122.516205],[47.596368,-122.516205],[47.596368,-122.516205],[47.596368,-122.516206],[47.596368,-122.516207],[47.596351,-122.516218],[47.59635,-122.516217],[47.596352,-122.516217],[47.59635,-122.516216],[47.59635,-122.516217],[47.596405,-122.516245],[47.596558,-122.516201],[47.596559,-122.516188],[47.59658,-122.516195],[47.596577,-122.516195],[47.596642,-122.516219],[47.596689,-122.516223],[47.596737,-122.516207],[47.59676,-122.51618],[47.596783,-122.516159],[47.596809,-122.51615],[47.596863,-122.516133],[47.596918,-122.516113],[47.596954,-122.516102],[47.597006,-122.516113],[47.597042,-122.516104],[47.597067,-122.516088],[47.597107,-122.516049],[47.597156,-122.516008],[47.597231,-122.516005],[47.597299,-122.515962],[47.597305,-122.515942],[47.597308,-122.515923],[47.597309,-122.51585],[47.597311,-122.515796],[47.597298,-122.515734],[47.597269,-122.515643],[47.597261,-122.515621],[47.597259,-122.515442],[47.597275,-122.515401],[47.597267,-122.515307],[47.597268,-122.515139],[47.597258,-122.515079]],"series_type":"distance","original_size":171,"resolution":"high"},"distance":{"data":[0.0,0.0,0.0,0.0,0.0,4.7,10.0,23.3,36.0,46.5,47.6,55.7,68.7,73.6,73.6,74.3,75.9,76.7,79.1,79.8,79.8,79.8,79.8,80.0,82.5,82.8,88.6,91.2,91.7,93.6,94.2,94.2,94.2,94.2,94.2,94.2,97.8,98.9,102.7,103.3,110.3,113.1,120.0,124.3,130.3,133.6,138.6,138.6,138.6,138.6,138.6,138.6,138.6,138.9,139.7,145.0,145.0,145.0,145.0,145.5,145.6,145.8,145.8,145.9,146.1,147.3,151.5,153.1,157.2,159.9,165.4,167.2,168.4,169.0,169.8,171.3,174.9,175.9,175.9,175.9,175.9,183.1,192.8,194.2,198.1,200.2,201.2,201.9,204.3,204.6,205.1,205.4,205.4,205.4,208.1,208.8,210.0,211.4,211.5,212.1,215.7,217.1,231.8,239.2,249.7,251.3,268.0,281.7,294.0,300.1,305.0,305.9,307.5,311.9,311.9,311.9,312.2,312.3,314.5,315.6,321.4,322.8,324.8,324.8,324.8,324.9,325.0,325.3,325.7,325.8,325.8,325.8,325.8,325.8,325.8,325.9,325.9,325.9,333.0,350.2,351.3,353.8,353.8,361.1,366.2,371.7,374.9,377.9,380.6,386.7,393.0,397.0,402.9,407.0,410.0,415.4,421.7,429.9,438.4,440.0,441.3,446.3,450.3,455.1,462.7,464.5,478.2,481.7,488.4,501.0,505.8],"series_type":"distance","original_size":171,"resolution":"high"},"altitude":{"data":[7.6,7.6,7.8,7.8,7.6,7.4,7.4,7.4,7.6,7.8,7.6,7.6,7.6,7.6,7.6,7.6,7.8,7.6,7.6,7.8,7.8,7.8,7.8,7.6,7.6,7.6,7.8,7.8,7.8,7.8,7.8,7.8,7.8,7.8,7.8,7.8,7.8,7.8,7.8,7.8,7.2,7.6,7.2,7.4,7.0,7.2,7.2,7.0,6.6,6.4,6.4,6.6,7.0,6.8,6.6,6.0,6.0,6.0,6.0,6.0,6.0,6.0,6.0,6.0,6.0,6.0,6.0,6.0,7.2,5.8,5.8,5.8,5.8,5.8,6.0,6.0,6.2,6.6,6.6,6.6,6.8,7.0,7.0,7.0,6.8,6.8,6.6,6.6,6.2,6.0,6.0,5.6,5.2,5.4,5.2,5.2,5.4,5.4,5.4,6.4,6.6,6.6,7.0,7.2,7.2,7.2,6.8,7.0,7.0,7.2,7.2,7.2,7.0,7.0,6.8,6.8,6.8,7.6,7.8,7.0,7.0,6.8,6.6,6.6,6.6,6.6,6.4,5.6,5.6,5.6,5.6,5.6,5.6,5.6,5.4,5.4,5.4,5.4,5.4,5.4,5.0,5.0,4.8,5.4,5.2,5.2,5.2,5.2,5.2,5.4,5.8,6.8,7.0,7.2,6.6,6.6,6.4,6.6,6.8,6.8,6.8,6.8,6.8,6.8,6.8,6.8,6.4,6.4,6.6,7.0,6.8],"series_type":"distance","original_size":171,"resolution":"high"},"time":{"data":[0.0,1.0,3.0,4.0,5.0,9.0,12.0,21.0,28.0,35.0,36.0,43.0,51.0,56.0,58.0,60.0,66.0,68.0,74.0,80.0,85.0,86.0,87.0,94.0,100.0,108.0,141.0,170.0,172.0,181.0,187.0,196.0,198.0,201.0,214.0,219.0,243.0,244.0,248.0,249.0,256.0,258.0,266.0,277.0,285.0,292.0,304.0,305.0,308.0,318.0,323.0,338.0,350.0,359.0,364.0,403.0,407.0,409.0,412.0,423.0,431.0,434.0,444.0,462.0,463.0,486.0,489.0,490.0,493.0,496.0,502.0,504.0,508.0,510.0,514.0,516.0,519.0,523.0,524.0,531.0,536.0,543.0,549.0,550.0,552.0,554.0,556.0,557.0,561.0,562.0,564.0,567.0,572.0,578.0,598.0,602.0,609.0,621.0,624.0,639.0,641.0,642.0,651.0,656.0,661.0,662.0,673.0,682.0,691.0,695.0,699.0,701.0,703.0,708.0,711.0,717.0,723.0,732.0,735.0,736.0,741.0,742.0,746.0,747.0,749.0,755.0,761.0,768.0,778.0,788.0,791.0,793.0,796.0,818.0,820.0,829.0,834.0,838.0,873.0,882.0,888.0,893.0,903.0,911.0,914.0,917.0,920.0,922.0,924.0,928.0,932.0,935.0,939.0,943.0,946.0,950.0,954.0,959.0,965.0,966.0,967.0,972.0,975.0,979.0,985.0,986.0,994.0,996.0,1000.0,1007.0,1010.0],"series_type":"distance","original_size":171,"resolution":"high"}}

0 comments on commit 7c5e7e7

Please sign in to comment.