Last active
April 2, 2016 19:38
-
-
Save mbjd/9b540ffee3ed2d09845f to your computer and use it in GitHub Desktop.
Display some numbers about how much cycling you've done this year and how you need to adjust to reach a certain goal. Pulls data from a strava profile.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# This script parses a strava athlete page to get the amount of bicycling kilometers this year | |
# to date from strava, and calculates some statistics from that (km/day and km/week to date and | |
# necessary to hit a target, and km to go / km already done). | |
# | |
# usage: $ ./kmtogo.py [target] | |
# If no target is specified, it'll take the default value (2000). | |
# | |
import sys | |
import time | |
import json | |
import urllib | |
import requests | |
import datetime | |
from pprint import pprint | |
from bs4 import BeautifulSoup | |
ATHLETE = 1201380 # Replace this with your athlete id | |
TARGET = 2000 # km | |
try: | |
TARGET = int(sys.argv[1]) | |
except: | |
pass | |
def getkm(url): | |
try: | |
html = requests.get(url).text | |
except Exception, e: | |
print("Network error!") | |
sys.exit() | |
soup = BeautifulSoup(html, 'html.parser') | |
table = soup.find_all('td') | |
return float(table[0].contents[0].replace(',', '')) | |
def daysRemaining(): | |
now = time.time() | |
newYear = time.mktime(datetime.date(datetime.date.today().year + 1, 1, 1).timetuple()) | |
delta = newYear - now | |
return delta / 86400 | |
def daysElapsed(): | |
now = time.time() | |
yearStart = time.mktime(datetime.date(datetime.date.today().year, 1, 1).timetuple()) | |
delta = now - yearStart | |
return delta / 86400 | |
url = 'http://strava.com/athletes/{}'.format(ATHLETE) | |
kmToDate = getkm(url) | |
perDayToDate = (kmToDate / daysElapsed()) | |
perDayToGo = ((TARGET - kmToDate) / daysRemaining()) | |
print('YTD: {:5.2f} km/d, {:6.2f} km/w -> {:6.1f} km total up until now'.format(perDayToDate, perDayToDate * 7, kmToDate)) | |
print('To go: {:5.2f} km/d, {:6.2f} km/w -> {:6.1f} km to reach {} km'.format(perDayToGo, perDayToGo * 7, TARGET - kmToDate, TARGET)) | |
print('You\'ll have to cycle {:.6f} times more than until now'.format(perDayToGo / perDayToDate)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment