diff --git a/export/src/export/Main.java b/export/src/export/Main.java index eea2096..a46f970 100644 --- a/export/src/export/Main.java +++ b/export/src/export/Main.java @@ -23,6 +23,7 @@ import java.util.zip.ZipOutputStream; import javax.net.ssl.HttpsURLConnection; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -33,18 +34,160 @@ public class Main private static int athleteId = 0; private static JSONParser parser = new JSONParser(); - private static void oneAthlete(String token) + /** + * + * @param token + */ + @SuppressWarnings("unchecked") + static void oneAthlete(String token) { - HashMap athletesTempFiles = new HashMap(); + Map athletesTempFiles = new HashMap(); // get general information athletesTempFiles.put("general_information.json", saveGeneralInformation(token)); - int activityId=0; - // get Activities + Map activities = getActivities(token); // for each activity: save streams + int simpleActivityId=0; + for (String id: activities.keySet()) + { + JSONObject data = addStreams(id, activities.get(id), token); + data.put("activity_id", simpleActivityId); + + try + { + File temp = File.createTempFile("activity_" +simpleActivityId , ".json"); + temp.deleteOnExit(); + BufferedWriter bw = new BufferedWriter(new FileWriter(temp)); + bw.write(data.toString()); + bw.close(); + athletesTempFiles.put(""+simpleActivityId, temp); + } + catch (IOException e) + { + //Temp File coldnt be created + // TODO Auto-generated catch block + e.printStackTrace(); + } + + simpleActivityId++; + } + + try + { + zipAllFiles(athletesTempFiles); + } + catch (IOException e) + { + //Problem zipping + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** + * Adds the streams to the given activity + * @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 + */ + @SuppressWarnings("unchecked") + static JSONObject addStreams(String id, JSONObject data, String token) + { + //TODO: das könnte mit dem Array Probleme geben.... + String requestUrlExtension = "activities/"+id+"/streams?keys=[time, distance, latlng, altitude, " + + "velocity_smooth, heartrate, cadence, watts, temp, moving, grade_smooth]&key_by_type="; + String json = makeOneRequest(requestUrlExtension, token); + String type = "type"; + + Object obj; + try + { + obj = parser.parse(json); + JSONArray listOfStreams = (JSONArray) obj; + for (int i=0; i getActivities(String token) + { + Map result = new HashMap<>(); + /* + * Possible values = AlpineSki, BackcountrySki, Canoeing, Crossfit, EBikeRide, Elliptical, Golf, + * Handcycle, Hike, IceSkate, InlineSkate, Kayaking, Kitesurf, NordicSki, Ride, RockClimbing, + * RollerSki, Rowing, Run, Sail, Skateboard, Snowboard, Snowshoe, Soccer, StairStepper, + * StandUpPaddling, Surfing, Swim, Velomobile, VirtualRide, VirtualRun, Walk, WeightTraining, + * Wheelchair, Windsurf, Workout, Yoga + */ + String type = "type"; + String timezone = "timezone"; + String start = "start_date_local"; //Start date measured in users time zone + String id = "id"; + + int pageIndex = 1; + while(true) + { + String requestExtension = "athlete/activities?before=&after=&page="+pageIndex+"&per_page="; + String json = makeOneRequest(requestExtension, token); + if (json.isEmpty()||json.isBlank()||json.equals("")) //don't know where the last page is... + { + break; + } + + Object obj; + try + { + obj = parser.parse(json); + JSONArray listOfActivites = (JSONArray) obj; + for (int i=0; i which should be zipped * @throws IOException If there was an error zipping */ - private static void zipAllFiles(Map files) throws IOException + static void zipAllFiles(Map files) throws IOException { //TODO: checken, ob hier an den richtigen Ort gespeichert wird. //TODO: evtl. alles nochmal zippen @@ -135,7 +279,7 @@ public class Main * @param token Identification / authorization token of the athlete * @return The response as a String, an empty String in case of error. */ - private static String makeOneRequest(String requestUrlExtension, String token) + static String makeOneRequest(String requestUrlExtension, String token) { HttpURLConnection connection = null; StringBuilder result = new StringBuilder(); @@ -187,7 +331,7 @@ public class Main oneAthlete(token); athleteId++; } - + //TODO: alles nochmal zippen } } diff --git a/export/src/export/MainTest.java b/export/src/export/MainTest.java new file mode 100644 index 0000000..ca1fd81 --- /dev/null +++ b/export/src/export/MainTest.java @@ -0,0 +1,52 @@ +package export; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MainTest +{ + + @Test + public void testOneAthlete() + { + fail("Not yet implemented"); + } + + @Test + public void testAddStreams() + { + fail("Not yet implemented"); + } + + @Test + public void testGetActivities() + { + fail("Not yet implemented"); + } + + @Test + public void testSaveGeneralInformation() + { + fail("Not yet implemented"); + } + + @Test + public void testZipAllFiles() + { + fail("Not yet implemented"); + } + + @Test + public void testMakeOneRequest() + { + fail("Not yet implemented"); + } + + @Test + public void testMain() + { + fail("Not yet implemented"); + } + +}