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 and sign in (email + OTP)
    2. F12 → Application → Cookies → https://www.strava.com
    3. Copy the Value of strava_remember_token
    4. Configure it:
       Local (config/config.yml):
         sync.strava.web_auth.jwt: "<JWT>"
       Env:
         export GIT_FIT_STRAVA_WEB_AUTH_JWT="<JWT>"
       CI (base64 secret):
         echo -n "<JWT>" | base64 -w0 | gh secret set GIT_FIT_STRAVA_WEB_AUTH_AUTH_SEED
  Token lifetime: ~30 days (JWT exp claim)
HELP

Instance Method Summary collapse

Instance Method Details

#fetch(*activity_ids) ⇒ Object



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
81
82
83
84
85
# File 'lib/git_fit/cli/strava.rb', line 49

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 = web_cfg['cookie_path'] || GitFit::Auth.file('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)
  print_token_info(client)
  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