From 893d3be277eaa22781cad05641ba01936de9c8aa Mon Sep 17 00:00:00 2001 From: Sergio Garcez Date: Wed, 5 Feb 2020 15:23:36 +0000 Subject: [PATCH] Tidy up --- auth.go | 24 +++++++++--------------- main.go | 45 ++++++++++++++++++++++++++------------------- uploader.go | 28 +++++++++++++++++++++------- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/auth.go b/auth.go index 44e150c..37d8e6a 100644 --- a/auth.go +++ b/auth.go @@ -4,30 +4,24 @@ import ( "fmt" "net/http" - "github.com/strava/go.strava" + strava "github.com/strava/go.strava" ) -// authHandler provides an auth url and HandlerFunc to handle its redirect -func authHandler(port string) (string, string, http.HandlerFunc, error) { - - // Application id and secret can be found at https://www.strava.com/settings/api - // define a strava.OAuthAuthenticator to hold state. - // The callback url is used to generate an AuthorizationURL. +// AuthHandler provides an url to direct the user to as well as +// an http.HandlerFunc to handle the redirect from the remote host. +func AuthHandler(port string) (authURL string, localPath string, handler http.HandlerFunc) { authenticator := &strava.OAuthAuthenticator{ CallbackURL: fmt.Sprintf("http://localhost:%s/exchange_token", port), } - callbackPath, err := authenticator.CallbackPath() - if err != nil { - return "", callbackPath, nil, err - } + // the path that our server should listen on + localPath = "/exchange_token" - authURL := authenticator.AuthorizationURL( - "state1", strava.Permissions.WriteViewPrivate, true) + authURL = authenticator.AuthorizationURL("state1", "activity:write", true) - handler := authenticator.HandlerFunc(success, failure) + handler = authenticator.HandlerFunc(success, failure) - return authURL, callbackPath, handler, nil + return } func success(auth *strava.AuthorizationResponse, w http.ResponseWriter, r *http.Request) { diff --git a/main.go b/main.go index fd80b98..855f76a 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "strings" "fmt" "io/ioutil" "log" @@ -45,19 +46,25 @@ func new() *cobra.Command { } var wg sync.WaitGroup - wg.Add(len(files)) log.Printf("Processing %d files\n", len(files)) + for _, f := range files { + if f.IsDir() || strings.HasPrefix(f.Name(), ".") || !strings.HasSuffix(f.Name(), ".fit") { + log.Printf("Ignoring %s\n", f.Name()) + continue + } + + wg.Add(1) go func(fname string) { defer wg.Done() f, err := os.Open(path.Join(inputDir, fname)) if err != nil { - log.Print(err) + log.Printf("%s - open: %s", fname, err) return } aid, err := u.Upload(fname, f) if err != nil { - log.Print(err) + log.Printf("%s - upload: %s", fname, err) return } log.Printf( @@ -82,22 +89,10 @@ func new() *cobra.Command { m := http.NewServeMux() s := &http.Server{Addr: fmt.Sprintf(":%s", port), Handler: m} - authURL, callbackPath, callbackHandler, err := authHandler(port) - if err != nil { - log.Fatal(err) - } - handleAndKill := func(in http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - in(w, r) - fmt.Fprintf(w, "\nYou can close this window") - go func() { - if err := s.Shutdown(context.Background()); err != nil { - log.Fatal(err) - } - }() - } - } - m.HandleFunc(callbackPath, handleAndKill(callbackHandler)) + + authURL, path, handler := AuthHandler(port) + + m.HandleFunc(path, handleAndKill(s, handler)) fmt.Printf("-------------------------------\n") fmt.Printf("Use this URL to authorise your application:\n\n%s\n", authURL) @@ -124,3 +119,15 @@ func main() { os.Exit(1) } } + +func handleAndKill(s *http.Server, in http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + in(w, r) + fmt.Fprintf(w, "\nYou can close this window") + go func() { + if err := s.Shutdown(context.Background()); err != nil { + log.Fatal(err) + } + }() + } +} \ No newline at end of file diff --git a/uploader.go b/uploader.go index 8a53b7d..32c3614 100644 --- a/uploader.go +++ b/uploader.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "time" "io" "log" @@ -34,12 +36,24 @@ func (u *uploader) Upload(fname string, f io.Reader) (*int64, error) { return nil, err } - uploadSummary, err := u.service.Get(resp.Id).Do() - if err != nil { - // TODO: parse error and sleep if activity isn't ready yet - log.Printf("%s - %s", fname, err) - return nil, err - } + tries := 0 + for { + + sum, err := u.service.Get(resp.Id).Do() + if err != nil { + return nil, fmt.Errorf("get uploaded activity summary: %w", err) + } - return &uploadSummary.ActivityId, nil + if sum.Error != "" { + return nil, fmt.Errorf("%s", sum.Error) + } + + if sum.Status == "Your activity is still being processed." && tries < 10 { + log.Printf("%s - waiting for activity to be processed", fname) + tries++ + time.Sleep(5* time.Second) + } else { + return &sum.ActivityId, nil + } + } }