Skip to content

Commit 80dfde6

Browse files
committed
Optimized scraper and fixed bug with missing some pages
1 parent 43f7952 commit 80dfde6

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

app/src/main/java/nz/zhang/lecturerecordingplayer/canvasscraper/CanvasScraper.kt

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import okhttp3.Request
1111
import okhttp3.Response
1212
import okhttp3.logging.HttpLoggingInterceptor
1313
import okhttp3.logging.HttpLoggingInterceptor.Level.BASIC
14+
import java.net.SocketTimeoutException
1415
import java.util.regex.Pattern
1516

1617
class CanvasScraper constructor(private val authCookie: String, private val listener: ScraperListener) {
@@ -61,8 +62,11 @@ class CanvasScraper constructor(private val authCookie: String, private val list
6162
}
6263

6364
private fun parseForLectureUrls(apiUrl: HttpUrl) {
64-
// Don't visit pages more than once
65-
if (apiUrl in visitedUrls) {
65+
// Don't visit pages more than once, or try to download files or users
66+
if (apiUrl in visitedUrls ||
67+
apiUrl.toString().endsWith("download?download_frd=1") ||
68+
apiUrl.toString().contains("/files/") ||
69+
apiUrl.toString().contains("/users/")) {
6670
return
6771
}
6872
visitedUrls += apiUrl
@@ -118,11 +122,14 @@ class CanvasScraper constructor(private val authCookie: String, private val list
118122
for (propertyName in toCheck) {
119123
val propertyValue = jsonObject[propertyName]
120124
if (propertyValue != null && !propertyValue.isJsonNull) {
121-
if (propertyValue.asString.endsWith("/modules") || propertyValue.asString.endsWith("/items")) {
122-
visitCanvasJsonUrl(propertyValue.asString+"?per_page=100") // if we're in modules or items, read more
125+
if (propertyValue.asString.contains("https://")) { // if the URL is even a valid URL...
126+
if (propertyValue.asString.endsWith("/modules") || propertyValue.asString.endsWith("/items")) {
127+
visitCanvasJsonUrl(propertyValue.asString + "?per_page=100") // if we're in modules or items, read more
128+
} else {
129+
visitCanvasJsonUrl(propertyValue.asString) // read more pages
130+
}
131+
break
123132
}
124-
visitCanvasJsonUrl(propertyValue.asString) // read more pages
125-
break
126133
}
127134
}
128135
} else if (json.isJsonArray) {
@@ -180,15 +187,20 @@ class CanvasScraper constructor(private val authCookie: String, private val list
180187
private fun apiCall(apiUrl: HttpUrl): JsonElement {
181188
// Execute the request
182189
val request = Request.Builder().authenticate().url(apiUrl).build()
183-
val response = okHttpClient.newCall(request).execute()
184-
val validate = response.validate(contentType = "json")
185-
if (validate == null) {
186-
Log.e("CanvasScraper", "Malformed response")
187-
return JsonObject() // empty
190+
try {
191+
val response = okHttpClient.newCall(request).execute()
192+
val validate = response.validate(contentType = "json")
193+
if (validate == null) {
194+
Log.e("CanvasScraper", "Malformed response")
195+
return JsonObject() // empty
196+
}
197+
// Remove "while(1);" prefix that Canvas returns for whatever reason
198+
val removePrefix = response?.body()?.string()?.removePrefix("while(1);")
199+
if (removePrefix != null) return jsonParser.parse(removePrefix) else return JsonObject()
200+
} catch (e:SocketTimeoutException) {
201+
Log.e("CanvasScraper", "Request timed out")
202+
return JsonObject()
188203
}
189-
// Remove "while(1);" prefix that Canvas returns for whatever reason
190-
val removePrefix = response?.body()?.string()?.removePrefix("while(1);")
191-
if (removePrefix != null) return jsonParser.parse(removePrefix) else return JsonObject()
192204
}
193205

194206
private fun Request.Builder.authenticate(): Request.Builder {

0 commit comments

Comments
 (0)