佳明批量下载活动文件导入Strava

佳明导出.fit文件

  • 国际服:https://www.garmin.com/en-US/account/profile

    国内服:https://www.garmin.cn/zh-CN/account/profile

  • 选择Data Management-Export Your Data,导出所有数据,数据准备完成后会发送邮件提供下载链接
    在这里插入图片描述

  • 所有.fit活动文件在/DI_CONNECT/DI-Connect-Uploaded-Files/中的压缩包内

解析.fit文件

可以用fitparse库解析.fit文件,选取自己想上传的文件

例如想获取活动时间在2024-1-20之前的文件:

from fitparse import FitFile
from datetime import datetime
fit = FitFile(fit_filepath)
cutoff_date = datetime(2024, 1, 20)
for record in fit.get_messages('record'):
  timestamp = record.get_value('timestamp')
    if timestamp and timestamp < cutoff_date:
      print(f"File {fit_filepath} has a timestamp before 2024/01/20: {timestamp}")
      break

strava导入.fit文件

  • 在 Strava 开发者控制台创建应用:

    • Strava Developer 开发者平台,选择"Create & Manage Your App"创建一个新的应用,网站可以设置成http://localhost:8080,用于用户授权后传authorization_code
      在这里插入图片描述

    • 获取应用的 Client IDClient Secret
      在这里插入图片描述

  • 用户授权:

    • 本地起服务:

      import http.server
      import socketserver
      import urllib.parse
      PORT = 8080
      Handler = http.server.SimpleHTTPRequestHandler
      class MyHandler(http.server.BaseHTTPRequestHandler):
          def do_GET(self):
              parsed_path = urllib.parse.urlparse(self.path)
              query_params = urllib.parse.parse_qs(parsed_path.query)
              if 'code' in query_params:
                  authorization_code = query_params['code'][0]
                  print(f"Authorization code received: {authorization_code}")
                  self.send_response(200)
                  self.end_headers()
                  self.wfile.write(b"Authorization code received successfully")
              else:
                  self.send_response(400)
                  self.end_headers()
                  self.wfile.write(b"Error: Authorization code not found")
      with socketserver.TCPServer(("", PORT), MyHandler) as httpd:
          print(f"Serving at port {PORT}")
          httpd.serve_forever()
      
    • 替换client_id,在浏览器中访问链接:
      https://www.strava.com/oauth/authorize?client_id={client_id}&response_type=code&redirect_uri=http://localhost:8080&scope=activity:write,read&state=random_string&approval_prompt=auto

    • 在浏览器中授权后,本地服务端获得Authorization Code

  • OAuth认证:

    • 根据Client ID、Client Secret、Authorization Code,获取有读写权限的Access Token
      import requests
      data = {
          'client_id': client_id,
          'client_secret': client_secret,
          'code': authorization_code,
          'grant_type': 'authorization_code',
          'redirect_uri': 'http://localhost:8080'
      }
      response = requests.post('https://www.strava.com/oauth/token', data=data)
      if response.status_code == 200:
          token_data = response.json()
          new_access_token = token_data['access_token']  # 新的 Access Token
          new_refresh_token = token_data['refresh_token']  # 新的 Refresh Token
          print(f"New Access Token: {new_access_token}")
          print(f"New Refresh Token: {new_refresh_token}")
      else:
          print(f"Failed to refresh token: {response.text}")
      
  • 过期刷新Access Token:

    • Refresh Token在Strava创建的应用页面可找到
    • 上一步的data改成如下,重新请求获取Access Token
      refresh_data = {
          'client_id': client_id,
          'client_secret': client_secret,
          'refresh_token': refresh_token,
          'grant_type': 'refresh_token'
      }
      
  • 批量上传.fit文件

      import requests
      import os
      upload_url = 'https://www.strava.com/api/v3/uploads'
      fit_files = [f for f in os.listdir(source_folder) if f.endswith('.fit')]
      for fit_file in fit_files:
          fit_filepath = os.path.join(source_folder, fit_file)
          print(f"Uploading {fit_filepath}...")
          with open(fit_filepath, 'rb') as f:
              files = {'file': (fit_file, f, 'application/octet-stream')}
              params = {
                  'access_token': access_token,
                  'data_type': 'fit',  # 上传文件类型为 FIT
                  'name': fit_file,  # 设置上传活动的名称,默认用文件名
              }
              response = requests.post(upload_url, files=files, data=params)
              if response.status_code == 201:
                  print(f"Successfully uploaded {fit_file} to Strava.")
              else:
                  print(f"Failed to upload {fit_file}: {response.text}")
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值