some more logging and version 0.4.99 (RC1)
This commit is contained in:
@ -23,6 +23,7 @@
|
||||
package org.mosad.seil0.projectlaogai.controller
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import org.jetbrains.anko.doAsync
|
||||
import org.json.JSONObject
|
||||
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.coursesCacheTime
|
||||
@ -32,69 +33,87 @@ import java.io.BufferedWriter
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.net.URL
|
||||
import kotlin.Exception
|
||||
|
||||
class TCoRAPIController {
|
||||
|
||||
companion object {
|
||||
private const val className = "TCoRAPIController"
|
||||
private const val tcorBaseURL = "https://tcor.mosad.xyz"
|
||||
|
||||
/**
|
||||
* get the json object from tcor api and write it as file (cache)
|
||||
*/
|
||||
fun getCoursesList(context: Context) = doAsync {
|
||||
val url = URL("https://tcor.mosad.xyz/courseList")
|
||||
val file = File(context.filesDir, "courses.json")
|
||||
try {
|
||||
val url = URL("$tcorBaseURL/courseList")
|
||||
val file = File(context.filesDir, "courses.json")
|
||||
|
||||
// read data from the API
|
||||
val coursesObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
|
||||
// read data from the API
|
||||
val coursesObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
|
||||
|
||||
// write the json object to a file
|
||||
val writer = BufferedWriter(FileWriter(file))
|
||||
writer.write(coursesObject.toString())
|
||||
writer.close()
|
||||
// write the json object to a file
|
||||
val writer = BufferedWriter(FileWriter(file))
|
||||
writer.write(coursesObject.toString())
|
||||
writer.close()
|
||||
|
||||
// update cache time
|
||||
coursesCacheTime = System.currentTimeMillis() / 1000
|
||||
PreferencesController.save(context)
|
||||
} catch (ex: Exception) {
|
||||
Log.e(className, "failed to get /courseList", ex)
|
||||
}
|
||||
|
||||
// update cache time
|
||||
coursesCacheTime = System.currentTimeMillis() / 1000
|
||||
PreferencesController.save(context)
|
||||
}
|
||||
|
||||
/**
|
||||
* get the json object from tcor api and write it as file (cache)
|
||||
*/
|
||||
fun getMensa(context: Context) = doAsync {
|
||||
val url = URL("https://tcor.mosad.xyz/mensamenu")
|
||||
val file = File(context.filesDir, "mensa.json")
|
||||
try {
|
||||
val url = URL("$tcorBaseURL/mensamenu")
|
||||
val file = File(context.filesDir, "mensa.json")
|
||||
|
||||
// read data from the API
|
||||
val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
|
||||
// read data from the API
|
||||
val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
|
||||
|
||||
// write the json object to a file
|
||||
val writer = BufferedWriter(FileWriter(file))
|
||||
writer.write(mensaObject.toString())
|
||||
writer.close()
|
||||
// write the json object to a file
|
||||
val writer = BufferedWriter(FileWriter(file))
|
||||
writer.write(mensaObject.toString())
|
||||
writer.close()
|
||||
|
||||
// update cache time
|
||||
mensaCacheTime = System.currentTimeMillis() / 1000
|
||||
PreferencesController.save(context)
|
||||
} catch (ex: Exception) {
|
||||
Log.e(className, "failed to get /mensamenu", ex)
|
||||
}
|
||||
|
||||
// update cache time
|
||||
mensaCacheTime = System.currentTimeMillis() / 1000
|
||||
PreferencesController.save(context)
|
||||
}
|
||||
|
||||
/**
|
||||
* get the json object from tcor api and write it as file (cache)
|
||||
*/
|
||||
fun getTimetable(courseName: String, week: Int, context: Context) = doAsync {
|
||||
val url = URL("https://tcor.mosad.xyz/timetable?courseName=$courseName&week=$week")
|
||||
val file = File(context.filesDir, "timetable-$courseName-$week.json")
|
||||
try {
|
||||
val url = URL("$tcorBaseURL/timetable?courseName=$courseName&week=$week")
|
||||
val file = File(context.filesDir, "timetable-$courseName-$week.json")
|
||||
|
||||
// read data from the API
|
||||
val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
|
||||
// read data from the API
|
||||
val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
|
||||
|
||||
// write the json object to a file
|
||||
val writer = BufferedWriter(FileWriter(file))
|
||||
writer.write(mensaObject.toString())
|
||||
writer.close()
|
||||
// write the json object to a file
|
||||
val writer = BufferedWriter(FileWriter(file))
|
||||
writer.write(mensaObject.toString())
|
||||
writer.close()
|
||||
|
||||
// update cache time
|
||||
timetableCacheTime = System.currentTimeMillis() / 1000
|
||||
PreferencesController.save(context)
|
||||
} catch (ex: Exception) {
|
||||
Log.e(className, "failed to get /timetable", ex)
|
||||
}
|
||||
|
||||
// update cache time
|
||||
timetableCacheTime = System.currentTimeMillis() / 1000
|
||||
PreferencesController.save(context)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user