Add support for Prever

This commit is contained in:
Matte23
2021-02-15 19:52:34 +01:00
parent 0a414b0dbc
commit 24d51aaeec
12 changed files with 244 additions and 15 deletions

View File

@@ -25,6 +25,7 @@ abstract class Server(
val ktorClient: HttpClient
) {
abstract val serverID: Int
open val idsAreHumanReadable = true
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>

View File

@@ -24,12 +24,15 @@ import net.underdesk.circolapp.shared.PlatformDispatcher
import net.underdesk.circolapp.shared.data.Circular
import net.underdesk.circolapp.shared.server.curie.CurieServer
import net.underdesk.circolapp.shared.server.porporato.PorporatoServer
import net.underdesk.circolapp.shared.server.prever.PreverAgrarioServer
import net.underdesk.circolapp.shared.server.prever.PreverAlberghieroServer
class ServerAPI(serverName: Servers) {
private val ktorClient = KtorFactory().createClient()
private var server: Server
fun serverID(): Int = server.serverID
fun idsAreHumanReadable() = server.idsAreHumanReadable
init {
server = createServer(serverName, ktorClient)
@@ -61,7 +64,7 @@ class ServerAPI(serverName: Servers) {
companion object {
enum class Servers {
CURIE, PORPORATO
CURIE, PORPORATO, PREVER_AGRARIO, PREVER_ALBERGHIERO
}
enum class Result {
@@ -85,16 +88,22 @@ class ServerAPI(serverName: Servers) {
fun getServerName(server: Servers) = when (server) {
Servers.CURIE -> "Liceo scientifico Maria Curie"
Servers.PORPORATO -> "Liceo G.F. Porporato"
Servers.PREVER_AGRARIO -> "I.I.S. Arturo Prever - Agrario"
Servers.PREVER_ALBERGHIERO -> "I.I.S. Arturo Prever - Alberghiero"
}
fun getServerWebsite(server: Servers) = when (server) {
Servers.CURIE -> "https://www.curiepinerolo.edu.it/"
Servers.PORPORATO -> "https://www.liceoporporato.edu.it/"
Servers.PREVER_AGRARIO -> "https://www.prever.edu.it/agrario/"
Servers.PREVER_ALBERGHIERO -> "https://www.prever.edu.it/alberghiero/"
}
fun createServer(server: Servers, ktorClient: HttpClient) = when (server) {
Servers.CURIE -> CurieServer(ktorClient)
Servers.PORPORATO -> PorporatoServer(ktorClient)
Servers.PREVER_AGRARIO -> PreverAgrarioServer(ktorClient)
Servers.PREVER_ALBERGHIERO -> PreverAlberghieroServer(ktorClient)
}
}
}

View File

@@ -0,0 +1,34 @@
/*
* Circolapp
* Copyright (C) 2019-2021 Matteo Schiff
*
* 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, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.shared.server.pojo
import kotlinx.serialization.Serializable
@Serializable
data class Post(
val id: Long,
val date: String,
val link: String,
val title: Title
)
@Serializable
data class Title(
val rendered: String
)

View File

@@ -0,0 +1,107 @@
/*
* Circolapp
* Copyright (C) 2019-2021 Matteo Schiff
*
* 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, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.shared.server.prever
import io.ktor.client.*
import io.ktor.client.features.*
import io.ktor.client.request.*
import io.ktor.utils.io.errors.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.underdesk.circolapp.shared.data.Circular
import net.underdesk.circolapp.shared.server.Server
import net.underdesk.circolapp.shared.server.ServerAPI
import net.underdesk.circolapp.shared.server.pojo.Post
import kotlin.coroutines.cancellation.CancellationException
abstract class GenericPreverServer(ktorClient: HttpClient) : Server(ktorClient) {
abstract val categoryId: Int
override val idsAreHumanReadable = false
override suspend fun getCircularsFromServer()
: Pair<List<Circular>, ServerAPI.Companion.Result> {
return try {
val list = arrayListOf<Circular>()
var page = 1
var newCircularsInPage: List<Circular>
do {
newCircularsInPage = parsePage(page)
list.addAll(newCircularsInPage)
page++
} while (newCircularsInPage.size >= 99)
list.sortByDescending { it.id }
Pair(list, ServerAPI.Companion.Result.SUCCESS)
} catch (exception: IOException) {
Pair(emptyList(), ServerAPI.Companion.Result.NETWORK_ERROR)
} catch (exception: Exception) {
Pair(emptyList(), ServerAPI.Companion.Result.GENERIC_ERROR)
}
}
@OptIn(ExperimentalStdlibApi::class)
@Throws(IOException::class, CancellationException::class)
private suspend fun parsePage(page: Int): List<Circular> {
val posts = retrievePageFromServer(page)
return withContext(Dispatchers.Default) {
val list = arrayListOf<Circular>()
for (post in posts) {
list.add(generateFromString(post.id, post.title.rendered, post.date, post.link))
}
list
}
}
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)
}
@OptIn(ExperimentalStdlibApi::class)
@Throws(IOException::class, CancellationException::class)
private suspend fun retrievePageFromServer(page: Int): List<Post> {
return try {
ktorClient.get(getEndpointUrl(page))
} catch (ex: ClientRequestException) {
emptyList()
}
}
private fun generateFromString(id: Long, string: String, date: String, url: String): Circular {
val title = string.replace("&#8211;", "-")
val dateList = date.split("T")[0].split("-")
val realDate = dateList[2] + "/" + dateList[1] + "/" + dateList[0]
return Circular(id, serverID, title, url, url, realDate)
}
private fun getEndpointUrl(page: Int) =
"https://www.prever.edu.it/wp-json/wp/v2/posts?_fields=id,date,link,title&categories=${categoryId}&per_page=100&page=${page}&after=2020-09-01T00:00:00Z"
}

View File

@@ -0,0 +1,27 @@
/*
* Circolapp
* Copyright (C) 2019-2021 Matteo Schiff
*
* 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, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.shared.server.prever
import io.ktor.client.*
import net.underdesk.circolapp.shared.server.ServerAPI
class PreverAgrarioServer(ktorClient: HttpClient) : GenericPreverServer(ktorClient) {
override val categoryId = 476
override val serverID = ServerAPI.getServerId(ServerAPI.Companion.Servers.PREVER_AGRARIO)
}

View File

@@ -0,0 +1,27 @@
/*
* Circolapp
* Copyright (C) 2019-2021 Matteo Schiff
*
* 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, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.shared.server.prever
import io.ktor.client.*
import net.underdesk.circolapp.shared.server.ServerAPI
class PreverAlberghieroServer(ktorClient: HttpClient) : GenericPreverServer(ktorClient) {
override val categoryId = 94
override val serverID = ServerAPI.getServerId(ServerAPI.Companion.Servers.PREVER_ALBERGHIERO)
}