GoetheHamburg.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * DSBDirect
  3. * Copyright (C) 2019 Jasper Michalke <jasper.michalke@jasmich.ml>
  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.activity.MainActivity;
  25. import godau.fynn.dsbdirect.table.Entry;
  26. import godau.fynn.dsbdirect.table.Table;
  27. import org.jsoup.Jsoup;
  28. import org.jsoup.nodes.Document;
  29. import org.jsoup.nodes.Element;
  30. import org.jsoup.select.Elements;
  31. import java.text.ParseException;
  32. import java.text.SimpleDateFormat;
  33. import java.util.ArrayList;
  34. import java.util.Date;
  35. import java.util.regex.Matcher;
  36. import java.util.regex.Pattern;
  37. public class GoetheHamburg extends Reader {
  38. public GoetheHamburg(String html) {
  39. super(html);
  40. }
  41. @Override
  42. public ArrayList<Entry> read() {
  43. ArrayList<Entry> entries = new ArrayList<>();
  44. Document document = Jsoup.parse(mHtml);
  45. // Find date
  46. Date date = null;
  47. try {
  48. String dateString = document.selectFirst(".dayHeader, legend").text().replaceAll(", \\w+", "");
  49. date = new SimpleDateFormat("dd.MM.yyyy").parse(dateString);
  50. } catch (ParseException e) {
  51. date=new Date();
  52. e.printStackTrace();
  53. }
  54. //android.widget.Toast.makeText(getApplicationContext(), dateString, Toast.LENGTH_SHORT).show();
  55. // Prepare parsing tables
  56. Elements classBlocks = document.select("table");
  57. String[] classBlocksStrings = new String[classBlocks.size()];
  58. Elements rows = classBlocks.select("tr");
  59. String teacher = "";
  60. String lastClass = ""; // Every class is just listed once even if there are several substitutes
  61. for (Element row :
  62. rows) {
  63. try {
  64. String infoText;
  65. String affectedClass;
  66. try {
  67. infoText=row.select("td").get(1).text();
  68. affectedClass=row.select("td").get(0).text();
  69. lastClass=affectedClass;
  70. } catch (Exception e) {
  71. infoText=row.select("td").get(0).text();
  72. Log.d("Multi row", infoText);
  73. affectedClass=lastClass;
  74. }
  75. String lesson="";
  76. Pattern pattern = Pattern.compile("(\\d.*) Std");
  77. Matcher matcher = pattern.matcher(infoText);
  78. if(matcher.find()) lesson=matcher.group(1).replaceAll("\\.", "");;
  79. teacher = "";
  80. pattern = Pattern.compile("bei ([A-ZÄÖÜ]{3}, [A-ZÄÖÜ]{3}|[A-ZÄÖÜ]{3})");
  81. matcher = pattern.matcher(infoText);
  82. if(matcher.find()) teacher=matcher.group(1);
  83. String subject="";
  84. pattern = Pattern.compile("\\d.* Std. (\\w+)");
  85. matcher = pattern.matcher(infoText);
  86. if(matcher.find()&& !matcher.group(1).equals("bei")) subject=matcher.group(1)+" ";
  87. String info="";
  88. String[] regex={"im Raum ([HVGFT]\\d{3}|ESH)", "statt bei [A-ZÄÖÜ]{3}", "\\(.*\\)", "(f.llt aus|Vtr. ohne Lehrer)"};
  89. for (int i = 0; i < regex.length; i++) {
  90. pattern = Pattern.compile(regex[i]);
  91. matcher = pattern.matcher(infoText);
  92. if(matcher.find()) info=matcher.group(0);
  93. }
  94. info=subject+info;
  95. if(!info.contains("im Raum")) {
  96. pattern = Pattern.compile("im Raum ([HVGFT]\\d{3}|ESH)");
  97. matcher = pattern.matcher(infoText);
  98. if(matcher.find()) info+=" ("+matcher.group(1)+")";
  99. }
  100. entries.add(new Entry(affectedClass, lesson, teacher, info, date));
  101. }
  102. catch (Exception e) {
  103. Log.d("GOETHE","Error in row: "+row.text());
  104. entries.add(new Entry(null, null, null, row.text(), date));
  105. }
  106. }
  107. return entries;
  108. }
  109. }