Fixed Problems with reading the streams and activities
Inserted a request delay to only use 30% of allowed requests
This commit is contained in:
+39
-17
@@ -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?"
|
||||||
String requestUrlExtension = "activities/" + id + "/streams?keys=[time, distance, latlng, altitude, "
|
+ "keys=[time,distance,latlng,altitude,velocity_smooth,heartrate,"
|
||||||
+ "velocity_smooth, heartrate, cadence, watts, temp, moving, grade_smooth]&key_by_type=";
|
+ "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;
|
JSONObject listOfStreams = (JSONObject) obj;
|
||||||
for (int i = 0; i < listOfStreams.size(); i++)
|
for (Object key : listOfStreams.keySet())
|
||||||
{
|
{
|
||||||
JSONObject oneStream = (JSONObject) listOfStreams.get(i);
|
JSONObject oneStream = (JSONObject) listOfStreams.get(key);
|
||||||
data.put("stream_" + oneStream.get(type).toString(), oneStream);
|
data.put("stream_" + key.toString(), oneStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (ParseException | NumberFormatException e)
|
catch (ParseException | NumberFormatException e)
|
||||||
@@ -170,10 +178,10 @@ 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();
|
||||||
@@ -322,9 +341,13 @@ 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 )
|
||||||
{
|
{
|
||||||
writeError("Wrong response code: " + responseCode);
|
//excluded error messages appearing on missing streams
|
||||||
|
if(responseCode != HttpURLConnection.HTTP_NOT_FOUND)
|
||||||
|
{
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
@@ -433,7 +454,8 @@ public class Main
|
|||||||
Tuple withAccessToken = getAccessToken(oneUser);
|
Tuple withAccessToken = getAccessToken(oneUser);
|
||||||
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()));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user