Class: GitFit::StravaCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/git_fit/cli/strava.rb

Constant Summary collapse

VALID_FORMATS =
%w[original tcx gpx].freeze
TOKEN_HELP =
<<~HELP
  Token expired or invalid — generate a fresh one:
    1. Open https://www.strava.com/login → sign in with email + OTP
    2. F12 → Application → Cookies → https://www.strava.com
       Copy Value of: strava_remember_token
    3. Local use:
         config/config.yml → sync.strava.web_auth.jwt: "<paste JWT>"
       CI use:
         echo -n "<paste JWT>" | base64 -w0 | gh secret set GIT_FIT_STRAVA_WEB_AUTH_AUTH_SEED -R Lax/git-fit
  Docs: docs/strava-web-auth.md
  Token lifetime: ~30 days (JWT exp claim)
HELP

Instance Method Summary collapse

Instance Method Details

#fetch(*activity_ids) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/git_fit/cli/strava.rb', line 45

def fetch(*activity_ids)
  format = options[:format].to_s.downcase
  unless VALID_FORMATS.include?(format)
    raise ArgumentError, "unknown --format '#{format}' (valid: #{VALID_FORMATS.join(', ')})"
  end

  config = git_fit_config
  web_cfg = config.sync_config('strava')['web_auth'] || {}
  jwt = options[:jwt] || web_cfg['jwt']
  auth_seed = web_cfg['auth_seed']

  output_dir = options[:output]
  FileUtils.mkdir_p(output_dir)
  cache_dir = File.join('data', 'cache', 'strava_downloads')
  FileUtils.mkdir_p(cache_dir)
  cookie_path = File.join('data', 'cache', 'strava_session.yml')

  if activity_ids.empty?
    activity_ids = pending_ids(config, output_dir)
    return if activity_ids.nil?
  end

  client = build_client(jwt, auth_seed, cookie_path)
  fmt_sym = format.to_sym
  stats = { success: 0, skipped: 0, failed: 0, token_errors: 0 }

  activity_ids.each do |aid|
    process_activity(client, aid, output_dir, cache_dir, fmt_sym, stats)
  end

  client.persist_cookies!

  say ''
  say "fetch: #{stats[:success]} downloaded, #{stats[:skipped]} skipped, #{stats[:failed]} failed", :blue
  say TOKEN_HELP, :yellow if stats[:token_errors] > 0 && stats[:success] == 0
end