Skip to content

Commit b9bdf75

Browse files
committed
Fix location redirect and update baseUri
1 parent b3774d0 commit b9bdf75

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Improve redirect handling
77
- Add new weekly scraping interval
88
- Add setting to hide credit points
9+
- Fix: Update baseUri after location redirect
910

1011
# 1.1.2
1112

app/src/main/java/de/mygrades/main/core/Scraper.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public String scrape(boolean tableAsInterimResult) throws IOException, ParseExce
169169
Log.v(TAG, action.toString());
170170

171171
String url = getUrl(parsedHtml, action.getUrl());
172-
Log.v(TAG, "Action " + (i + 1) + "/" + actions.size() + " -- Sending Request to url: " + url);
172+
Log.v(TAG, "--- Action " + (i + 1) + "/" + actions.size() + " -- Sending Request to url: " + url);
173173

174174
// make request with data, cookies, current method
175175
getRequestData(requestData, action.getActionParams());
@@ -242,7 +242,7 @@ public Map<String, String> scrapeMultipleTables(Map<String, TransformerMapping>
242242
Log.v(TAG, action.toString());
243243

244244
String url = getUrl(parsedHtml, action.getUrl());
245-
Log.v(TAG, "Action " + (i + 1) + "/" + actions.size() + " -- Sending Request to url: " + url);
245+
Log.v(TAG, "--- Action " + (i + 1) + "/" + actions.size() + " -- Sending Request to url: " + url);
246246

247247
// make request with data, cookies, current method
248248
getRequestData(requestData, action.getActionParams());
@@ -321,15 +321,15 @@ public Map<String, String> scrapeMultipleTables(Map<String, TransformerMapping>
321321
* @param url url as string
322322
* @throws IOException if there is an error connecting to the url
323323
*/
324-
private void makeJsoupRequest(Map<String, String> requestData, Connection.Method method, String url) throws IOException {
324+
private void makeJsoupRequest(Map<String, String> requestData, Connection.Method method, String url) throws IOException, URISyntaxException {
325325
Connection.Response response = Jsoup.connect(url)
326326
.data(requestData)
327327
.cookies(cookies)
328328
.referrer(previousUrl) // some websites block without referrer
329329
.userAgent(Config.BROWSER_USER_AGENT) // set explicit user agent
330330
.method(method)
331331
.timeout(Config.SCRAPER_TIMEOUT)
332-
//.validateTLSCertificates(false) // do not validate ssl certificates -> must be used for self certified
332+
.followRedirects(false)
333333
.execute();
334334

335335
// get cookies from response and add to all cookies
@@ -342,6 +342,13 @@ private void makeJsoupRequest(Map<String, String> requestData, Connection.Method
342342
document.select("script").remove();
343343
document.select("td:contains(aktuellen ECTS-Grades)").remove(); // remove invalid html (see error #71)
344344

345+
// check for location redirect
346+
String location = response.header("location");
347+
if (location != null) {
348+
baseUri = new URL(location).toURI();
349+
makeJsoupRequest(new HashMap<String, String>(), Connection.Method.GET, location);
350+
}
351+
345352
// check for meta refresh tag
346353
Element meta = document.select("meta[http-equiv=Refresh").first();
347354
if (meta != null) {

0 commit comments

Comments
 (0)