Add command for downloading group names from RuneStar

Signed-off-by: Graham <gpe@openrs2.org>
bzip2
Graham 4 years ago
parent 3f8b8eec1a
commit 37d86af7c1
  1. 11
      archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt
  2. 14
      archive/src/main/kotlin/org/openrs2/archive/name/DownloadCommand.kt
  3. 1
      archive/src/main/kotlin/org/openrs2/archive/name/NameCommand.kt
  4. 5
      archive/src/main/kotlin/org/openrs2/archive/name/NameDownloader.kt
  5. 11
      archive/src/main/kotlin/org/openrs2/archive/name/NameImporter.kt
  6. 46
      archive/src/main/kotlin/org/openrs2/archive/name/RuneStarNameDownloader.kt

@ -6,6 +6,8 @@ import com.google.inject.multibindings.Multibinder
import org.openrs2.archive.key.KeyDownloader import org.openrs2.archive.key.KeyDownloader
import org.openrs2.archive.key.OpenOsrsKeyDownloader import org.openrs2.archive.key.OpenOsrsKeyDownloader
import org.openrs2.archive.key.RuneLiteKeyDownloader import org.openrs2.archive.key.RuneLiteKeyDownloader
import org.openrs2.archive.name.NameDownloader
import org.openrs2.archive.name.RuneStarNameDownloader
import org.openrs2.buffer.BufferModule import org.openrs2.buffer.BufferModule
import org.openrs2.cache.CacheModule import org.openrs2.cache.CacheModule
import org.openrs2.db.Database import org.openrs2.db.Database
@ -25,8 +27,11 @@ public object ArchiveModule : AbstractModule() {
.toProvider(DatabaseProvider::class.java) .toProvider(DatabaseProvider::class.java)
.`in`(Scopes.SINGLETON) .`in`(Scopes.SINGLETON)
val binder = Multibinder.newSetBinder(binder(), KeyDownloader::class.java) val keyBinder = Multibinder.newSetBinder(binder(), KeyDownloader::class.java)
binder.addBinding().to(OpenOsrsKeyDownloader::class.java) keyBinder.addBinding().to(OpenOsrsKeyDownloader::class.java)
binder.addBinding().to(RuneLiteKeyDownloader::class.java) keyBinder.addBinding().to(RuneLiteKeyDownloader::class.java)
val nameBinder = Multibinder.newSetBinder(binder(), NameDownloader::class.java)
nameBinder.addBinding().to(RuneStarNameDownloader::class.java)
} }
} }

@ -0,0 +1,14 @@
package org.openrs2.archive.name
import com.github.ajalt.clikt.core.CliktCommand
import com.google.inject.Guice
import kotlinx.coroutines.runBlocking
import org.openrs2.archive.ArchiveModule
public class DownloadCommand : CliktCommand(name = "download") {
override fun run(): Unit = runBlocking {
val injector = Guice.createInjector(ArchiveModule)
val importer = injector.getInstance(NameImporter::class.java)
importer.download()
}
}

@ -6,6 +6,7 @@ import com.github.ajalt.clikt.core.subcommands
public class NameCommand : NoOpCliktCommand(name = "name") { public class NameCommand : NoOpCliktCommand(name = "name") {
init { init {
subcommands( subcommands(
DownloadCommand(),
GenerateCommand(), GenerateCommand(),
ImportCommand() ImportCommand()
) )

@ -0,0 +1,5 @@
package org.openrs2.archive.name
public interface NameDownloader {
public suspend fun download(): Sequence<String>
}

@ -7,8 +7,17 @@ import javax.inject.Singleton
@Singleton @Singleton
public class NameImporter @Inject constructor( public class NameImporter @Inject constructor(
private val database: Database private val database: Database,
private val downloaders: Set<NameDownloader>
) { ) {
public suspend fun download() {
val names = mutableSetOf<String>()
for (downloader in downloaders) {
names += downloader.download()
}
import(names)
}
public suspend fun import(names: Iterable<String>) { public suspend fun import(names: Iterable<String>) {
database.execute { connection -> database.execute { connection ->
connection.prepareStatement( connection.prepareStatement(

@ -0,0 +1,46 @@
package org.openrs2.archive.name
import kotlinx.coroutines.future.await
import org.openrs2.http.checkStatusCode
import java.io.IOException
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.streams.asSequence
@Singleton
public class RuneStarNameDownloader @Inject constructor(
private val client: HttpClient
) : NameDownloader {
override suspend fun download(): Sequence<String> {
val names = readTsv(NAMES_ENDPOINT, 4)
val individualNames = readTsv(INDIVIDUAL_NAMES_ENDPOINT, 0)
return names + individualNames
}
private suspend fun readTsv(uri: URI, column: Int): Sequence<String> {
val request = HttpRequest.newBuilder(uri)
.GET()
.build()
val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofLines()).await()
response.checkStatusCode()
return response.body().map { line ->
val columns = line.split('\t')
if (column >= columns.size) {
throw IOException("Column out of range")
}
columns[column]
}.asSequence()
}
private companion object {
private val NAMES_ENDPOINT = URI("https://raw.githubusercontent.com/RuneStar/cache-names/master/names.tsv")
private val INDIVIDUAL_NAMES_ENDPOINT =
URI("https://raw.githubusercontent.com/RuneStar/cache-names/master/individual-names.tsv")
}
}
Loading…
Cancel
Save