- try making a factorio mod portal api client, for auto releasing
- make webmapTileDir a configurable variable
This commit is contained in:
parent
ec1f1bc200
commit
7141d7e5f7
16 changed files with 287 additions and 34 deletions
37
modules/factorio-mod-portal-client/build.gradle.kts
Normal file
37
modules/factorio-mod-portal-client/build.gradle.kts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
plugins {
|
||||
dev.adamko.kafkatorio.lang.`kotlin-jvm`
|
||||
kotlin("plugin.serialization")
|
||||
application
|
||||
}
|
||||
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation(projects.modules.eventsServer)
|
||||
|
||||
implementation(platform(libs.kotlinx.serialization.bom))
|
||||
implementation(libs.kotlinx.serialization.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
|
||||
implementation(libs.kotlinx.coroutines)
|
||||
|
||||
implementation(platform(libs.http4k.bom))
|
||||
implementation(libs.bundles.http4k)
|
||||
implementation(libs.http4k.format.kotlinx)
|
||||
|
||||
implementation(libs.skrapeit)
|
||||
}
|
||||
|
||||
//
|
||||
//application {
|
||||
// mainClass.set("dev.adamko.kafkatorio.processor.EventProcessorKt")
|
||||
//}
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
kotlinOptions.freeCompilerArgs += listOf(
|
||||
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
|
||||
"-opt-in=kotlinx.coroutines.FlowPreview",
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package dev.adamko.kafkatorio
|
||||
|
||||
import dev.adamko.kafkatorio.processor.config.env
|
||||
import dev.adamko.kafkatorio.processor.config.fromEnvVarPath
|
||||
import dev.adamko.kafkatorio.processor.config.fromResourcesFile
|
||||
import java.time.Clock
|
||||
import org.http4k.cloudnative.env.Environment
|
||||
import org.http4k.cloudnative.env.EnvironmentKey
|
||||
import org.http4k.cloudnative.env.Secret
|
||||
import org.http4k.lens.nonEmptyString
|
||||
import org.http4k.lens.secret
|
||||
|
||||
|
||||
/**
|
||||
* Load properties, in descending priority order, from
|
||||
*
|
||||
* 1. Environment variables
|
||||
* 2. This app's env file (as defined by the environment variable [Lenses.APPLICATION_PROPERTIES_FILE_PATH])
|
||||
* # 3. A resources-dir yaml file
|
||||
*
|
||||
*/
|
||||
internal class ApplicationProperties(
|
||||
vararg environments: Environment = arrayOf(
|
||||
Environment.env(),
|
||||
Environment.fromEnvVarPath(),
|
||||
Environment.fromResourcesFile(".secret.config.yml")
|
||||
)
|
||||
) {
|
||||
|
||||
private val appProps: Environment =
|
||||
environments.reduce { acc, environment -> acc.overrides(environment) }
|
||||
|
||||
|
||||
fun clock(): Clock = Clock.systemUTC()
|
||||
|
||||
|
||||
val factorioModPortalUsername: String
|
||||
get() = EnvironmentKey
|
||||
.nonEmptyString()
|
||||
.required("factorio-mod-portal.username")[appProps]
|
||||
|
||||
|
||||
val factorioModPortalPassword: Secret
|
||||
get() = EnvironmentKey
|
||||
.secret()
|
||||
.required("factorio-mod-portal.password")[appProps]
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
package dev.adamko.kafkatorio
|
||||
|
||||
import dev.adamko.kafkatorio.processor.serdes.KXS.asPrettyJsonString
|
||||
import dev.adamko.kafkatorio.processor.serdes.jsonMapper
|
||||
import it.skrape.core.htmlDocument
|
||||
import it.skrape.selects.html5.input
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import org.http4k.client.OkHttp
|
||||
import org.http4k.core.HttpHandler
|
||||
import org.http4k.core.Method
|
||||
import org.http4k.core.Request
|
||||
import org.http4k.core.Response
|
||||
import org.http4k.core.body.form
|
||||
import org.http4k.core.cookie.Cookie
|
||||
import org.http4k.core.cookie.cookie
|
||||
import org.http4k.core.cookie.cookies
|
||||
import org.http4k.core.then
|
||||
import org.http4k.filter.ClientFilters
|
||||
|
||||
|
||||
/**
|
||||
* * https://github.com/shanemadden/factorio-mod-portal-publish/blob/master/entrypoint.sh
|
||||
* * https://github.com/nicolas-lang/factorio-create-release-action/blob/master/entrypoint.py
|
||||
* * https://github.com/nicolas-lang/factorio-create-release-action/blob/master/factorioModPortal.py
|
||||
* * https://github.com/justarandomgeek/vscode-factoriomod-debug/blob/master/src/ModPackageProvider.ts#L530
|
||||
*
|
||||
*
|
||||
*/
|
||||
internal class FactorioModPortalClient(
|
||||
private val appProps: ApplicationProperties = ApplicationProperties(),
|
||||
private val loginFormUrl: String = "https://factorio.com/login?mods=1",
|
||||
private val loginUrl: String = "https://factorio.com/login",
|
||||
) {
|
||||
|
||||
private val client: HttpHandler =
|
||||
ClientFilters.FollowRedirects()
|
||||
.then(OkHttp())
|
||||
|
||||
private val csrfCookie: Cookie by lazy { fetchCsrfToken() }
|
||||
private val sessionCookie: Cookie by lazy { login() }
|
||||
|
||||
|
||||
private fun fetchCsrfToken(): Cookie {
|
||||
|
||||
val request = Request(Method.GET, loginFormUrl)
|
||||
val response: Response = client(request)
|
||||
|
||||
val csrfToken = htmlDocument(response.bodyString()) {
|
||||
input {
|
||||
withId = csrfTokenName
|
||||
withAttribute = "name" to csrfTokenName
|
||||
findFirst { attribute("value") }
|
||||
}
|
||||
}
|
||||
|
||||
println("csrfToken: $csrfToken")
|
||||
return Cookie(csrfTokenName, csrfToken)
|
||||
}
|
||||
|
||||
|
||||
private fun login(): Cookie {
|
||||
|
||||
val loginRequest = Request(Method.POST, loginUrl)
|
||||
.header("Referer", loginFormUrl)
|
||||
// .cookie(csrfCookie)
|
||||
.form("csrf_token", csrfCookie.value)
|
||||
.form("username_or_email", appProps.factorioModPortalUsername)
|
||||
.form("password", appProps.factorioModPortalPassword.use { it })
|
||||
.form("next_url", "/profile")
|
||||
|
||||
val loginResponse = client(loginRequest)
|
||||
|
||||
println(loginResponse.bodyString())
|
||||
|
||||
val sessionCookie = loginResponse.cookies()
|
||||
.first { it.name == "session" && it.domain == ".factorio.com" }
|
||||
|
||||
println("sessionCookie: $sessionCookie")
|
||||
|
||||
return sessionCookie
|
||||
}
|
||||
|
||||
fun getModInfo(modName: String): JsonObject {
|
||||
val response = client(
|
||||
Request(Method.GET, "https://mods.factorio.com/api/mods/${modName}/full")
|
||||
.cookie(csrfCookie)
|
||||
.cookie(sessionCookie)
|
||||
)
|
||||
return jsonMapper.parseToJsonElement(response.bodyString()).jsonObject
|
||||
}
|
||||
|
||||
fun fetchModUploadToken(modName: String): String {
|
||||
val response = client(
|
||||
Request(Method.GET, "https://mods.factorio.com/mod/${modName}/edit")
|
||||
.cookie(csrfCookie)
|
||||
.cookie(sessionCookie)
|
||||
)
|
||||
|
||||
return response.bodyString()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val csrfTokenName = "csrf_token"
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package dev.adamko.kafkatorio
|
||||
|
||||
import dev.adamko.kafkatorio.processor.serdes.KXS.asPrettyJsonString
|
||||
|
||||
|
||||
private const val modName = "kafkatorio-events"
|
||||
|
||||
|
||||
fun main() {
|
||||
val client = FactorioModPortalClient()
|
||||
// val modInfo = client.getModInfo(modName)
|
||||
// println(modInfo.asPrettyJsonString())
|
||||
println(client.fetchModUploadToken(modName))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue