Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected LinkedHashMap<String, ArrayList<String>> doInBackground(Void... args) {
- // HTTP-Request zur Seite mit der Tabelle
- Connection.Response response = Relax.getConnectionResponse(RelaxMeta.RESOURCE_URL, String.valueOf(course.getId()), true);
- /*
- * Map zur Unterscheidung von "Abschnitt" und "Name" in der Tabelle
- *
- * map["Angewandte Chemie"] = {PDF-URL1, PDF-URL2, ....}
- */
- LinkedHashMap<String, ArrayList<String>> map = new LinkedHashMap<>();
- try {
- // <tr>-Elemente von <tbody> selecten
- Elements rows = response.parse().select("tbody").select(RelaxMeta.PATTERN_RESOURCES_ROWS);
- String last;
- String check;
- ArrayList<String> urls = new ArrayList<>();
- for (Element row : rows) {
- // <td>-Elemente der jeweiligen <tr> -- siehe Screenshot
- Elements columns = row.select(RelaxMeta.PATTERN_RESOURCES_COLUMN);
- // Titel, beim 1. for-Durchlauf "Angewandte Chemie"
- last = columns.get(0).text();
- // wenn der Titel "" ist, sind wir in der 2. Reihe (index = 1)
- if (last.equals("")) {
- String href;
- try {
- // 2. <td> der <tr> selecten, 1. <a>-Element holen
- href = columns.get(1).select("a").get(0).attr("href");
- // wenn es geklappt hat war das die 2. row
- } catch (IndexOutOfBoundsException ex) {
- // ansonsten war es die 3. - die juckt nicht
- continue;
- }
- // HTTP-Request zum geholten <a> und statuscode holen
- response = Relax.getConnectionResponse(href, false);
- int statusCode = response.statusCode();
- // 200?
- if (statusCode == HttpStatus.SC_OK) {
- // siehe Screenshot 2 ("Klicken Sie hier..... <a>blabla.pdf</a>")
- addUrl(urls, response.parse().select(String.format(RelaxMeta.PATTERN_STARTSWITH, RelaxMeta.PLUGINFILE_URL)).get(0).attr("href"));
- } else if (statusCode == HttpStatus.SC_SEE_OTHER) {
- // ansonsten wurde weitergeleitet, hier interessiert der Location-Header (Link zur PDF)
- addUrl(urls, response.header("Location"));
- }
- } else {
- // wenn der Titel nicht "" ist, sind wir in der 1. Reihe
- // den letzten Titel als check speichern
- check = last;
- currentKey = check;
- // map füllen, falls Links gesammelt wurden
- if (urls.size() > 0) {
- map.put(check, urls);
- urls = new ArrayList<>();
- }
- // Ordner erstellen --- das funktioniert
- File destinationDir = new File(String.format("%s/%s", course.getDir().toString(), check));
- if (!destinationDir.exists()) {
- if (!destinationDir.mkdirs()) {
- continue;
- }
- }
- currentDestination = destinationDir.toString();
- }
- }
- } catch (IOException ex) {
- //
- }
- return map;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement