{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d104368a",
   "metadata": {},
   "source": [
    "# Loading your Strava running activities\n",
    "\n",
    "Many runners use third-party apps to track running activities. Runpandas supports loading activities directly from Strava Running Web App. In this notebook, we will extract this running data and analyse it locally using runpandas. We also take the opportunity to illustrate the runpandas methods for fetching and parsing data from Strava."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f0ee1ef0",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "import runpandas\n",
    "import os\n",
    "import pandas as pd\n",
    "pd.set_option('display.max_rows', 500)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e03adfbc",
   "metadata": {},
   "source": [
    "We load the environment variables from our terminal or use a `.env` file with the STRAVA social app personal access tokens. You can create your own using the link instructions here: https://developers.strava.com/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "360f8407",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from dotenv import load_dotenv\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e287d2a0",
   "metadata": {},
   "source": [
    "Firstly, import the runpandas ``StravaClient``, create a Client instance, and read in the client ID and secret loaded previously. Next the one time authentication. The command ``client.authenticate_web``  will open a browser with a URL for the athlete to use to approve access to their data from the app. \n",
    "\n",
    "The athlete is then prompted to log in to the Strava website and give consent to the requesting application. Once the user authorizes,  it will store the access token and refresh token. From this point this access token, which lasts for 6 hours, will be what you need to access data. The client also save it locally so it can be re-read and refreshed as needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "346f6024",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = runpandas.StravaClient()\n",
    "client.authenticate_web()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30d9c24e",
   "metadata": {},
   "source": [
    "Now we can start to look at our athlete’s activities; at the example below we request the activity with the a specified id.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "32938533",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Unable to set attribute media_type on entity <ActivityPhotoPrimary id=None>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Session                           Running: 18-06-2022 07:08:07\n",
       "Total distance (meters)                                21389.8\n",
       "Total ellapsed time                            0 days 02:02:20\n",
       "Total moving time                              0 days 02:02:19\n",
       "Average speed (km/h)                                       NaN\n",
       "Average moving speed (km/h)                                NaN\n",
       "Average pace (per 1 km)                                    NaN\n",
       "Average pace moving (per 1 km)                             NaN\n",
       "Average cadence                                        87.7889\n",
       "Average moving cadence                                  87.847\n",
       "Average heart rate                                     155.674\n",
       "Average moving heart rate                              155.713\n",
       "Average temperature                                        NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "activity = runpandas.read_strava('7329257123')\n",
    "activity.summary()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "2a188acd0f27a53b17cfad69c436eac3f19ae51e9e26340e7d32ca2c8c278930"
  },
  "kernelspec": {
   "display_name": "Python 3.8.3 ('runpandas_dev')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
