Private
Public Access
1
0
Files
rowsandall/garminscript.py
2021-05-16 07:30:56 +02:00

190 lines
5.0 KiB
Python

from requests_oauthlib import OAuth1Session, OAuth1
import hmac
import hashlib
import arrow
import datetime
import json
import requests
from urllib.parse import quote_plus as rawurlencode
import logging
import uuid
#logging.basicConfig()
#logging.getLogger().setLevel(logging.DEBUG)
#requests_log = logging.getLogger("requests.packages.urllib3")
#requests_log.setLevel(logging.DEBUG)
#requests_log.propagate = True
payload = {
'workoutName': '30min at threshold',
'sport': 'GENERIC',
'description': 'Uploaded from Rowsandall.com',
'estimatedDurationInSecs': 1800,
'estimatedDistanceInMeters': 6936,
'workoutProvider': 'Rowsandall.com',
'workoutSourceId': 'Rowsandall.com',
'steps': [
{
'type': 'WorkoutStep',
'stepOrder': 0,
#'repeatType': 'Step',
#'repeatValue': 1,
'intensity': 'ACTIVE',
'description': 'At 220W',
'durationType': 'TIME',
'durationValue': 1800,
'durationValueType': None,
'targetType': 'POWER',
'targetValue': 226,
'targetValueLow': None,
'targetValueHigh': None,
}
]
}
payload2 = {
"workoutName": "Test",
"description": "Test",
"sport": "CYCLING",
"steps": [
{
"type": "WorkoutStep",
"stepOrder": 1,
"intensity": "INTERVAL",
"description": "Free Ride",
"durationType": "TIME",
"durationValue": 1800,
"durationValueType": None,
"targetType": "POWER",
"targetValue": 226,
"targetValueLow": None,
"targetValueHigh": None,
"targetValueType": None,
"exerciseCategory": None
}]}
payload = {'workoutName': '4x1000m',
'sport': 'GENERIC',
'description': 'Uploaded from Rowsandall.com',
'estimatedDurationInSecs': 2700,
'estimatedDistanceInMeters': 8768,
'workoutProvider': 'Rowsandall.com',
'workoutSourceId': 'Rowsandall.com',
'steps': [{'type': 'WorkoutStep',
'stepOrder': 0,
'repeatType': None,
'repeatValue': 1,
'intensity': 'INTERVAL',
'description': '0',
'durationType': 'DISTANCE',
'durationValue': 2000,
'durationValueType': 'METER',
'targetType': None,
'targetValue': None,
'targetValueLow': None,
'targetValueHigh': None},
{'type': 'WorkoutRepeatStep',
'stepOrder': 1,
'repeatType': 'REPEAT_UNTIL_STEPS_CMPLT',
'repeatValue': 4,
'intensity': 'INTERVAL',
'description': '3',
'durationType': 'REPS',
'durationValue': None,
'durationValueType': None,
'targetType': None,
'targetValue': None,
'targetValueLow': None,
'targetValueHigh': None,
'steps': [{'type': 'WorkoutStep',
'stepOrder': 2,
'repeatType': None,
'repeatValue': 1,
'intensity': 'INTERVAL',
'description': '1',
'durationType': 'DISTANCE',
'durationValue': 1000,
'durationValueType': 'METER',
#'targetType': 'CADENCE',
#'targetValue': 25,
#'targetValueLow': None,
#'targetValueHigh': None
},
{'type': 'WorkoutStep',
'stepOrder': 3,
'repeatType': None,
'repeatValue': 1,
'intensity': 'REST',
'description': '2',
'durationType': 'TIME',
'durationValue': 60,
'durationValueType': None,
'targetType': None,
'targetValue': None,
'targetValueLow': None,
'targetValueHigh': None}]},
{'type': 'WorkoutStep',
'stepOrder': 4,
'repeatType': None,
'repeatValue': 1,
'intensity': 'INTERVAL',
'description': '4',
'durationType': 'DISTANCE',
'durationValue': 2000,
'durationValueType': 'METER',
'targetType': None,
'targetValue': None,
'targetValueLow': None,
'targetValueHigh': None}]}
print(json.dumps(payload))
oauth_consumer_key = 'ca29ba5e-6868-4468-987d-4ee60a1f04bf'
oauth_consumer_secret = 'SKRqjML9mOBV7BcPpN7LsbuDNDtvLOvRiyo'
oauth_token = '79454eab-bf82-4329-9de2-82a6bd911498'
oauth_token_secret = 'DihdHJ2ThEdbsyoStpPTEmYh5F52L697HhD'
oauth_token='673806b7-aa7b-4064-8290-2dd1b0236ae6'
oauth_token_secret='Ci0m4nKLs4Cfpnw856s2BKQRlDJa6nxdmX5'
authheaders = OAuth1(client_key=oauth_consumer_key,
client_secret=oauth_consumer_secret,
resource_owner_key=oauth_token,
resource_owner_secret=oauth_token_secret,
signature_method='HMAC-SHA1',
#encoding='base64'
)
url = 'https://apis.garmin.com/training-api/workout'
response = requests.post(url,auth=authheaders,json=payload)
#therequest = requests.prepare_request()
# build base_string
print(response.status_code)
print(response.json())
garmin_workout_id = response.json()['workoutId']
url = 'http://apis.garmin.com/training-api/schedule'
payload = {
'workoutId': garmin_workout_id,
'date': '2021-05-16'
}
response = requests.post(url,auth=authheaders,json=payload)
print(response.status_code)
print(response.json())