-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.py
executable file
·140 lines (106 loc) · 4.49 KB
/
fix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
import time
import sys
import json
from selenium import webdriver
import re
from datetime import datetime
from geopy.geocoders import Nominatim
import requests
from stravaauth import get_tokens, get_account, site_login
#from selenium.webdriver.firefox.options import Options as FirefoxOptions
#options = FirefoxOptions()
#options.add_argument("--headless")
def get_activities_from_file(af):
with open(af) as tokenFile:
activities = json.load(tokenFile)
return activities
def correct_elevation(driver,act_id):
activity_url = 'https://www.strava.com/activities/{}'.format(act_id)
driver.get(activity_url)
driver.find_element_by_xpath('//*[@id="elevation-adjusted-help"]').click()
driver.find_element_by_xpath('/html/body/div[6]/div[1]/a').click()
time.sleep(4)
def elevation_check(activity,thresh=0.01):
elevation = activity['total_elevation_gain']
distance = activity['distance']
if distance == 0:
return -1
ratio = float(elevation) / float(distance)
if ratio > thresh:
print(activity['id'], activity['name'], elevation, distance, ratio)
return activity['id']
else:
return -1
def elevation_fix(driver,activities,thresh=0.01):
site_login(driver)
for activity in activities:
try:
atype = activity['workout_type']
result = elevation_check(activity,thresh)
if result > 0 and (atype is None or atype == 0):
correct_elevation(driver,activity['id'])
continue
except:
continue
def name_fix(activities,tokens):
app = Nominatim(user_agent="strava-fix")
for activity in activities:
name = activity['name']
# convert distance from meters to miles
distance = activity['distance']/1609.344
if re.match(r'.*?(Morning|Lunch|Afternoon|Evening|Night) Run.*', name): #name == 'Morning Run':
if activity['start_latlng'] is None:
continue
dt = datetime.strptime(activity['start_date'], '%Y-%m-%dT%H:%M:%SZ')
print(name, dt)
try:
(lat, lon) = activity['start_latlng']
except ValueError as e:
(lat, lon) = (0,0)
##################
# Reverse Geocode (TODO: Update this library.)
coordinates = f"{lat}, {lon}"
try:
location = app.reverse(coordinates, language='en').raw
#print(location)
try:
aname = '{}/{}/{} {}, {} ({}) - {:4.2f} miles'.format( dt.month, dt.day, dt.year,
location['address']['city'], location['address']['state'],
location['address']['municipality'], distance)
except KeyError as e:
try:
aname = '{}/{}/{} {}, {} - {:4.2f} miles'.format( dt.month, dt.day, dt.year,
location['address']['city'], location['address']['state'], distance)
except KeyError as e:
aname = '{}/{}/{} Unknown - {:4.2f} miles'.format( dt.month, dt.day, dt.year, distance)
except:
aname = '{}/{}/{} {}, {} - {:4.2f} miles'.format( dt.month, dt.day, dt.year,
'Chicago', 'Illinois', distance)
#################
print(aname)
activity['name'] = aname
print("Updating {}".format(activity['id']))
#print(activity)
access_token = tokens['access_token']
headers = {'Authorization': 'Bearer ' + access_token, "Content-Type": "application/json"}
url = 'https://www.strava.com/api/v3/activities/{}'.format(activity['id'])
activity_json = json.dumps(activity)
response = requests.put(url, data=activity_json, headers=headers)
print(response.content)
time.sleep(1)
if __name__ == '__main__':
###############################
# Get Auth Tokens from File
# Refresh if needed
tokens = get_tokens()
###############################
# Get Activities from JSON file
activities = get_activities_from_file('data/activities0.json')
###############################
# Activity Name Fix
name_fix(activities,tokens)
###############################
# Fix the Elevations - Chicago is Flat, so we look for a pretty low threshold
driver = webdriver.Firefox()
elevation_fix(driver,activities)