Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. @Override
  2. protected LinkedHashMap<String, ArrayList<String>> doInBackground(Void... args) {
  3.  
  4.     // HTTP-Request zur Seite mit der Tabelle
  5.     Connection.Response response = Relax.getConnectionResponse(RelaxMeta.RESOURCE_URL, String.valueOf(course.getId()), true);
  6.  
  7.     /*
  8.      * Map zur Unterscheidung von "Abschnitt" und "Name" in der Tabelle
  9.      *
  10.      *  map["Angewandte Chemie"] = {PDF-URL1, PDF-URL2, ....}
  11.      */
  12.     LinkedHashMap<String, ArrayList<String>> map = new LinkedHashMap<>();
  13.  
  14.     try {
  15.         // <tr>-Elemente von <tbody> selecten
  16.         Elements rows = response.parse().select("tbody").select(RelaxMeta.PATTERN_RESOURCES_ROWS);
  17.  
  18.         String last;
  19.         String check;
  20.         ArrayList<String> urls = new ArrayList<>();
  21.  
  22.         for (Element row : rows) {
  23.  
  24.             // <td>-Elemente der jeweiligen <tr> -- siehe Screenshot
  25.             Elements columns = row.select(RelaxMeta.PATTERN_RESOURCES_COLUMN);
  26.  
  27.             // Titel, beim 1. for-Durchlauf "Angewandte Chemie"
  28.             last = columns.get(0).text();
  29.  
  30.             // wenn der Titel "" ist, sind wir in der 2. Reihe (index = 1)
  31.             if (last.equals("")) {
  32.  
  33.                 String href;
  34.  
  35.                 try {
  36.                     // 2. <td> der <tr> selecten, 1. <a>-Element holen
  37.                     href = columns.get(1).select("a").get(0).attr("href");
  38.  
  39.                     // wenn es geklappt hat war das die 2. row
  40.                 } catch (IndexOutOfBoundsException ex) {
  41.  
  42.                     // ansonsten war es die 3. - die juckt nicht
  43.                     continue;
  44.                 }
  45.  
  46.                 // HTTP-Request zum geholten <a> und statuscode holen
  47.                 response = Relax.getConnectionResponse(href, false);
  48.                 int statusCode = response.statusCode();
  49.  
  50.                 // 200?
  51.                 if (statusCode == HttpStatus.SC_OK) {
  52.  
  53.                     // siehe Screenshot 2 ("Klicken Sie hier..... <a>blabla.pdf</a>")
  54.                     addUrl(urls, response.parse().select(String.format(RelaxMeta.PATTERN_STARTSWITH, RelaxMeta.PLUGINFILE_URL)).get(0).attr("href"));
  55.                 } else if (statusCode == HttpStatus.SC_SEE_OTHER) {
  56.  
  57.                     // ansonsten wurde weitergeleitet, hier interessiert der Location-Header (Link zur PDF)
  58.                     addUrl(urls, response.header("Location"));
  59.                 }
  60.             } else {
  61.  
  62.                 // wenn der Titel nicht "" ist, sind wir in der 1. Reihe
  63.  
  64.                 // den letzten Titel als check speichern
  65.                 check = last;
  66.                 currentKey = check;
  67.  
  68.                 // map füllen, falls Links gesammelt wurden
  69.                 if (urls.size() > 0) {
  70.                     map.put(check, urls);
  71.                     urls = new ArrayList<>();
  72.                 }
  73.  
  74.                 // Ordner erstellen --- das funktioniert
  75.                 File destinationDir = new File(String.format("%s/%s", course.getDir().toString(), check));
  76.  
  77.                 if (!destinationDir.exists()) {
  78.                     if (!destinationDir.mkdirs()) {
  79.                         continue;
  80.                     }
  81.                 }
  82.  
  83.                 currentDestination = destinationDir.toString();
  84.             }
  85.         }
  86.     } catch (IOException ex) {
  87.         //
  88.     }
  89.  
  90.     return map;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement