96 lines
3.3 KiB
Kotlin
96 lines
3.3 KiB
Kotlin
|
/**
|
||
|
* ProjectLaogai
|
||
|
*
|
||
|
* Copyright 2019 <seil0@mosad.xyz>
|
||
|
*
|
||
|
* This program is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation; either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
|
* MA 02110-1301, USA.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
package org.mosad.seil0.projectlaogai.controller
|
||
|
|
||
|
import android.content.Context
|
||
|
import org.jetbrains.anko.doAsync
|
||
|
import org.json.JSONObject
|
||
|
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.coursesCacheTime
|
||
|
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.mensaCacheTime
|
||
|
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.timetableCacheTime
|
||
|
import java.io.*
|
||
|
import java.net.URL
|
||
|
|
||
|
class TCoRAPIController(cont: Context) {
|
||
|
private val context = cont
|
||
|
|
||
|
/**
|
||
|
* get the json object from tcor api and write it as file (cache)
|
||
|
*/
|
||
|
fun getCoursesList() = doAsync {
|
||
|
val url = URL("https://tcor.mosad.xyz/courses")
|
||
|
val file = File(context.filesDir, "courses.json")
|
||
|
|
||
|
// 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()
|
||
|
|
||
|
// 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() = doAsync {
|
||
|
val url = URL("https://tcor.mosad.xyz/mensamenu")
|
||
|
val file = File(context.filesDir, "mensa.json")
|
||
|
|
||
|
// 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()
|
||
|
|
||
|
// 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) = doAsync {
|
||
|
val url = URL("https://tcor.mosad.xyz/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())
|
||
|
|
||
|
// 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)
|
||
|
}
|
||
|
}
|