Browse Source

Fixed Problems with reading the streams and activities

Inserted a request delay to only use 30% of allowed requests
master
Bianca Steffes 3 years ago
parent
commit
ae6e61dcd6
  1. 48
      export/src/export/Main.java

48
export/src/export/Main.java

@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -37,6 +38,10 @@ public class Main
private static JSONParser parser = new JSONParser(); private static JSONParser parser = new JSONParser();
private static String testRequest; private static String testRequest;
private static File errorFile; private static File errorFile;
//TODO: enduco needs to insert the correct request limit for 15 Minutes here
private static final int requestLimit15Minutes = 100;
private static int waitTimeMil = 60000*3*15/requestLimit15Minutes;
private static long lastRequest=0;
private static void writeError(String text) private static void writeError(String text)
{ {
@ -119,21 +124,24 @@ public class Main
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static JSONObject addStreams(String id, JSONObject data, String token) static JSONObject addStreams(String id, JSONObject data, String token)
{ {
// TODO: Array representation is not tested
String requestUrlExtension = "activities/" + id + "/streams?keys=[time, distance, latlng, altitude, "
+ "velocity_smooth, heartrate, cadence, watts, temp, moving, grade_smooth]&key_by_type=";
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 = makeOneGetRequest(requestUrlExtension, token); String json = makeOneGetRequest(requestUrlExtension, token);
String type = "type";
if (json.isEmpty()||json.isBlank()||json.equals(""))
{
return data;
}
Object obj; Object obj;
try try
{ {
obj = parser.parse(json); obj = parser.parse(json);
JSONArray listOfStreams = (JSONArray) obj;
for (int i = 0; i < listOfStreams.size(); i++)
JSONObject listOfStreams = (JSONObject) obj;
for (Object key : listOfStreams.keySet())
{ {
JSONObject oneStream = (JSONObject) listOfStreams.get(i);
data.put("stream_" + oneStream.get(type).toString(), oneStream);
JSONObject oneStream = (JSONObject) listOfStreams.get(key);
data.put("stream_" + key.toString(), oneStream);
} }
} }
catch (ParseException | NumberFormatException e) catch (ParseException | NumberFormatException e)
@ -171,9 +179,9 @@ public class Main
int pageIndex = 1; int pageIndex = 1;
while (true) while (true)
{ {
String requestExtension = "athlete/activities?before=&after=&page=" + pageIndex + "&per_page=";
String requestExtension = "athlete/activities?page=" + pageIndex;
String json = makeOneGetRequest(requestExtension, token); String json = makeOneGetRequest(requestExtension, token);
if (json.isEmpty() || json.isBlank() || json.equals("")) // don't know where the last page is...
if (json.isEmpty() || json.isBlank() || json.equals("") || json.equals("[]")) // don't know where the last page is...
{ {
break; break;
} }
@ -286,7 +294,6 @@ public class Main
*/ */
static String makeOneGetRequest(String requestUrlExtension, String token) static String makeOneGetRequest(String requestUrlExtension, String token)
{ {
// TODO: http requests are not tested
if (testRequest != null) if (testRequest != null)
{ {
String varTestRequest = testRequest.replaceAll("\\:\\s*([0-9]{15,})\\,", ":\"$1\","); String varTestRequest = testRequest.replaceAll("\\:\\s*([0-9]{15,})\\,", ":\"$1\",");
@ -296,6 +303,18 @@ public class Main
HttpsURLConnection connection = null; HttpsURLConnection connection = null;
try try
{ {
long timeSinceLastRequest =System.currentTimeMillis()-lastRequest;
if( timeSinceLastRequest<waitTimeMil)
{
try
{
TimeUnit.MILLISECONDS.sleep(waitTimeMil-timeSinceLastRequest);
}
catch (InterruptedException e)
{
}
};
lastRequest=System.currentTimeMillis();
// Create connection // Create connection
URL url = new URL(baseUrl + requestUrlExtension); URL url = new URL(baseUrl + requestUrlExtension);
connection = (HttpsURLConnection) url.openConnection(); connection = (HttpsURLConnection) url.openConnection();
@ -323,8 +342,12 @@ public class Main
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
int responseCode = connection.getResponseCode(); int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK ) if (responseCode != HttpURLConnection.HTTP_OK )
{
//excluded error messages appearing on missing streams
if(responseCode != HttpURLConnection.HTTP_NOT_FOUND)
{ {
writeError("Wrong response code: " + responseCode); writeError("Wrong response code: " + responseCode);
}
return ""; return "";
} }
@ -352,7 +375,6 @@ public class Main
*/ */
static String makeOnePostRequest(Triplet data) static String makeOnePostRequest(Triplet data)
{ {
// TODO: http requests are not tested
HttpsURLConnection connection = null; HttpsURLConnection connection = null;
try try
{ {
@ -425,7 +447,6 @@ public class Main
// the requests // the requests
List<Triplet> newRefreshTokens = new ArrayList<>(); List<Triplet> newRefreshTokens = new ArrayList<>();
// TODO: Should we expect problems with memory??
Map<String, File> allFiles = new HashMap<>(); Map<String, File> allFiles = new HashMap<>();
for (Triplet oneUser : refreshTokens) for (Triplet oneUser : refreshTokens)
{ {
@ -434,6 +455,7 @@ public class Main
if (withAccessToken == null) if (withAccessToken == null)
{ {
writeError("Coulnd't get new access token for client "+athleteId); writeError("Coulnd't get new access token for client "+athleteId);
continue;
} }
newRefreshTokens.add(new Triplet(oneUser.getA(), oneUser.getB(), withAccessToken.getB())); newRefreshTokens.add(new Triplet(oneUser.getA(), oneUser.getB(), withAccessToken.getB()));

Loading…
Cancel
Save