#19 Parser for Goethe-Gymnasium Hamburg

Closed
TheOneWithTheBraid wants to merge 34 commits from theonewiththebraid/goethe into fynngodau/master

+ 7 - 0
app/src/main/java/godau/fynn/dsbdirect/Utility.java

@@ -37,6 +37,7 @@ import android.view.View;
 import android.view.ViewGroup;
 import godau.fynn.dsbdirect.manager.DownloadManager;
 import godau.fynn.dsbdirect.table.PollingService;
+import godau.fynn.dsbdirect.table.reader.GoetheHamburg;
 import godau.fynn.dsbdirect.table.reader.Reader;
 import godau.fynn.dsbdirect.table.reader.Roentgen;
 import godau.fynn.dsbdirect.table.reader.Untis;
@@ -58,6 +59,7 @@ public class Utility {
     public static final String UNTIS = "untis";
     public static final String UNTIS4 = "untis4"; // technically a SUPER SECRET SETTING
     public static final String ROENTGEN = "roentgen";
+    public static final String GOETHE = "goethe";
 
     public static final String DATE_FORMAT = "EEEE, d.M.yyyy";
 
@@ -123,6 +125,8 @@ public class Utility {
                 return new Untis4(html);
             case ROENTGEN:
                 return new Roentgen(html);
+            case GOETHE:
+                return new GoetheHamburg(html);
         }
 
         // Dynamic decision: check whether this might be a Untis table
@@ -152,6 +156,9 @@ public class Utility {
             case ROENTGEN:
             case "219261": // Röntgen-Gymnasium Würzburg
                 return ROENTGEN;
+            case GOETHE:
+            case "189802": // Goethe-Gymnasium Hamburg
+                return GOETHE;
             default:
                 return NULL;
         }

+ 1 - 1
app/src/main/java/godau/fynn/dsbdirect/activity/SettingsActivity.java

@@ -244,7 +244,7 @@ public class SettingsActivity extends Activity {
             findPreference("settings_about_email").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                 @Override
                 public boolean onPreferenceClick(Preference preference) {
-                    emailTheDev(getContext());
+                    emailTheDev(getActivity());
                     return false;
                 }
             });

+ 146 - 0
app/src/main/java/godau/fynn/dsbdirect/table/reader/GoetheHamburg.java

@@ -0,0 +1,146 @@
+/*
+ * DSBDirect
+ * Copyright (C) 2019 Jasper Michalke <jasper.michalke@jasmich.ml>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ * This software is not affiliated with heinekingmedia GmbH, the
+ * developer of the DSB platform.
+ */
+
+package godau.fynn.dsbdirect.table.reader;
+
+import android.content.Context;
+import android.util.Log;
+
+import godau.fynn.dsbdirect.activity.MainActivity;
+import godau.fynn.dsbdirect.table.Entry;
+import godau.fynn.dsbdirect.table.Table;
+
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+
+public class GoetheHamburg extends Reader {
+
+
+    public GoetheHamburg(String html) {
+        super(html);
+
+    }
+
+    @Override
+    public ArrayList<Entry> read() {
+        ArrayList<Entry> entries = new ArrayList<>();
+
+
+        Document document = Jsoup.parse(mHtml);
+
+        // Find date
+
+        Date date = null;
+        try {
+            String dateString = document.selectFirst(".dayHeader, legend").text().replaceAll(", \\w+", "");
+            date = new SimpleDateFormat("dd.MM.yyyy").parse(dateString);
+        } catch (ParseException e) {
+            date=new Date();
+            e.printStackTrace();
+        }
+
+        //android.widget.Toast.makeText(getApplicationContext(), dateString, Toast.LENGTH_SHORT).show();
+
+
+        // Prepare parsing tables
+
+
+        Elements classBlocks = document.select("table");
+        String[] classBlocksStrings = new String[classBlocks.size()];
+
+        Elements rows = classBlocks.select("tr");
+
+        String teacher = "";
+        String lastClass = ""; // Every class is just listed once even if there are several substitutes
+
+        for (Element row :
+                rows) {
+            try {
+
+                String infoText;
+                String affectedClass;
+                try {
+                    infoText=row.select("td").get(1).text();
+                    affectedClass=row.select("td").get(0).text();
+                    lastClass=affectedClass;
+                } catch (Exception e) {
+                    infoText=row.select("td").get(0).text();
+                    Log.d("Multi row", infoText);
+                    affectedClass=lastClass;
+                }
+
+                String lesson="";
+                Pattern pattern = Pattern.compile("(\\d.*) Std");
+                Matcher matcher = pattern.matcher(infoText);
+                if(matcher.find()) lesson=matcher.group(1).replaceAll("\\.", "");;
+
+
+                teacher = "";
+                pattern = Pattern.compile("bei ([A-ZÄÖÜ]{3}, [A-ZÄÖÜ]{3}|[A-ZÄÖÜ]{3})");
+                matcher = pattern.matcher(infoText);
+                if(matcher.find()) teacher=matcher.group(1);
+
+
+                String subject="";
+                pattern = Pattern.compile("\\d.* Std. (\\w+)");
+                matcher = pattern.matcher(infoText);
+                if(matcher.find()&& !matcher.group(1).equals("bei")) subject=matcher.group(1)+" ";
+
+
+                String info="";
+                String[] regex={"im Raum ([HVGFT]\\d{3}|ESH)", "statt bei [A-ZÄÖÜ]{3}", "\\(.*\\)", "(f.llt aus|Vtr. ohne Lehrer)"};
+                for (int i = 0; i < regex.length; i++) {
+                    pattern = Pattern.compile(regex[i]);
+                    matcher = pattern.matcher(infoText);
+                    if(matcher.find()) info=matcher.group(0);
+                }
+
+                info=subject+info;
+
+
+                if(!info.contains("im Raum")) {
+                    pattern = Pattern.compile("im Raum ([HVGFT]\\d{3}|ESH)");
+                    matcher = pattern.matcher(infoText);
+                    if(matcher.find()) info+=" ("+matcher.group(1)+")";
+                }
+
+                entries.add(new Entry(affectedClass, lesson, teacher, info, date));
+            }
+            catch (Exception e) {
+                Log.d("GOETHE","Error in row: "+row.text());
+                entries.add(new Entry(null, null, null, row.text(), date));
+            }
+
+        }
+
+        return entries;
+    }
+}

+ 2 - 2
app/src/main/java/godau/fynn/dsbdirect/table/reader/Reader.java

@@ -107,11 +107,11 @@ public abstract class Reader {
 
             boolean unterrichtsfrei = e.getInfo().contains("Unterrichtsfrei") || e.getInfo().contains("Unterrichtsende") || e.getInfo().contains("Unterrichtsschluss");
 
-            boolean classInClass = e.getAffectedClass().contains(number) && e.getAffectedClass().contains(letter);
+            boolean classInClass = e.getAffectedClass().contains(number) && e.getAffectedClass().toLowerCase().contains(letter.toLowerCase());
             boolean classInInfo = e.getInfo().contains(number) && e.getInfo().contains(letter);
             boolean affectedClassEmpty = e.getAffectedClass().length() == 0;
 
-            boolean teacherAppears = e.getReplacementTeacher().contains(name) || e.getInfo().contains(name);
+            boolean teacherAppears = e.getReplacementTeacher().toLowerCase().contains(name.toLowerCase()) || e.getInfo().contains(name);
 
             boolean infoOnly = affectedClassEmpty && e.getLesson().length() == 0 && e.getReplacementTeacher().length() == 0 && e.getInfo().length() > 0;
 

+ 1 - 2
app/src/main/res/values-de/strings.xml

@@ -134,7 +134,7 @@
     <string-array name="settings_view_parser_options">
         <item>Automatisch</item>
         <item>Untis</item>
-        <item>Röntgen</item>
+        <item>HeinekingMedia (Goethe)</item>
     </string-array>
     <string-array name="settings_view_layout_options">
         <item>Liste</item>
@@ -208,5 +208,4 @@
     <string name="migrate_roentgen_title">Röntgen nicht mehr unterstützt</string>
     <string name="migrate_roentgen_message">Röntgen benutzt nicht länger DSB, um seinen Vertretungsplan zu veröffentlichen. Deswegen ist es nicht mehr möglich, DSBDirect zu verwenden, um auf den Röntgen-Vertretungsplan zuzugreifen.</string>
     <string name="contact_app_developer">App-Entwickler kontaktieren</string>
-    <string name="settings_view_parser_disabled">Es gibt keine Auswahl, nur Untis-Pläne können momentan ausgelesen werden</string>
 </resources>

+ 2 - 3
app/src/main/res/values/strings.xml

@@ -201,17 +201,16 @@
     <string name="migrate_roentgen_title">Röntgen no longer supported</string>
     <string name="migrate_roentgen_message">Röntgen is no longer using DSB to publish plans. Thus, it is no longer possible to use DSBDirect to access the Röntgen substitute plan.</string>
     <string name="contact_app_developer">Contact app developer</string>
-    <string name="settings_view_parser_disabled">There is no choice, only Untis boards can be parsed at the moment</string>
 
     <string-array name="settings_view_parser_options">
         <item>Automatic</item>
         <item>Untis</item>
-        <item>Röntgen</item>
+        <item>HeinekingMedia (Goethe)</item>
     </string-array>
     <string-array name="settings_view_parser_values">
         <item>automatic</item>
         <item>untis</item>
-        <item>roentgen</item>
+        <item>goethe</item>
     </string-array>
 
     <string-array name="settings_view_layout_options">

+ 1 - 2
app/src/main/res/xml/preferences.xml

@@ -37,8 +37,7 @@
             android:entries="@array/settings_view_parser_options"
             android:entryValues="@array/settings_view_parser_values"
             android:defaultValue="automatic"
-            android:summary="@string/settings_view_parser_disabled"
-            android:enabled="false"
+            android:summary="%s"
             android:dependency="parse"
             android:key="parser"
             /> <!-- Thanks, https://stackoverflow.com/a/24772748 -->

+ 1 - 0
settings.gradle

@@ -1 +1,2 @@
 include ':app'
+