跳转到主要内容

A Python package that makes it easy to access and download data from the Strava V3 REST API.

项目描述

欢迎来到stravalib

DOI PyPI PyPI - Python Version Documentation Status Package Tests Status PyPI - Downloads codecov

The stravalib Python package provides easy-to-use tools for accessing and downloading Strava data from the Strava V3 web service. Stravalib provides a Client class that supports

  • 使用stravalib进行身份验证
  • 访问和下载Strava活动、俱乐部和个人资料数据
  • 修改账户活动

它还支持通过Python Pint库处理日期/时间/时间属性和数量。

依赖项

  • Python 3.9+
  • Setuptools用于安装依赖项
  • 其他Python库(使用pip安装时自动安装):requests, pytz, pint, arrow, pydantic

安装

该软件包可在PyPI上使用pip进行安装

pip install stravalib

如何为Stravalib做出贡献

开始吧!

准备好贡献了吗?以下是设置Stravalib进行本地开发的方法。

  1. 在GitHub上分支仓库

要创建GitHub上自己的仓库副本,请导航到stravalib/stravalib <https://github.com/stravalib/stravalib>_仓库,然后在页面右上角点击分支按钮。

  1. 在本地克隆您的分支

使用git clone获取本地文件系统中stravalib仓库的本地副本:

$ git clone git@github.com:your_name_here/stravalib.git
$ cd stravalib/
  1. 为本地开发设置您的分支

此库的文档使用sphinx创建。要构建文档的HTML版本,请使用以下命令

$ make -C docs html

从源代码构建

要本地以可编辑模式构建项目,请访问项目根目录并运行

$ pip install -e .

要执行unit或集成测试,您需要运行

$ make test

本地测试

要运行端到端测试,您需要将test.ini-example(您可以在/stravalib/tests/中找到它)重命名为test.ini。在test.ini中提供您的access_tokenactivity_id。现在您可以运行

shell$ pytest stravalib/tests/functional

拉取请求和测试

请添加覆盖您更改的测试,这将大大减少审查和合并您的拉取请求的努力。如果您需要,有一个基于responses包中的RequestsMock的pytest fixture mock_strava_api。它阻止对实际Strava API发出请求,而是根据已发布的Strava API文档中的示例注册响应。此fixture的示例用法可以在stravalib.tests.integration包中找到。

基本用法

请查看源代码(特别是如果您想与API玩耍,请特别查看stravalib.client.Client类)。目前实现了Strava API的大部分功能;然而,某些功能(如流)仍在待办事项列表中。

认证

为了使用此库,您需要在Strava中创建一个应用程序,这是免费的。请参阅此教程以获取有关使用Strava创建应用程序的说明 - 我们将很快更新我们的文档。

注意 我们将在接下来的几个月内更新我们的文档,以提供支持此功能的明确说明。

创建您的应用程序后,stravalib有几个辅助方法使认证更加容易。

from stravalib.client import Client

client = Client()
authorize_url = client.authorization_url(
    client_id=1234, redirect_uri="http://localhost:8282/authorized"
)
# Have the user click the authorization URL, a 'code' param will be added to the redirect_uri
# .....

# Extract the code from your webapp response
code = requests.get("code")  # or whatever your framework does
token_response = client.exchange_code_for_token(
    client_id=1234, client_secret="asdf1234", code=code
)
access_token = token_response["access_token"]
refresh_token = token_response["refresh_token"]
expires_at = token_response["expires_at"]

# Now store that short-lived access token somewhere (a database?)
client.access_token = access_token
# You must also store the refresh token to be used later on to obtain another valid access token
# in case the current is already expired
client.refresh_token = refresh_token

# An access_token is only valid for 6 hours, store expires_at somewhere and
# check it before making an API call.
client.token_expires_at = expires_at

athlete = client.get_athlete()
print(
    "For {id}, I now have an access token {token}".format(
        id=athlete.id, token=access_token
    )
)

# ... time passes ...
if time.time() > client.token_expires_at:
    refresh_response = client.refresh_access_token(
        client_id=1234, client_secret="asdf1234", refresh_token=client.refresh_token
    )
    access_token = refresh_response["access_token"]
    refresh_token = refresh_response["refresh_token"]
    expires_at = refresh_response["expires_at"]

运动员和活动

(这是您能做的事情的预览。)

# Currently-authenticated (based on provided token) athlete
# Will have maximum detail exposed (resource_state=3)
curr_athlete = client.get_athlete()

# Fetch another athlete
other_athlete = client.get_athlete(123)
# Will only have summary-level attributes exposed (resource_state=2)

# Get an activity
activity = client.get_activity(123)
# If activity is owned by current user, will have full detail (resource_state=3)
# otherwise summary-level detail.

流表示上传文件的原始数据。活动、努力和段落都有流。有许多类型的流,如果活动没有请求的流类型,则返回的集合将不包括它。

# Activities can have many streams, you can request n desired stream types
types = [
    "time",
    "latlng",
    "altitude",
    "heartrate",
    "temp",
]

streams = client.get_activity_streams(123, types=types, resolution="medium")

#  Result is a dictionary object.  The dict's key are the stream type.
if "altitude" in streams.keys():
    print(streams["altitude"].data)

处理单位

stravalib使用python Pint库来简化处理API中具有相关单位(例如距离、速度)的值。您可以直接使用pint库或通过stravalib.unithelper模块进行快捷操作。

activity = client.get_activity(96089609)
assert isinstance(activity.distance, unithelper.Quantity)
print(activity.distance)
# 22530.80 m

# Meters!?

from stravalib import unithelper

print(unithelper.miles(activity.distance))
# 14.00 mi

# And to get the number:
num_value = float(unithelper.miles(activity.distance))
# Or:
num_value = unithelper.miles(activity.distance).num

还在阅读吗?

已发布的 Sphinx 文档提供了更多信息。[查看 Sphinx 文档]

项目详情


下载文件

下载适合您平台的文件。如果您不确定该选择哪个,请了解有关安装包的更多信息。

源代码分发

stravalib-2.0.tar.gz (226.9 kB 查看哈希值)

上传时间 源代码

构建分发

stravalib-2.0-py3-none-any.whl (118.6 kB 查看哈希值)

上传时间 Python 3

支持者