Untis.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * DSBDirect
  3. * Copyright (C) 2019 Fynn Godau
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * This software is not affiliated with heinekingmedia GmbH, the
  19. * developer of the DSB platform.
  20. */
  21. package godau.fynn.dsbdirect.table.reader;
  22. import android.content.Context;
  23. import android.util.Log;
  24. import godau.fynn.dsbdirect.Utility;
  25. import godau.fynn.dsbdirect.table.Entry;
  26. import org.jsoup.Jsoup;
  27. import org.jsoup.nodes.Document;
  28. import org.jsoup.nodes.Element;
  29. import org.jsoup.select.Elements;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.Calendar;
  33. import java.util.Date;
  34. public class Untis extends Reader {
  35. private static final int UNDEFINED = 0;
  36. private static final int CLASS = 1;
  37. private static final int SUBJECT = 2;
  38. private static final int LESSON = 3;
  39. private static final int TYPE = 4;
  40. private static final int TEACHER = 5;
  41. private static final int ROOM = 6;
  42. private static final int OLD_SUBJECT = 7;
  43. private static final int OLD_TEACHER = 8;
  44. private static final int OLD_CLASS = 9;
  45. private static final int INFO = 10;
  46. private static final int MASTER_SIZE = 11;
  47. public Untis(String html) {
  48. super(html);
  49. }
  50. @Override
  51. public ArrayList<Entry> read() {
  52. ArrayList<Entry> entries = new ArrayList<>();
  53. // Leave brs be
  54. mHtml = mHtml.replaceAll("<br>", "&lt;br&gt;");
  55. // Leave strikes be
  56. mHtml = mHtml.replaceAll("<s(trike)*>", "&lt;strike&gt;");
  57. mHtml = mHtml.replaceAll("</s(trike)*>", "&lt;&#47;strike&gt;");
  58. Document d = Jsoup.parse(mHtml);
  59. // Tables are inside center tags
  60. Elements centers = d.getElementsByTag("center");
  61. // Every other center contains an advertisement for Untis
  62. for (int centerIndex = 0; centerIndex < centers.size(); centerIndex += 2) {
  63. Element center = centers.get(centerIndex);
  64. // Get which date this center is about
  65. String dateString = center.selectFirst("div").text();
  66. String[] dateDigits = dateString.split(" ")[0].split("\\.");
  67. Calendar calendar = Utility.zeroOClock(Calendar.getInstance());
  68. calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateDigits[0]));
  69. calendar.set(Calendar.MONTH, Integer.parseInt(dateDigits[1]) - 1);
  70. calendar.set(Calendar.YEAR, Integer.parseInt(dateDigits[2]));
  71. Date date = calendar.getTime();
  72. // Get info box, if present
  73. Elements infoTables = center.getElementsByClass("info");
  74. if (infoTables.size() != 0) {
  75. Element infoTableBody = infoTables.first().getElementsByTag("tbody").first();
  76. Elements infoTableTrs = infoTableBody.getElementsByTag("tr");
  77. /* First tr will (probably) contain "Nachrichten zum Tag" headline, but we check it anyway because
  78. * the headline is inside a th tag, not a td tag.
  79. */
  80. for (Element tr : infoTableTrs) {
  81. Elements tds = tr.getElementsByTag("td");
  82. if (tds.size() > 0 && !isUselessLine(tds.first().text())) {
  83. // Construct an entry for this line
  84. // If there are two columns: separate them with a ':'
  85. Entry e = new Entry(null, null, null,
  86. Utility.smartConcatenate(tds, ": "),
  87. date
  88. );
  89. entries.add(e);
  90. }
  91. }
  92. }
  93. // Get main table
  94. Elements mainTables = center.getElementsByClass("mon_list"); // There should be exactly one
  95. if (mainTables.size() > 0) {
  96. Element mainTableBody = mainTables.first().getElementsByTag("tbody").first();
  97. Elements mainTableTrs = mainTableBody.getElementsByTag("tr");
  98. // Get definitions from the first row
  99. Object[] mainTableDefinitionObjects = mainTableTrs.first().getElementsByTag("th")
  100. .eachText().toArray();
  101. String[] mainTableDefinitions = Arrays.copyOf(mainTableDefinitionObjects,
  102. mainTableDefinitionObjects.length, String[].class);
  103. int[] positions = getMasterTablePositions(mainTableDefinitions);
  104. // Get every row
  105. // Start with 1 because first row contained definitions
  106. for (int trIndex = 1; trIndex < mainTableTrs.size(); trIndex++) {
  107. Element tr = mainTableTrs.get(trIndex);
  108. Elements tds = tr.getElementsByTag("td");
  109. String[] masterRow = new String[MASTER_SIZE];
  110. // Get value from every column
  111. for (int tdIndex = 0; tdIndex < tds.size(); tdIndex++) {
  112. Element td = tds.get(tdIndex);
  113. String s = td.text();
  114. masterRow[positions[tdIndex]] = s;
  115. }
  116. entries.add(constructEntry(masterRow, date));
  117. }
  118. }
  119. }
  120. Log.d("UNTISREAD", "Read out " + entries.size() + " entries");
  121. return entries;
  122. }
  123. private Entry constructEntry(String[] masterRow, Date date) {
  124. String classString = ratherThisThanThat(masterRow[CLASS], masterRow[OLD_CLASS]);
  125. String subject = ratherThisThanThat(masterRow[SUBJECT], masterRow[OLD_SUBJECT]);
  126. String combinedClassString = Utility.smartConcatenate(new String[]{classString, subject}, " · ");
  127. String lesson = masterRow[LESSON];
  128. String teacher = ratherThisThanThat(masterRow[TEACHER], masterRow[OLD_TEACHER]);
  129. String info = Utility.smartConcatenate(new String[]{
  130. masterRow[TYPE], masterRow[ROOM], masterRow[INFO]
  131. }, " · ");
  132. return new Entry(combinedClassString, lesson, teacher, info, date);
  133. }
  134. private String ratherThisThanThat(String string1, String string2) {
  135. // Prefers to return string1, but returns string2 surrounded by strike tags if string1 is null, empty or just dashes.
  136. final String DASHES_REGEX = "-+(?!.)"; // matches one or more "-" if nothing else follows it
  137. if (string1 != null && !string1.isEmpty() && !string1.matches(DASHES_REGEX)) {
  138. return string1;
  139. } else if (string2 != null) { // Don't concatenate around with null
  140. return "<strike>" + string2 + "</strike>";
  141. } else {
  142. return null;
  143. }
  144. }
  145. private int[] getMasterTablePositions(String[] definitions) {
  146. // Return where in the imaginary master table each column belongs
  147. int[] positions = new int[definitions.length];
  148. // Test each definition for matches with strings
  149. for (int i = 0; i < definitions.length; i++) {
  150. switch (definitions[i]) {
  151. case "Klasse(n)":
  152. positions[i] = CLASS; continue;
  153. case "Fach":
  154. positions[i] = SUBJECT; continue;
  155. case "Stunde":
  156. positions[i] = LESSON; continue;
  157. case "Art":
  158. positions[i] = TYPE; continue;
  159. case "Vertreter":
  160. case "Vertr. von":
  161. positions[i] = TEACHER; continue;
  162. case "Raum":
  163. positions[i] = ROOM; continue;
  164. case "(Fach)":
  165. positions[i] = OLD_SUBJECT; continue;
  166. case "(Lehrer)":
  167. positions[i] = OLD_TEACHER; continue;
  168. case "(Klasse(n))":
  169. positions[i] = OLD_CLASS; continue;
  170. case "Vertretungs-Text":
  171. positions[i] = INFO; continue;
  172. default:
  173. positions[i] = UNDEFINED;
  174. }
  175. }
  176. return positions;
  177. }
  178. private boolean isUselessLine(String string) {
  179. String[] uselessLines = {"Abwesende Klassen", "Betroffene Klassen"};
  180. for (String useless :
  181. uselessLines) {
  182. if (string.contains(useless)) {
  183. // If it contains something useless, this line is useless
  184. return true;
  185. }
  186. }
  187. // It didn't contain anything useless and thus is not useless
  188. return false;
  189. }
  190. }