|
|
@ -49,6 +49,11 @@ public class Main |
|
|
|
private static int dailyRequestCount = 0; |
|
|
|
private static int waitTimeMil = 60000 * 3 * 15 / requestLimit15Minutes; |
|
|
|
private static long lastRequestTimeInMillis = 0; |
|
|
|
private static String accessToken = ""; |
|
|
|
/** |
|
|
|
* a is client_id, b is client_secret, c is refresh_token |
|
|
|
*/ |
|
|
|
private static Triplet refreshInfo; |
|
|
|
|
|
|
|
private static void writeError(String text) |
|
|
|
{ |
|
|
@ -80,32 +85,50 @@ public class Main |
|
|
|
/** |
|
|
|
* Saves the data of one athlete into a temp file and returns it. |
|
|
|
* |
|
|
|
* @param token Identifier / authorization token of the athlete |
|
|
|
* @return created temp file |
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
static File oneAthlete(String token) |
|
|
|
static File oneAthlete() |
|
|
|
{ |
|
|
|
// get Activities and general information |
|
|
|
Map<String, JSONObject> activities; |
|
|
|
JSONObject athleteInfo; |
|
|
|
try |
|
|
|
{ |
|
|
|
activities = getActivities(); |
|
|
|
athleteInfo = saveGeneralInformation(); |
|
|
|
} |
|
|
|
catch (NoAccessException e1) |
|
|
|
{ |
|
|
|
// get Activities |
|
|
|
Map<String, JSONObject> activities = getActivities(token); |
|
|
|
writeError("Athlete " + athleteId + ": Access expired and no new token possible"); |
|
|
|
return null; // no data at all. Stop right away |
|
|
|
} |
|
|
|
if (athleteInfo == null) // error getting General Information |
|
|
|
{ |
|
|
|
athleteInfo = new JSONObject(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// for each activity: save streams |
|
|
|
JSONArray allActivities = new JSONArray(); |
|
|
|
int simpleActivityId = 0; |
|
|
|
for (String id : activities.keySet()) |
|
|
|
{ |
|
|
|
JSONObject data = addStreams(id, activities.get(id), token); |
|
|
|
JSONObject data; |
|
|
|
try |
|
|
|
{ |
|
|
|
data = addStreams(id, activities.get(id)); |
|
|
|
} |
|
|
|
catch (NoAccessException e) |
|
|
|
{ |
|
|
|
writeError("Athlete " + athleteId + ": Access expired and no new token possible"); |
|
|
|
break; //stop the loop and save what you got up to there |
|
|
|
} |
|
|
|
data.put("activity_id", simpleActivityId); |
|
|
|
allActivities.add(data); |
|
|
|
simpleActivityId++; |
|
|
|
} |
|
|
|
|
|
|
|
// get general information |
|
|
|
JSONObject athleteInfo = saveGeneralInformation(token); |
|
|
|
if(athleteInfo==null) //error getting General Information |
|
|
|
{ |
|
|
|
athleteInfo = new JSONObject(); |
|
|
|
} |
|
|
|
athleteInfo.put("activities", allActivities); |
|
|
|
|
|
|
|
try |
|
|
@ -129,17 +152,17 @@ public class Main |
|
|
|
* |
|
|
|
* @param id Strava id of the activity |
|
|
|
* @param data general information of the activity |
|
|
|
* @param token Identifier / authorization token of the athlete |
|
|
|
* @return The data with the added streams |
|
|
|
* @throws NoAccessException If the access token expired and no new one could be acquired. |
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
static JSONObject addStreams(String id, JSONObject data, String token) |
|
|
|
static JSONObject addStreams(String id, JSONObject data) throws NoAccessException |
|
|
|
{ |
|
|
|
String requestUrlExtension = "activities/" + id + "/streams?" |
|
|
|
+ "keys=[time,distance,latlng,altitude,velocity_smooth,heartrate," |
|
|
|
+ "cadence,watts,temp,moving,grade_smooth]&key_by_type=true"; |
|
|
|
|
|
|
|
String json = makeGetRequestWithRetry(requestUrlExtension, token);; |
|
|
|
String json = makeGetRequestWithRetry(requestUrlExtension); |
|
|
|
|
|
|
|
if (json == null || json.isEmpty() || json.isBlank() || json.equals("")) |
|
|
|
{ |
|
|
@ -167,12 +190,12 @@ public class Main |
|
|
|
* Gathers all activities of a user, extracts the general information and the |
|
|
|
* ids. |
|
|
|
* |
|
|
|
* @param token Identifier / authorization token of the athlete |
|
|
|
* @return A Map with key = Strava Id of an activity and value = JSONObject with |
|
|
|
* the general information of the activity |
|
|
|
* @throws NoAccessException If the access token expired and no new one could be acquired. |
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
static Map<String, JSONObject> getActivities(String token) |
|
|
|
static Map<String, JSONObject> getActivities() throws NoAccessException |
|
|
|
{ |
|
|
|
Map<String, JSONObject> result = new HashMap<>(); |
|
|
|
/* |
|
|
@ -191,10 +214,12 @@ public class Main |
|
|
|
int pageIndex = 1; |
|
|
|
while (true) |
|
|
|
{ |
|
|
|
//TODO: this 'per_page' value could perhaps be the root of all evil |
|
|
|
String requestExtension = "athlete/activities?per_page=100&page=" + pageIndex; |
|
|
|
String json = makeGetRequestWithRetry(requestExtension, token); |
|
|
|
if (json==null || json.isEmpty() || json.isBlank() || json.equals("") || json.equals("[]")) // don't know where the last page is... |
|
|
|
String json = makeGetRequestWithRetry(requestExtension); |
|
|
|
if (json == null || json.isEmpty() || json.isBlank() || json.equals("") || json.equals("[]")) // don't know |
|
|
|
// where the |
|
|
|
// last page |
|
|
|
// is... |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
@ -231,7 +256,8 @@ public class Main |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* In the case that the daily request limit has been reached, this method waits for the next day |
|
|
|
* In the case that the daily request limit has been reached, this method waits |
|
|
|
* for the next day |
|
|
|
*/ |
|
|
|
static void checkRequestLimit() |
|
|
|
{ |
|
|
@ -257,13 +283,15 @@ public class Main |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Method is used to find the next request window: It tries the same request again after 5 minutes. |
|
|
|
* After a set number of times (retryTimes) it stops if it still wasn't successful. |
|
|
|
* Method is used to find the next request window: It tries the same request |
|
|
|
* again after 5 minutes. After a set number of times (retryTimes) it stops if |
|
|
|
* it still wasn't successful. |
|
|
|
* |
|
|
|
* @param urlExtension UrlExtension for the request |
|
|
|
* @param token Token for the request |
|
|
|
* @return Data as a String or {@code null} if there is no data |
|
|
|
* @throws NoAccessException If the access token expired and no new one could be acquired. |
|
|
|
*/ |
|
|
|
static String makeGetRequestWithRetry(String urlExtension, String token) |
|
|
|
static String makeGetRequestWithRetry(String urlExtension) throws NoAccessException |
|
|
|
{ |
|
|
|
checkRequestLimit(); |
|
|
|
String json = null; |
|
|
@ -272,7 +300,7 @@ public class Main |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
json = makeOneGetRequest(urlExtension, token); |
|
|
|
json = makeOneGetRequest(urlExtension); |
|
|
|
dailyRequestCount++; |
|
|
|
} |
|
|
|
catch (ResponseCodeWrongException e) |
|
|
@ -280,12 +308,19 @@ public class Main |
|
|
|
// tried enough times, so stop now |
|
|
|
if (count >= retryTimes) |
|
|
|
{ |
|
|
|
writeError("Athlete: "+athleteId+" Retry limit reached. Last error code: "+e.getResponseCode()); |
|
|
|
writeError( |
|
|
|
"Athlete: " + athleteId + " Retry limit reached. Last error code: " + e.getResponseCode()); |
|
|
|
return null; |
|
|
|
} |
|
|
|
//request limit is reached, try again later |
|
|
|
if (e.getResponseCode()==httpCodeLimitReached) |
|
|
|
if (e.getResponseCode()==HttpURLConnection.HTTP_UNAUTHORIZED) |
|
|
|
{ //token might have expired |
|
|
|
if(!getAccessToken()) //token doesn't work anymore and we can't get a new one |
|
|
|
{ |
|
|
|
throw new NoAccessException(); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (e.getResponseCode() == httpCodeLimitReached) |
|
|
|
{// request limit is reached, try again later |
|
|
|
count++; |
|
|
|
} |
|
|
|
else // some other error: try only one other time! |
|
|
@ -309,11 +344,11 @@ public class Main |
|
|
|
/** |
|
|
|
* Extracts an athletes general information. |
|
|
|
* |
|
|
|
* @param token Identifier / authorization token of the athlete |
|
|
|
* @return extracted data or null if there was an error |
|
|
|
* @throws NoAccessException If the access token expired and no new one could be acquired. |
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
static JSONObject saveGeneralInformation(String token) |
|
|
|
static JSONObject saveGeneralInformation() throws NoAccessException |
|
|
|
{ |
|
|
|
String sex = "sex"; // Possible values = M, F |
|
|
|
String country = "country"; |
|
|
@ -321,8 +356,7 @@ public class Main |
|
|
|
String meas_pref = "measurement_preference"; // Possible values = feet, meters |
|
|
|
String weight = "weight"; |
|
|
|
|
|
|
|
|
|
|
|
String json = makeGetRequestWithRetry("athlete", token); |
|
|
|
String json = makeGetRequestWithRetry("athlete"); |
|
|
|
|
|
|
|
JSONObject toSave = new JSONObject(); |
|
|
|
try |
|
|
@ -384,12 +418,10 @@ public class Main |
|
|
|
* Handles one GET request to the API |
|
|
|
* |
|
|
|
* @param requestUrlExtension Extension for the baseUrl (without '/') |
|
|
|
* @param token Identification / authorization token of the |
|
|
|
* athlete |
|
|
|
* @return The response as a String, an empty String in case of error. |
|
|
|
* @throws ResponseCodeWrongException If there was an http error |
|
|
|
*/ |
|
|
|
static String makeOneGetRequest(String requestUrlExtension, String token) throws ResponseCodeWrongException |
|
|
|
static String makeOneGetRequest(String requestUrlExtension) throws ResponseCodeWrongException |
|
|
|
{ |
|
|
|
if (testRequest != null) |
|
|
|
{ |
|
|
@ -410,13 +442,14 @@ public class Main |
|
|
|
catch (InterruptedException e) |
|
|
|
{ |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
; |
|
|
|
lastRequestTimeInMillis = System.currentTimeMillis(); |
|
|
|
// Create connection |
|
|
|
URL url = new URL(baseUrl + requestUrlExtension); |
|
|
|
connection = (HttpsURLConnection) url.openConnection(); |
|
|
|
connection.setRequestMethod("GET"); |
|
|
|
connection.setRequestProperty("Authorization", "Bearer " + token); |
|
|
|
connection.setRequestProperty("Authorization", "Bearer " + accessToken); |
|
|
|
|
|
|
|
return getResponse(connection); |
|
|
|
} |
|
|
@ -433,7 +466,8 @@ public class Main |
|
|
|
* @param connection Connection to the site |
|
|
|
* @return Response as a String |
|
|
|
* @throws IOException in case of error with the stream |
|
|
|
* @throws ResponseCodeWrongException if no data was read because of http problems |
|
|
|
* @throws ResponseCodeWrongException if no data was read because of http |
|
|
|
* problems |
|
|
|
*/ |
|
|
|
static String getResponse(HttpsURLConnection connection) throws IOException, ResponseCodeWrongException |
|
|
|
{ |
|
|
@ -467,12 +501,10 @@ public class Main |
|
|
|
/** |
|
|
|
* Used for generating the accessToken |
|
|
|
* |
|
|
|
* @param data Triplet containing: a the client_id, b the client_secret, c the |
|
|
|
* refresh_token |
|
|
|
* @return The response as a String, an empty String in case of error. |
|
|
|
* @throws ResponseCodeWrongException If there was an http error |
|
|
|
*/ |
|
|
|
static String makeOnePostRequest(Triplet data) throws ResponseCodeWrongException |
|
|
|
static String makeOnePostRequest() throws ResponseCodeWrongException |
|
|
|
{ |
|
|
|
HttpsURLConnection connection = null; |
|
|
|
try |
|
|
@ -484,10 +516,10 @@ public class Main |
|
|
|
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
|
|
|
|
|
|
|
Map<String, String> params = new LinkedHashMap<>(); |
|
|
|
params.put("client_id", data.getA()); |
|
|
|
params.put("client_secret", data.getB()); |
|
|
|
params.put("client_id", refreshInfo.getA()); |
|
|
|
params.put("client_secret", refreshInfo.getB()); |
|
|
|
params.put("grant_type", "refresh_token"); |
|
|
|
params.put("refresh_token", data.getC()); |
|
|
|
params.put("refresh_token", refreshInfo.getC()); |
|
|
|
|
|
|
|
StringBuilder postData = new StringBuilder(); |
|
|
|
for (String key : params.keySet()) |
|
|
@ -516,11 +548,15 @@ public class Main |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Sends the old refresh token to strava and retrieves a new one and an access token |
|
|
|
* @param refreshInfo Refresh data with a the cliend_id, b the client_secret and c the refresh_token |
|
|
|
* @return A tuple with a the access_token and b the new refresh_token or {@code null} if there was an error |
|
|
|
* Sends the old refresh token to strava and retrieves a new one and an access |
|
|
|
* token |
|
|
|
* |
|
|
|
* @param refreshInfo Refresh data with a the cliend_id, b the client_secret and |
|
|
|
* c the refresh_token |
|
|
|
* @return {@code true} if everything went right or {@code false} if there was |
|
|
|
* an error |
|
|
|
*/ |
|
|
|
static Tuple getAccessToken(Triplet refreshInfo) |
|
|
|
static boolean getAccessToken() |
|
|
|
{ |
|
|
|
checkRequestLimit(); |
|
|
|
String json = null; |
|
|
@ -529,7 +565,7 @@ public class Main |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
json = makeOnePostRequest(refreshInfo); |
|
|
|
json = makeOnePostRequest(); |
|
|
|
dailyRequestCount++; |
|
|
|
} |
|
|
|
catch (ResponseCodeWrongException e) |
|
|
@ -537,11 +573,19 @@ public class Main |
|
|
|
// tried enough times, so stop now |
|
|
|
if (count >= retryTimes) |
|
|
|
{ |
|
|
|
return null; |
|
|
|
return false; |
|
|
|
} |
|
|
|
//request limit is reached, try again later |
|
|
|
if (e.getResponseCode()==httpCodeLimitReached) |
|
|
|
|
|
|
|
if (e.getResponseCode()==HttpURLConnection.HTTP_UNAUTHORIZED) |
|
|
|
{ //token might have expired |
|
|
|
if(!getAccessToken()) //token doesn't work anymore and we can't get a new one |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
else if (e.getResponseCode() == httpCodeLimitReached) |
|
|
|
{ |
|
|
|
// request limit is reached, try again later |
|
|
|
count++; |
|
|
|
} |
|
|
|
else // some other error: try only one other time! |
|
|
@ -563,13 +607,15 @@ public class Main |
|
|
|
{ |
|
|
|
Object obj = parser.parse(json); |
|
|
|
JSONObject data = (JSONObject) obj; |
|
|
|
return new Tuple(data.get("access_token").toString(), data.get("refresh_token").toString()); |
|
|
|
accessToken = data.get("access_token").toString(); |
|
|
|
refreshInfo.setC(data.get("refresh_token").toString()); |
|
|
|
return true; |
|
|
|
} |
|
|
|
catch (ParseException e) |
|
|
|
{ |
|
|
|
writeError("Athlete " + athleteId + ": Error parsing general information."); |
|
|
|
writeError("Athlete " + athleteId + ": Error parsing refresh info."); |
|
|
|
} |
|
|
|
return null; |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) |
|
|
@ -586,21 +632,21 @@ public class Main |
|
|
|
int zipcount = 1; |
|
|
|
for (Triplet oneUser : refreshTokens) |
|
|
|
{ |
|
|
|
refreshInfo = oneUser; |
|
|
|
athleteId++; |
|
|
|
// a is access_token and b is new refresh_token |
|
|
|
Tuple withAccessToken = getAccessToken(oneUser); |
|
|
|
if (withAccessToken == null) |
|
|
|
if (!getAccessToken()) |
|
|
|
{ |
|
|
|
writeError("Couldn't get new access token for client " + athleteId); |
|
|
|
continue; |
|
|
|
} |
|
|
|
newRefreshTokens.add(new Triplet(oneUser.getA(), oneUser.getB(), withAccessToken.getB())); |
|
|
|
|
|
|
|
File athlete = oneAthlete(withAccessToken.getA()); |
|
|
|
File athlete = oneAthlete(); |
|
|
|
if (athlete != null) |
|
|
|
{ |
|
|
|
allFiles.put("Athlete_" + athleteId + ".json", athlete); |
|
|
|
} |
|
|
|
newRefreshTokens.add(refreshInfo); |
|
|
|
|
|
|
|
// pack zip-files of 10 athletes |
|
|
|
if (allFiles.size() >= 10) |
|
|
|
{ |
|
|
|