automatically reacts to offers :D

This commit is contained in:
KoenDR06 2025-09-16 23:42:53 +02:00
parent 207dce7113
commit 09c3d8cf37
3 changed files with 73 additions and 45 deletions

Binary file not shown.

View file

@ -15,11 +15,13 @@ data class ReactableOffer(
val floor: Floor
)
fun main() {
print("Authenticating")
fun main(args: Array<String>) {
val headless = args.size == 1 && args[0] == "headless"
if (!headless) print("Authenticating")
val sessionToken = auth()
print("\rGetting rooms ")
if (!headless) print("\rGetting rooms ")
val rooms = getRooms().filter { room ->
room.unitType == config.general.unitType
}
@ -36,7 +38,7 @@ fun main() {
val coupled = rooms.mapNotNull { room ->
val offer: Offer? = offers.find { room.wocasId.toInt() == it.eenheidNummer.toInt() }
print("\rFiltering per room: ${index++} / ${rooms.size}")
if (!headless) print("\rFiltering per room: ${index++} / ${rooms.size}")
if (offer == null) null else ReactableOffer(room, offer, getFloorInfo(room, sessionToken))
}.filter {
val gender = it.floor.floorInfo.genderPreference
@ -44,13 +46,21 @@ fun main() {
val smoking = it.floor.floorInfo.smokingAllowed ?: true
val pets = it.floor.floorInfo.petsAllowed ?: false
val date = it.room.expireBy.take(10)
val date1 = LocalDate.now()
val date2 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
val days = ChronoUnit.DAYS.between(date1, date2)
((gender == "female" && config.gender.female) ||
(gender == "male" && config.gender.male) ||
(gender == "none" && config.gender.none)) &&
((config.general.smoking == -1 && !smoking) || (config.general.smoking == 1 && smoking) || config.general.smoking == 0) &&
((config.general.pets == -1 && !pets) || (config.general.pets == 1 && pets) || config.general.pets == 0)
((config.general.pets == -1 && !pets) || (config.general.pets == 1 && pets) || config.general.pets == 0) &&
days == 0L
}.sortedBy {
it.floor.potentialPosition
}.sortedBy {
@ -61,47 +71,55 @@ fun main() {
ChronoUnit.DAYS.between(date1, date2)
}
val fileName = "offers.md"
val out = File(fileName)
if (!out.exists()) out.createNewFile()
if (!headless) {
val fileName = "offers.md"
val out = File(fileName)
if (!out.exists()) out.createNewFile()
val str = StringBuilder()
val str = StringBuilder()
coupled.forEach {
val address = it.offer.adres[0]
coupled.forEach {
val address = it.offer.adres[0]
str.append("## [${address.straatnaam} ${address.nummer}, ${address.plaats.lowercase().replaceFirstChar { if (it.isLowerCase()) it - 32 else it }}](https://sshxl.nl/nl/aanbod/${it.room.flowId}-${address.straatnaam.lowercase().replace(" ", "-")})\n")
str.append("## [${address.straatnaam} ${address.nummer}, ${address.plaats.lowercase().replaceFirstChar { if (it.isLowerCase()) it - 32 else it }}](https://sshxl.nl/nl/aanbod/${it.room.flowId}-${address.straatnaam.lowercase().replace(" ", "-")})\n")
str.append("\n| Categorie | Waarde |\n")
str.append("|-------------|--------------------|\n")
str.append("\n| Categorie | Waarde |\n")
str.append("|-------------|--------------------|\n")
str.append("| Huisgenoten | ${(it.room.numberOfRooms-1).toString().padEnd(18, ' ')} |\n")
str.append("| Huisgenoten | ${(it.room.numberOfRooms-1).toString().padEnd(18, ' ')} |\n")
val genderString = when (it.floor.floorInfo.genderPreference) {
"none" -> "Geen voorkeur"
"male" -> "Man"
"female" -> "Vrouw"
else -> it.floor.floorInfo.genderPreference
val genderString = when (it.floor.floorInfo.genderPreference) {
"none" -> "Geen voorkeur"
"male" -> "Man"
"female" -> "Vrouw"
else -> it.floor.floorInfo.genderPreference
}
str.append("| Geslacht | ${genderString.padEnd(18, ' ')} |\n")
str.append("| Roken | ${(if (it.floor.floorInfo.smokingAllowed ?: true) "✅ Mag" else "❌ Mag niet").padEnd(17, ' ')} |\n")
str.append("| Huisdieren | ${(if (it.floor.floorInfo.petsAllowed ?: false) "✅ Mogen" else "❌ Mogen niet").padEnd(17, ' ')} |\n")
val positionString = "${it.floor.potentialPosition} / ${it.floor.applicantCount}."
str.append("| Reacties | ${positionString.padEnd(18, ' ')} |\n")
val date = it.room.expireBy.take(10)
val date1 = LocalDate.now()
val date2 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
val daysLeft = ChronoUnit.DAYS.between(date1, date2)
str.append("| Tijd over | $daysLeft dagen over. |\n")
str.append("\n")
str.append("### Message: \n\n${it.floor.floorInfo.description ?: "Deze pannekoeken hebben geen bericht achtergelaten"}\n")
str.append("\n\n")
}
out.writeText(str.toString())
println("\r${coupled.size} offers found, wrote to $fileName")
} else {
coupled.filter {
it.floor.potentialPosition <= 20
}.forEach {
postEndpoint("reactions/${it.room.flowId}", mapOf(), sessionToken)
}
str.append("| Geslacht | ${genderString.padEnd(18, ' ')} |\n")
str.append("| Roken | ${(if (it.floor.floorInfo.smokingAllowed ?: true) "✅ Mag" else "❌ Mag niet").padEnd(17, ' ')} |\n")
str.append("| Huisdieren | ${(if (it.floor.floorInfo.petsAllowed ?: false) "✅ Mogen" else "❌ Mogen niet").padEnd(17, ' ')} |\n")
val positionString = "${it.floor.potentialPosition} / ${it.floor.applicantCount}."
str.append("| Reacties | ${positionString.padEnd(18, ' ')} |\n")
val date = it.room.expireBy.take(10)
val date1 = LocalDate.now()
val date2 = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
val daysLeft = ChronoUnit.DAYS.between(date1, date2)
str.append("| Tijd over | $daysLeft dagen over. |\n")
str.append("\n")
str.append("### Message: \n\n${it.floor.floorInfo.description ?: "Deze pannekoeken hebben geen bericht achtergelaten"}\n")
str.append("\n\n")
}
out.writeText(str.toString())
println("\r${coupled.size} offers found, wrote to $fileName")
}

View file

@ -4,13 +4,15 @@ import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import kotlinx.serialization.json.Json
import me.koendev.utils.println
/**
* @param path the endpoint to GET. `sshxl.nl/api/v1/<PATH>`
*
* @return a HttpRequest with all the necessary properties to GET the endpoint.
*/
fun buildRequest(path: String, session: String): HttpRequest {
fun buildRequest(path: String, session: String): HttpRequest.Builder {
return HttpRequest.newBuilder()
.uri(URI("https://www.sshxl.nl/api/v1/$path"))
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0")
@ -24,8 +26,6 @@ fun buildRequest(path: String, session: String): HttpRequest {
"SSHContext=$session"
).joinToString("; ")
)
.GET()
.build()
}
fun getEndpoint(endpoint: String, session: String = ""): String {
@ -35,5 +35,15 @@ fun getEndpoint(endpoint: String, session: String = ""): String {
val request = buildRequest(endpoint, session)
return client.send(request, HttpResponse.BodyHandlers.ofString()).body()
return client.send(request.GET().build(), HttpResponse.BodyHandlers.ofString()).body()
}
fun postEndpoint(endpoint: String, data: Map<String, String>, session: String = ""): String {
val client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.build()
val request = buildRequest(endpoint, session)
return client.send(request.POST(HttpRequest.BodyPublishers.ofString(Json.encodeToString(data))).build(), HttpResponse.BodyHandlers.ofString()).println().body()
}