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. 56
      export/src/export/Main.java

56
export/src/export/Main.java

@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -37,6 +38,10 @@ public class Main
private static JSONParser parser = new JSONParser();
private static String testRequest;
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)
{
@ -119,21 +124,24 @@ public class Main
@SuppressWarnings("unchecked")
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 type = "type";
if (json.isEmpty()||json.isBlank()||json.equals(""))
{
return data;
}
Object obj;
try
{
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)
@ -170,10 +178,10 @@ public class Main
int pageIndex = 1;
while (true)
{
String requestExtension = "athlete/activities?before=&after=&page=" + pageIndex + "&per_page=";
{
String requestExtension = "athlete/activities?page=" + pageIndex;
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;
}
@ -286,7 +294,6 @@ public class Main
*/
static String makeOneGetRequest(String requestUrlExtension, String token)
{
// TODO: http requests are not tested
if (testRequest != null)
{
String varTestRequest = testRequest.replaceAll("\\:\\s*([0-9]{15,})\\,", ":\"$1\",");
@ -296,6 +303,18 @@ public class Main
HttpsURLConnection connection = null;
try
{
long timeSinceLastRequest =System.currentTimeMillis()-lastRequest;
if( timeSinceLastRequest<waitTimeMil)
{
try
{
TimeUnit.MILLISECONDS.sleep(waitTimeMil-timeSinceLastRequest);
}
catch (InterruptedException e)
{
}
};
lastRequest=System.currentTimeMillis();
// Create connection
URL url = new URL(baseUrl + requestUrlExtension);
connection = (HttpsURLConnection) url.openConnection();
@ -322,9 +341,13 @@ public class Main
{
StringBuilder result = new StringBuilder();
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK)
if (responseCode != HttpURLConnection.HTTP_OK )
{
writeError("Wrong response code: " + responseCode);
//excluded error messages appearing on missing streams
if(responseCode != HttpURLConnection.HTTP_NOT_FOUND)
{
writeError("Wrong response code: " + responseCode);
}
return "";
}
@ -352,7 +375,6 @@ public class Main
*/
static String makeOnePostRequest(Triplet data)
{
// TODO: http requests are not tested
HttpsURLConnection connection = null;
try
{
@ -425,7 +447,6 @@ public class Main
// the requests
List<Triplet> newRefreshTokens = new ArrayList<>();
// TODO: Should we expect problems with memory??
Map<String, File> allFiles = new HashMap<>();
for (Triplet oneUser : refreshTokens)
{
@ -433,7 +454,8 @@ public class Main
Tuple withAccessToken = getAccessToken(oneUser);
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()));

Loading…
Cancel
Save