-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetData.py
77 lines (73 loc) · 2.68 KB
/
getData.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
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Changed by user on installation
bot_directory = "C:\\Users\\Jed\\Documents\\outrun_website\\"
client_id = ""
client_secret = ''
# Constants
auth_url = "https://www.strava.com/oauth/token"
activites_url = "https://www.strava.com/api/v3/athlete/activities"
def getRefreshTokens(username,code): # Gets the refresh tokens needed to get information from the strava account
# Also stores in a file for later use
# Payload is needed to send API command
refresh_payload = {
'client_id': client_id,
'client_secret': client_secret,
'code': code,
'grant_type': "authorization_code",
'f': 'json'
}
print("Requesting Refresh Token...")
try: # code will only work once so possibility of failing
res1 = requests.post(auth_url, data=refresh_payload, verify=False) #API request
refresh_token = res1.json()['refresh_token'] # Gets data
print("Refresh Token = {}\n".format(refresh_token))
# Writes it to storage
f = open(bot_directory+"users.txt", "a+")
f.write(username+" ; "+str(refresh_token)+"\n")
f.close()
return refresh_token # Returns the token
except:
print("An error has occurred. Please try again")
print(res1) # Prints error
print()
return
def getActivityList(refresh_token):
# get access token
access_payload = {
'client_id': client_id,
'client_secret': client_secret,
'refresh_token': refresh_token,#''
'grant_type': "refresh_token",
'f': 'json'
}
print("Requesting Token...\n")
try:
res2 = requests.post(auth_url,data=access_payload, verify=False)
access_token = res2.json()['access_token']
print("Access Token = {}\n".format(access_token))
header = {'Authorization': 'Bearer ' + access_token}
param = {'per_page': 200, 'page': 1}
activityList = requests.get(activites_url, headers=header, params=param).json()
return activityList
except:
print("An error has occurred. Please try again")
print(res2)
print()
return
# Reads the file to get users
f=open(bot_directory+"users.txt", "r")
contents =f.read()
users = contents.split('\n')
names = []
for name in users:
names.append(name.split(' ; '))
try:
names.pop()
print(names)
for i in range(len(names)):
my_dataset = getActivityList(names[i][1])# gets the activity list of one person
print(my_dataset)
except:
print("An error has occurred. Check number of users")