Browse Source

Start: get athlete first draft

master
Bianca Steffes 3 years ago
commit
1961b3de52
  1. 1
      .gitignore
  2. 20
      export/.classpath
  3. 2
      export/.gitignore
  4. 23
      export/.project
  5. 15
      export/.settings/org.eclipse.jdt.core.prefs
  6. 4
      export/.settings/org.eclipse.m2e.core.prefs
  7. 28
      export/pom.xml
  8. 193
      export/src/export/Main.java

1
.gitignore

@ -0,0 +1 @@
/.metadata/

20
export/.classpath

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

2
export/.gitignore

@ -0,0 +1,2 @@
/bin/
/target/

23
export/.project

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>export</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

15
export/.settings/org.eclipse.jdt.core.prefs

@ -0,0 +1,15 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=14
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=14
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=14

4
export/.settings/org.eclipse.m2e.core.prefs

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

28
export/pom.xml

@ -0,0 +1,28 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>export</groupId>
<artifactId>export</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>14</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>

193
export/src/export/Main.java

@ -0,0 +1,193 @@
package export;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.net.ssl.HttpsURLConnection;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Main
{
public static final String baseUrl = "https://www.strava.com/api/v3/";
private static int athleteId = 0;
private static JSONParser parser = new JSONParser();
private static void oneAthlete(String token)
{
HashMap<String, File> athletesTempFiles = new HashMap<String, File>();
// get general information
athletesTempFiles.put("general_information.json", saveGeneralInformation(token));
int activityId=0;
// get Activities
// for each activity: save streams
}
/**
* Extracts an athletes general information and writes the information to a temp file.
* @param token Identifier / authorization token of the athlete
* @return Created temp file or {@code null} if there was an error.
*/
@SuppressWarnings("unchecked")
private static File saveGeneralInformation(String token)
{
//TODO: auf fehlende WErte vorbereiten
String sex = "sex";
String country = "country";
String date_pref = "date_preference";
String meas_pref = "measurement_preference";
String weight = "weight";
String json = makeOneRequest("athlete", token);
JSONObject toSave = new JSONObject();
try
{
Object obj = parser.parse(json);
JSONObject data = (JSONObject) obj;
toSave.put(sex, data.get(sex));
toSave.put(country, data.get(country));
toSave.put(date_pref, data.get(date_pref));
toSave.put(meas_pref, data.get(meas_pref));
toSave.put(weight, data.get(weight));
toSave.put("id", athleteId);
File temp = File.createTempFile("general_" +athleteId , ".json");
temp.deleteOnExit();
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write(toSave.toString());
bw.close();
return temp;
}
catch (ParseException e)
{
//sonst ganze Datei abspeichern, nicht schön, aber evtl sinnvoll
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
catch (IOException e)
{
//Fehler beim Schreiben als Temp-File
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* Zip a list of files in one .zip file.
* @param files HasMap of <intended Filename, File> which should be zipped
* @throws IOException If there was an error zipping
*/
private static void zipAllFiles(Map<String,File> files) throws IOException
{
//TODO: checken, ob hier an den richtigen Ort gespeichert wird.
//TODO: evtl. alles nochmal zippen
FileOutputStream fos = new FileOutputStream("athlete_"+athleteId+".zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
for (String key : files.keySet())
{
File fileToZip = files.get(key);
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(key);
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
zipOut.close();
fos.close();
}
/**
* Handles one 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.
*/
private static String makeOneRequest(String requestUrlExtension, String token)
{
HttpURLConnection connection = null;
StringBuilder result = new StringBuilder();
try
{
//Create connection
URL url = new URL(baseUrl+requestUrlExtension);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", "Bearer [["+token+"]]");
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK)
{
//TODO: mehr auf response codes achten
return "";
}
InputStream inputStream = connection.getInputStream();
try (Reader reader = new BufferedReader(
new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name()))))
{
int c = reader.read();
while (c != -1)
{
result.append((char) c);
c = reader.read();
}
}
}
catch(IOException e)
{
//TODO: was tun bei error
return "";
}
return result.toString();
}
public static void main(String[] args)
{
List<String> tokens = new ArrayList<>();
//TODO: tokens befüllen
for (String token : tokens)
{
oneAthlete(token);
athleteId++;
}
}
}
Loading…
Cancel
Save