mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-05 23:19:10 +00:00
Automatically resolve circular's direct URL
This commit is contained in:
@@ -25,5 +25,18 @@ import com.squareup.sqldelight.db.SqlDriver
|
||||
actual class DatabaseDriverFactory(private val context: Context) {
|
||||
actual fun createDriver(): SqlDriver {
|
||||
return AndroidSqliteDriver(AppDatabase.Schema, context, "circolapp.db")
|
||||
.also {
|
||||
var currentVer = DatabaseFactory.getVersion(it)
|
||||
val schemaVer: Int = AppDatabase.Schema.version
|
||||
|
||||
if (currentVer == 0) {
|
||||
currentVer = 1
|
||||
}
|
||||
|
||||
if (schemaVer > currentVer) {
|
||||
AppDatabase.Schema.migrate(it, currentVer, schemaVer)
|
||||
DatabaseFactory.setVersion(it, schemaVer)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,14 +29,26 @@ actual class SpecificCurieServer actual constructor(private val curieServer: Cur
|
||||
val list = ArrayList<Circular>()
|
||||
|
||||
htmlList.forEach { element ->
|
||||
val url = element.attr("href")
|
||||
if (element.parents().size == 6) {
|
||||
list.last().attachmentsNames.add(element.text())
|
||||
list.last().attachmentsUrls.add(element.attr("href"))
|
||||
list.last().attachmentsUrls.add(url)
|
||||
|
||||
if (url.endsWith(".pdf")) {
|
||||
list.last().realAttachmentsUrls.add(url)
|
||||
} else {
|
||||
list.last().realAttachmentsUrls.add("")
|
||||
}
|
||||
} else if (element.parents().size == 4) {
|
||||
list.add(curieServer.generateFromString(element.text(), element.attr("href")))
|
||||
list.add(curieServer.generateFromString(element.text(), url))
|
||||
}
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
actual fun parseFileUrl(string: String): String {
|
||||
val document = Jsoup.parseBodyFragment(string)
|
||||
return document.getElementsByClass("mtli_attachment")[0].attr("href")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,10 +23,12 @@ data class Circular(
|
||||
val school: Int,
|
||||
val name: String,
|
||||
val url: String,
|
||||
val realUrl: String?,
|
||||
val date: String,
|
||||
var favourite: Boolean = false,
|
||||
var reminder: Boolean = false,
|
||||
var read: Boolean = false,
|
||||
val attachmentsNames: MutableList<String> = mutableListOf(),
|
||||
val attachmentsUrls: MutableList<String> = mutableListOf()
|
||||
val attachmentsUrls: MutableList<String> = mutableListOf(),
|
||||
val realAttachmentsUrls: MutableList<String> = mutableListOf()
|
||||
)
|
||||
|
||||
@@ -34,18 +34,20 @@ class CircularDao(
|
||||
private val appDatabaseQueries = database.appDatabaseQueries
|
||||
|
||||
private val circularMapper =
|
||||
{ id: Long, school: Long, name: String, url: String, date: String, favourite: Long, reminder: Long, read: Long, attachmentsNames: String, attachmentsUrls: String ->
|
||||
{ id: Long, school: Long, name: String, url: String, date: String, favourite: Long, reminder: Long, read: Long, attachmentsNames: String, attachmentsUrls: String, realAttachmentsUrls: String, realUrl: String? ->
|
||||
Circular(
|
||||
id,
|
||||
school.toInt(),
|
||||
name,
|
||||
url,
|
||||
realUrl,
|
||||
date,
|
||||
favourite.toBoolean(),
|
||||
reminder.toBoolean(),
|
||||
read.toBoolean(),
|
||||
attachmentsNames.toList(),
|
||||
attachmentsUrls.toList()
|
||||
attachmentsUrls.toList(),
|
||||
realAttachmentsUrls.toList()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -61,7 +63,9 @@ class CircularDao(
|
||||
it.reminder.toLong(),
|
||||
it.read.toLong(),
|
||||
it.attachmentsNames.joinToString(),
|
||||
it.attachmentsUrls.joinToString()
|
||||
it.attachmentsUrls.joinToString(),
|
||||
it.realAttachmentsUrls.joinToString(),
|
||||
it.realUrl
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -76,6 +80,24 @@ class CircularDao(
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun setRealUrl(id: Long, school: Int, url: String?) =
|
||||
withContext(PlatformDispatcher.IO) {
|
||||
appDatabaseQueries.setRealUrl(
|
||||
url,
|
||||
id,
|
||||
school.toLong()
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun setRealAttachmentsUrls(id: Long, school: Int, urls: List<String>) =
|
||||
withContext(PlatformDispatcher.IO) {
|
||||
appDatabaseQueries.setRealAttachmentsUrls(
|
||||
urls.joinToString(),
|
||||
id,
|
||||
school.toLong()
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun markRead(id: Long, school: Int, read: Boolean) =
|
||||
withContext(PlatformDispatcher.IO) {
|
||||
appDatabaseQueries.markCircularRead(
|
||||
|
||||
@@ -57,4 +57,34 @@ class CircularRepository(
|
||||
}
|
||||
return Pair(onlyNewCirculars, errorCode)
|
||||
}
|
||||
|
||||
suspend fun getRealUrl(rawUrl: String, id: Long, school: Int): String {
|
||||
val result = serverAPI.getRealUrl(rawUrl)
|
||||
|
||||
if (result.second != ServerAPI.Companion.Result.SUCCESS)
|
||||
return rawUrl
|
||||
|
||||
circularDao.setRealUrl(id, school, result.first)
|
||||
return result.first
|
||||
}
|
||||
|
||||
suspend fun getRealUrlForAttachment(
|
||||
index: Int,
|
||||
rawUrls: List<String>,
|
||||
realUrls: List<String>,
|
||||
id: Long,
|
||||
school: Int
|
||||
): List<String> {
|
||||
val result = serverAPI.getRealUrl(rawUrls[index])
|
||||
|
||||
if (result.second != ServerAPI.Companion.Result.SUCCESS)
|
||||
return realUrls
|
||||
|
||||
val newList =
|
||||
if (realUrls.size != rawUrls.size) MutableList(rawUrls.size) { "" } else realUrls.toMutableList()
|
||||
newList[index] = result.first
|
||||
|
||||
circularDao.setRealAttachmentsUrls(id, school, newList)
|
||||
return newList
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,16 @@ import com.squareup.sqldelight.db.SqlDriver
|
||||
|
||||
object DatabaseFactory {
|
||||
fun createDatabase(sqlDriver: SqlDriver) = AppDatabase(sqlDriver)
|
||||
|
||||
fun getVersion(driver: SqlDriver): Int {
|
||||
val sqlCursor = driver.executeQuery(null, "PRAGMA user_version;", 0, null)
|
||||
if (!sqlCursor.next())
|
||||
return 0
|
||||
|
||||
return sqlCursor.getLong(0)?.toInt() ?: 0
|
||||
}
|
||||
|
||||
fun setVersion(driver: SqlDriver, version: Int) {
|
||||
driver.execute(null, "PRAGMA user_version = $version;", 0, null)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,5 +26,6 @@ abstract class Server(
|
||||
) {
|
||||
abstract val serverID: Int
|
||||
abstract suspend fun getCircularsFromServer(): Pair<List<Circular>, ServerAPI.Companion.Result>
|
||||
abstract suspend fun getRealUrl(rawUrl: String): Pair<String, ServerAPI.Companion.Result>
|
||||
abstract suspend fun newCircularsAvailable(): Pair<Boolean, ServerAPI.Companion.Result>
|
||||
}
|
||||
|
||||
@@ -50,6 +50,11 @@ class ServerAPI(serverName: Servers) {
|
||||
server.getCircularsFromServer()
|
||||
}
|
||||
|
||||
suspend fun getRealUrl(rawUrl: String): Pair<String, Result> =
|
||||
withContext(PlatformDispatcher.IO) {
|
||||
server.getRealUrl(rawUrl)
|
||||
}
|
||||
|
||||
fun changeServer(serverName: Servers) {
|
||||
server = createServer(serverName, ktorClient)
|
||||
}
|
||||
|
||||
@@ -46,6 +46,23 @@ class CurieServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getRealUrl(rawUrl: String): Pair<String, ServerAPI.Companion.Result> {
|
||||
if (rawUrl.endsWith(".pdf"))
|
||||
return Pair(rawUrl, ServerAPI.Companion.Result.SUCCESS)
|
||||
|
||||
return try {
|
||||
withContext(Dispatchers.Default) {
|
||||
val html: String = ktorClient.get(rawUrl)
|
||||
val realUrl = SpecificCurieServer(this@CurieServer).parseFileUrl(html)
|
||||
Pair(realUrl, ServerAPI.Companion.Result.SUCCESS)
|
||||
}
|
||||
} catch (exception: IOException) {
|
||||
Pair(rawUrl, ServerAPI.Companion.Result.NETWORK_ERROR)
|
||||
} catch (exception: Exception) {
|
||||
Pair(rawUrl, ServerAPI.Companion.Result.GENERIC_ERROR)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun newCircularsAvailable(): Pair<Boolean, ServerAPI.Companion.Result> {
|
||||
return Pair(true, ServerAPI.Companion.Result.SUCCESS)
|
||||
}
|
||||
@@ -61,6 +78,8 @@ class CurieServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
"""(\d+)""".toRegex()
|
||||
val idMatcher = idRegex.find(string)
|
||||
|
||||
val realUrl = if (url.endsWith(".pdf")) url else null
|
||||
|
||||
val id = idMatcher?.value?.toLong() ?: -1L
|
||||
|
||||
val dateRegex =
|
||||
@@ -75,9 +94,9 @@ class CurieServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
.removePrefix("_")
|
||||
.removePrefix(" ")
|
||||
|
||||
Circular(id, serverID, title, url, dateMatcher.value)
|
||||
Circular(id, serverID, title, url, realUrl, dateMatcher.value)
|
||||
} else {
|
||||
Circular(id, serverID, title, url, "")
|
||||
Circular(id, serverID, title, url, realUrl, "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,4 +107,5 @@ class CurieServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
|
||||
expect class SpecificCurieServer(curieServer: CurieServer) {
|
||||
fun parseHtml(string: String): List<Circular>
|
||||
fun parseFileUrl(string: String): String
|
||||
}
|
||||
|
||||
@@ -67,6 +67,10 @@ class PorporatoServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getRealUrl(rawUrl: String): Pair<String, ServerAPI.Companion.Result> {
|
||||
return Pair(rawUrl, ServerAPI.Companion.Result.SUCCESS)
|
||||
}
|
||||
|
||||
override suspend fun newCircularsAvailable(): Pair<Boolean, ServerAPI.Companion.Result> {
|
||||
return Pair(true, ServerAPI.Companion.Result.SUCCESS)
|
||||
}
|
||||
@@ -85,6 +89,7 @@ class PorporatoServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
val parent = list.find { it.id == attachment.id && !it.name.startsWith("All") }
|
||||
parent?.attachmentsNames?.add(attachment.name)
|
||||
parent?.attachmentsUrls?.add(attachment.url)
|
||||
parent?.realAttachmentsUrls?.add(attachment.url)
|
||||
|
||||
return@removeAll true
|
||||
}
|
||||
@@ -101,6 +106,7 @@ class PorporatoServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
val parent = list[lastIndex]
|
||||
parent.attachmentsNames.add(attachment.name)
|
||||
parent.attachmentsUrls.add(attachment.url)
|
||||
parent.realAttachmentsUrls.add(attachment.url)
|
||||
|
||||
return@removeAll true
|
||||
}
|
||||
@@ -146,9 +152,9 @@ class PorporatoServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
title = title.removeRange(dateMatcher.range)
|
||||
.removeSuffix(" (pubb.: )")
|
||||
|
||||
Circular(id, serverID, title, fullUrl, dateMatcher.value.replace("-", "/"))
|
||||
Circular(id, serverID, title, fullUrl, fullUrl, dateMatcher.value.replace("-", "/"))
|
||||
} else {
|
||||
Circular(id, serverID, title, fullUrl, "")
|
||||
Circular(id, serverID, title, fullUrl, fullUrl, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE Circulars ADD COLUMN realAttachmentsUrls TEXT NOT NULL DEFAULT "";
|
||||
ALTER TABLE Circulars ADD COLUMN realUrl TEXT;
|
||||
@@ -9,18 +9,30 @@ CREATE TABLE Circulars (
|
||||
read INTEGER NOT NULL DEFAULT 0,
|
||||
attachmentsNames TEXT NOT NULL,
|
||||
attachmentsUrls TEXT NOT NULL,
|
||||
realAttachmentsUrls TEXT NOT NULL,
|
||||
realUrl TEXT,
|
||||
PRIMARY KEY (id, school)
|
||||
);
|
||||
|
||||
insertCircular:
|
||||
INSERT OR IGNORE INTO Circulars(id, school, name, url, date, favourite, reminder, read, attachmentsNames, attachmentsUrls)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
|
||||
INSERT OR IGNORE INTO Circulars(id, school, name, url, date, favourite, reminder, read, attachmentsNames, attachmentsUrls, realAttachmentsUrls, realUrl)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
|
||||
|
||||
updateCircular:
|
||||
UPDATE Circulars
|
||||
SET favourite = ?, reminder = ?
|
||||
WHERE id = ? AND school = ?;
|
||||
|
||||
setRealUrl:
|
||||
UPDATE Circulars
|
||||
SET realUrl = ?
|
||||
WHERE id = ? AND school = ?;
|
||||
|
||||
setRealAttachmentsUrls:
|
||||
UPDATE Circulars
|
||||
SET realAttachmentsUrls = ?
|
||||
WHERE id = ? AND school = ?;
|
||||
|
||||
markCircularRead:
|
||||
UPDATE Circulars
|
||||
SET read = ?
|
||||
|
||||
@@ -24,5 +24,18 @@ import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
|
||||
actual class DatabaseDriverFactory {
|
||||
actual fun createDriver(): SqlDriver {
|
||||
return NativeSqliteDriver(AppDatabase.Schema, "circolapp.db")
|
||||
.also {
|
||||
var currentVer = DatabaseFactory.getVersion(it)
|
||||
val schemaVer: Int = AppDatabase.Schema.version
|
||||
|
||||
if (currentVer == 0) {
|
||||
currentVer = 1
|
||||
}
|
||||
|
||||
if (schemaVer > currentVer) {
|
||||
AppDatabase.Schema.migrate(it, currentVer, schemaVer)
|
||||
DatabaseFactory.setVersion(it, schemaVer)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,14 +30,25 @@ actual class SpecificCurieServer actual constructor(private val curieServer: Cur
|
||||
val list = ArrayList<Circular>()
|
||||
|
||||
htmlList?.forEach { element ->
|
||||
val url = element.attributes.objectForKey("href").toString()
|
||||
if (element.parentElement?.parentElement?.parentElement?.tagName == "li") {
|
||||
list.last().attachmentsNames.add(element.textContent)
|
||||
list.last().attachmentsUrls.add(element.attributes.objectForKey("href").toString())
|
||||
list.last().attachmentsUrls.add(url)
|
||||
|
||||
if (url.endsWith(".pdf")) {
|
||||
list.last().realAttachmentsUrls.add(url)
|
||||
} else {
|
||||
list.last().realAttachmentsUrls.add("")
|
||||
}
|
||||
} else {
|
||||
list.add(curieServer.generateFromString(element.textContent, element.attributes.objectForKey("href").toString()))
|
||||
list.add(curieServer.generateFromString(element.textContent, url))
|
||||
}
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
actual fun parseFileUrl(string: String): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,27 +25,18 @@ actual class DatabaseDriverFactory(private val path: String) {
|
||||
actual fun createDriver(): SqlDriver {
|
||||
return JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY + path)
|
||||
.also {
|
||||
val currentVer = getVersion(it)
|
||||
val currentVer = DatabaseFactory.getVersion(it)
|
||||
if (currentVer == 0) {
|
||||
AppDatabase.Schema.create(it)
|
||||
setVersion(it, 1)
|
||||
DatabaseFactory.setVersion(it, 1)
|
||||
} else {
|
||||
val schemaVer: Int = AppDatabase.Schema.version
|
||||
if (schemaVer > currentVer) {
|
||||
AppDatabase.Schema.migrate(it, currentVer, schemaVer)
|
||||
setVersion(it, schemaVer)
|
||||
DatabaseFactory.setVersion(it, schemaVer)
|
||||
println("init: migrated from $currentVer to $schemaVer")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getVersion(driver: SqlDriver): Int {
|
||||
val sqlCursor = driver.executeQuery(null, "PRAGMA user_version;", 0, null)
|
||||
return sqlCursor.getLong(0)!!.toInt()
|
||||
}
|
||||
|
||||
private fun setVersion(driver: SqlDriver, version: Int) {
|
||||
driver.execute(null, String.format("PRAGMA user_version = %d;", version), 0, null)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user