From 2c3529de4d0a2407e84a4718fb2c91601907c268 Mon Sep 17 00:00:00 2001 From: mdway Date: Thu, 28 Sep 2017 16:06:50 +0200 Subject: Initial commit --- .idea/compiler.xml | 9 ++ .idea/gradle.xml | 18 +++ ...adle__com_google_code_findbugs_jsr305_1_3_9.xml | 9 ++ .../Gradle__com_google_guava_guava_11_0_2.xml | 11 ++ .../Gradle__commons_codec_commons_codec_1_3.xml | 11 ++ ...adle__commons_logging_commons_logging_1_1_1.xml | 11 ++ .idea/libraries/Gradle__junit_junit_4_12.xml | 11 ++ ...apache_httpcomponents_httpclient_4_0_alpha4.xml | 9 ++ ...rg_apache_httpcomponents_httpcore_4_0_beta1.xml | 9 ++ ...adle__org_codehaus_groovy_groovy_all_2_3_11.xml | 11 ++ .../Gradle__org_hamcrest_hamcrest_core_1_3.xml | 11 ++ .idea/libraries/Gradle__org_json_json_20090211.xml | 9 ++ .idea/libraries/Gradle__org_jsoup_jsoup_1_10_3.xml | 11 ++ .idea/misc.xml | 6 + .idea/modules.xml | 10 ++ .idea/modules/dsb-api.iml | 13 ++ .idea/modules/dsb-api_main.iml | 23 +++ .idea/modules/dsb-api_test.iml | 27 ++++ .idea/uiDesigner.xml | 124 +++++++++++++++ GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed.apk | Bin 0 -> 3426687 bytes ...2.6.b.9.crk.ADS.Removed[1]_source_from_JADX.zip | Bin 0 -> 4157766 bytes build.gradle | 20 +++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54706 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 172 +++++++++++++++++++++ gradlew.bat | 84 ++++++++++ settings.gradle | 2 + src/main/java/de/romjaki/dsbclient/DSBClient.java | 144 +++++++++++++++++ .../java/de/romjaki/dsbclient/TimetableEntry.java | 95 ++++++++++++ src/main/java/de/romjaki/dsbclient/UnUtil.java | 75 +++++++++ src/test/java/Testing.java | 14 ++ 31 files changed, 955 insertions(+) create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/libraries/Gradle__com_google_code_findbugs_jsr305_1_3_9.xml create mode 100644 .idea/libraries/Gradle__com_google_guava_guava_11_0_2.xml create mode 100644 .idea/libraries/Gradle__commons_codec_commons_codec_1_3.xml create mode 100644 .idea/libraries/Gradle__commons_logging_commons_logging_1_1_1.xml create mode 100644 .idea/libraries/Gradle__junit_junit_4_12.xml create mode 100644 .idea/libraries/Gradle__org_apache_httpcomponents_httpclient_4_0_alpha4.xml create mode 100644 .idea/libraries/Gradle__org_apache_httpcomponents_httpcore_4_0_beta1.xml create mode 100644 .idea/libraries/Gradle__org_codehaus_groovy_groovy_all_2_3_11.xml create mode 100644 .idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/libraries/Gradle__org_json_json_20090211.xml create mode 100644 .idea/libraries/Gradle__org_jsoup_jsoup_1_10_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/modules/dsb-api.iml create mode 100644 .idea/modules/dsb-api_main.iml create mode 100644 .idea/modules/dsb-api_test.iml create mode 100644 .idea/uiDesigner.xml create mode 100644 GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed.apk create mode 100644 GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed[1]_source_from_JADX.zip create mode 100644 build.gradle create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle create mode 100644 src/main/java/de/romjaki/dsbclient/DSBClient.java create mode 100644 src/main/java/de/romjaki/dsbclient/TimetableEntry.java create mode 100644 src/main/java/de/romjaki/dsbclient/UnUtil.java create mode 100644 src/test/java/Testing.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..f3e8e28 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..05e5219 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_google_code_findbugs_jsr305_1_3_9.xml b/.idea/libraries/Gradle__com_google_code_findbugs_jsr305_1_3_9.xml new file mode 100644 index 0000000..63b7b35 --- /dev/null +++ b/.idea/libraries/Gradle__com_google_code_findbugs_jsr305_1_3_9.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_google_guava_guava_11_0_2.xml b/.idea/libraries/Gradle__com_google_guava_guava_11_0_2.xml new file mode 100644 index 0000000..9cd90cb --- /dev/null +++ b/.idea/libraries/Gradle__com_google_guava_guava_11_0_2.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__commons_codec_commons_codec_1_3.xml b/.idea/libraries/Gradle__commons_codec_commons_codec_1_3.xml new file mode 100644 index 0000000..a07b00a --- /dev/null +++ b/.idea/libraries/Gradle__commons_codec_commons_codec_1_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__commons_logging_commons_logging_1_1_1.xml b/.idea/libraries/Gradle__commons_logging_commons_logging_1_1_1.xml new file mode 100644 index 0000000..b9fb751 --- /dev/null +++ b/.idea/libraries/Gradle__commons_logging_commons_logging_1_1_1.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__junit_junit_4_12.xml b/.idea/libraries/Gradle__junit_junit_4_12.xml new file mode 100644 index 0000000..04c10dd --- /dev/null +++ b/.idea/libraries/Gradle__junit_junit_4_12.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_apache_httpcomponents_httpclient_4_0_alpha4.xml b/.idea/libraries/Gradle__org_apache_httpcomponents_httpclient_4_0_alpha4.xml new file mode 100644 index 0000000..aa27d7d --- /dev/null +++ b/.idea/libraries/Gradle__org_apache_httpcomponents_httpclient_4_0_alpha4.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_apache_httpcomponents_httpcore_4_0_beta1.xml b/.idea/libraries/Gradle__org_apache_httpcomponents_httpcore_4_0_beta1.xml new file mode 100644 index 0000000..6b106cd --- /dev/null +++ b/.idea/libraries/Gradle__org_apache_httpcomponents_httpcore_4_0_beta1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_codehaus_groovy_groovy_all_2_3_11.xml b/.idea/libraries/Gradle__org_codehaus_groovy_groovy_all_2_3_11.xml new file mode 100644 index 0000000..b484437 --- /dev/null +++ b/.idea/libraries/Gradle__org_codehaus_groovy_groovy_all_2_3_11.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..8262f72 --- /dev/null +++ b/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_json_json_20090211.xml b/.idea/libraries/Gradle__org_json_json_20090211.xml new file mode 100644 index 0000000..920bb5c --- /dev/null +++ b/.idea/libraries/Gradle__org_json_json_20090211.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_jsoup_jsoup_1_10_3.xml b/.idea/libraries/Gradle__org_jsoup_jsoup_1_10_3.xml new file mode 100644 index 0000000..16ff9be --- /dev/null +++ b/.idea/libraries/Gradle__org_jsoup_jsoup_1_10_3.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..905dc43 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/dsb-api.iml b/.idea/modules/dsb-api.iml new file mode 100644 index 0000000..30c79ad --- /dev/null +++ b/.idea/modules/dsb-api.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/dsb-api_main.iml b/.idea/modules/dsb-api_main.iml new file mode 100644 index 0000000..a25d64a --- /dev/null +++ b/.idea/modules/dsb-api_main.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/dsb-api_test.iml b/.idea/modules/dsb-api_test.iml new file mode 100644 index 0000000..107825c --- /dev/null +++ b/.idea/modules/dsb-api_test.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed.apk b/GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed.apk new file mode 100644 index 0000000..311d9ff Binary files /dev/null and b/GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed.apk differ diff --git a/GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed[1]_source_from_JADX.zip b/GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed[1]_source_from_JADX.zip new file mode 100644 index 0000000..6d64b78 Binary files /dev/null and b/GRB.Vertretungsplan.v.2.6.b.9.crk.ADS.Removed[1]_source_from_JADX.zip differ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..a33374b --- /dev/null +++ b/build.gradle @@ -0,0 +1,20 @@ +group 'roman' +version '1.0-SNAPSHOT' + +apply plugin: 'groovy' +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + compile 'org.codehaus.groovy:groovy-all:2.3.11' + compile group: 'com.google.guava', name: 'guava', version: '11.0.2' + compile group: 'org.jsoup', name: 'jsoup', version: '1.10.3' + compile group: 'org.json', name: 'json', version: '20090211' + compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.0-alpha4' + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..218ff24 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..cc4694f --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Mon Sep 25 19:47:36 CEST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..cccdd3d --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..f955316 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..3d5bd6b --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'dsb-api' + diff --git a/src/main/java/de/romjaki/dsbclient/DSBClient.java b/src/main/java/de/romjaki/dsbclient/DSBClient.java new file mode 100644 index 0000000..01927cd --- /dev/null +++ b/src/main/java/de/romjaki/dsbclient/DSBClient.java @@ -0,0 +1,144 @@ +package de.romjaki.dsbclient; + +import com.google.common.collect.Iterables; +import org.apache.http.HttpException; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +public class DSBClient { + private static final String BASE_URL = "https://iphone.dsbcontrol.de/iPhoneService.svc/DSB/"; + private static final String AUTH_URL = BASE_URL + "AuthID/"; + private static final String DATA_URL = BASE_URL + "timetables/"; + private static final DateFormat dateformat = new SimpleDateFormat("d.M.Y H:m"); + private final String user; + private final String pass; + private String authid; + private DefaultHttpClient client = new DefaultHttpClient(); + private Map> cache; + + private DSBClient(String user, String pass) { + this.user = user; + this.pass = pass; + } + + public static DSBClient buildBlocking(String user, String pass) throws HttpException, IOException, URISyntaxException, JSONException, ParseException { + DSBClient client = new DSBClient(user, pass); + client.login(); + client.receiveData(); + return client; + } + + public static DSBClient buildAsync(String user, String pass, Consumer exceptionCallback) { + DSBClient client = new DSBClient(user, pass); + Thread t = new Thread(() -> { + try { + client.login(); + client.receiveData(); + } catch (Exception e) { + exceptionCallback.accept(e); + } + }); + return client; + } + + private void login() throws IOException, HttpException, URISyntaxException { + HttpGet get = new HttpGet(AUTH_URL + user + "/" + pass); + + HttpResponse response = client.execute(get); + + authid = EntityUtils.toString(response.getEntity()).replace("\"", ""); + } + + private void receiveData() throws URISyntaxException, IOException, HttpException, JSONException, ParseException { + HttpGet get = new HttpGet(DATA_URL + authid); + + HttpResponse response = client.execute(get); + + String str = EntityUtils.toString(response.getEntity()); + JSONArray data = new JSONArray(str); + cache = new HashMap<>(); + for (int i = 0; i < data.length(); i++) { + JSONObject o = data.getJSONObject(i); + boolean isHtml = o.getBoolean("ishtml"); + Date timetabledate = dateformat.parse(o.getString("timetabledate")); + String timetableurl = o.getString("timetableurl"); + long millis = UnUtil.sanitizeDate(UnUtil.calendarByDate(timetabledate, Locale.GERMANY)).getTimeInMillis(); + List arrL = cache.computeIfAbsent(millis, ignored -> new ArrayList<>()); + arrL.addAll(parseEntry(isHtml, timetableurl)); + } + } + + + private List parseEntry(boolean isHtml, String timetableurl) throws URISyntaxException, IOException, HttpException { + if (!isHtml) return Collections.emptyList(); + HttpGet get = new HttpGet(timetableurl); + HttpResponse response = client.execute(get); + Document document = Jsoup.parse(EntityUtils.toString(response.getEntity())); + Elements rows = document.getElementsByTag("tr").not(":not(:has(th))"); + List entries = new ArrayList<>(rows.size()); + for (Element row : rows) { + entries.add(parseElement(row)); + } + return entries; + } + + private TimetableEntry parseElement(Element row) { + TimetableEntry entry = new TimetableEntry(); + entry.setKlasse(row.child(1).text()); + entry.setStunde(row.child(3).text()); + entry.setVertreter(row.child(4).text()); + entry.setFach(row.child(5).text()); + entry.setRaum(row.child(8).text()); + entry.setArt(row.child(9).text()); + entry.setText(row.child(11).text()); + if (entry.getFach() == null) { + entry.setFach(row.child(6).text()); + } + if (entry.getRaum() == null) { + entry.setRaum(row.child(7).text()); + } + if (entry.getKlasse() == null) { + entry.setKlasse(row.child(10).text()); + } + return entry; + } + + public List byTeacher(String teacher) { + return filter(timetableEntry -> timetableEntry.getVertreter().equalsIgnoreCase(teacher)); + } + + public List byDate(Date date) { + return cache.get(UnUtil.sanitizeDate(UnUtil.calendarByDate(date, Locale.GERMANY)).getTimeInMillis()); + } + + public List byClass(String klasse) { + return filter(timetableEntry -> timetableEntry.getKlasse().contains(klasse) || klasse.contains(timetableEntry.getKlasse())); + } + + + public List filter(Predicate predicate) { + return StreamSupport.stream(Iterables.concat(cache.values()).spliterator(), false) + .filter(predicate) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/de/romjaki/dsbclient/TimetableEntry.java b/src/main/java/de/romjaki/dsbclient/TimetableEntry.java new file mode 100644 index 0000000..a888cfc --- /dev/null +++ b/src/main/java/de/romjaki/dsbclient/TimetableEntry.java @@ -0,0 +1,95 @@ +package de.romjaki.dsbclient; + +public class TimetableEntry { + private String klasse; + private String stunde; + private String vertreter; + private String fach; + private String raum; + private String art; + private String text; + + public String getKlasse() { + return klasse; + } + + public void setKlasse(String klasse) { + klasse = klasse.trim(); + if (klasse.isEmpty()) { + klasse = null; + } + this.klasse = klasse; + } + + public String getStunde() { + return stunde; + } + + public void setStunde(String stunde) { + stunde = stunde.trim(); + if (stunde.isEmpty()) { + stunde = null; + } + this.stunde = stunde; + } + + public String getVertreter() { + return vertreter; + } + + public void setVertreter(String vertreter) { + vertreter = vertreter.trim(); + if (vertreter.isEmpty()) { + vertreter = null; + } + this.vertreter = vertreter; + } + + public String getFach() { + return fach; + } + + public void setFach(String fach) { + fach = fach.trim(); + if (fach.isEmpty()) { + fach = null; + } + this.fach = fach; + } + + public String getRaum() { + return raum; + } + + public void setRaum(String raum) { + raum = raum.trim(); + if (raum.isEmpty()) { + raum = null; + } + this.raum = raum; + } + + public String getArt() { + return art; + } + + public void setArt(String art) { + art = art.trim(); + if (art.isEmpty()) { + art = null; + } + this.art = art; + } + + public String getText() { + return text; + } + + public void setText(String text) { + text = text.trim(); + if (text.isEmpty()) { + text = null; + } + this.text = text; + } +} diff --git a/src/main/java/de/romjaki/dsbclient/UnUtil.java b/src/main/java/de/romjaki/dsbclient/UnUtil.java new file mode 100644 index 0000000..2c3af02 --- /dev/null +++ b/src/main/java/de/romjaki/dsbclient/UnUtil.java @@ -0,0 +1,75 @@ +package de.romjaki.dsbclient; + + +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.message.BasicNameValuePair; + +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.*; + +import static java.util.Calendar.*; + +public class UnUtil { + + private UnUtil() { + UnUtil.singleton(UnUtil.class); + } + + public static Calendar calendarByDate(Date date, Locale germany) { + Calendar calendar = Calendar.getInstance(Locale.GERMANY); + calendar.setTime(date); + return calendar; + } + + public static Calendar sanitizeDate(Calendar temp) { + Calendar calendar = Calendar.getInstance(temp.getTimeZone()); + + calendar.set( + temp.get(YEAR), + temp.get(MONTH), + temp.get(DAY_OF_MONTH), + 0, 0, 0); + return calendar; + } + + public static void singleton(Class utilClass) throws IllegalStateException { + throw new IllegalStateException("No " + utilClass.getCanonicalName() + " class instances for you :P"); + } + + public static String httpGet(String urlString, Map> headers) throws IOException { + return performGeneralHttpRequest("GET", urlString, headers); + } + + public static String performGeneralHttpRequest(String method, String urlString, Map> headers) throws IOException { + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod(method); + conn.getRequestProperties().putAll(headers); + + conn.setDoInput(true); + conn.setDoOutput(true); + conn.setUseCaches(false); + + InputStream stream = conn.getInputStream(); + return readFully(stream); + } + + public static UrlEncodedFormEntity createFormEntity(Map params) { + List nameValuePairs = new ArrayList<>(); + params.forEach((name, val) -> nameValuePairs.add(new BasicNameValuePair(name, val))); + try { + return new UrlEncodedFormEntity(nameValuePairs, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return null; + } + } + + public static String readFully(InputStream stream) { + return new Scanner(stream).useDelimiter("\\A").next(); + } +} diff --git a/src/test/java/Testing.java b/src/test/java/Testing.java new file mode 100644 index 0000000..b404e8d --- /dev/null +++ b/src/test/java/Testing.java @@ -0,0 +1,14 @@ +import de.romjaki.dsbclient.DSBClient; +import org.apache.http.HttpException; +import org.json.JSONException; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.text.ParseException; + +public class Testing { + public static void main(String[] args) throws URISyntaxException, IOException, HttpException, JSONException, ParseException { + DSBClient client = DSBClient.buildBlocking("166162", "Riedberg2014"); + + } +} -- cgit