forked from openrs2/openrs2
parent
b2ccbad031
commit
3a067b8b9c
@ -0,0 +1,31 @@ |
|||||||
|
package org.openrs2.archive.key |
||||||
|
|
||||||
|
import kotlinx.coroutines.Dispatchers |
||||||
|
import kotlinx.coroutines.future.await |
||||||
|
import kotlinx.coroutines.withContext |
||||||
|
import org.openrs2.crypto.XteaKey |
||||||
|
import org.openrs2.http.checkStatusCode |
||||||
|
import java.net.URI |
||||||
|
import java.net.http.HttpClient |
||||||
|
import java.net.http.HttpRequest |
||||||
|
import java.net.http.HttpResponse |
||||||
|
|
||||||
|
public abstract class JsonKeyDownloader( |
||||||
|
private val client: HttpClient, |
||||||
|
private val jsonKeyReader: JsonKeyReader |
||||||
|
) : KeyDownloader { |
||||||
|
override suspend fun download(url: String): Sequence<XteaKey> { |
||||||
|
val request = HttpRequest.newBuilder(URI(url)) |
||||||
|
.GET() |
||||||
|
.build() |
||||||
|
|
||||||
|
val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()).await() |
||||||
|
response.checkStatusCode() |
||||||
|
|
||||||
|
return withContext(Dispatchers.IO) { |
||||||
|
response.body().use { input -> |
||||||
|
jsonKeyReader.read(input) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,38 +1,19 @@ |
|||||||
package org.openrs2.archive.key |
package org.openrs2.archive.key |
||||||
|
|
||||||
import kotlinx.coroutines.Dispatchers |
|
||||||
import kotlinx.coroutines.future.await |
|
||||||
import kotlinx.coroutines.withContext |
|
||||||
import org.openrs2.crypto.XteaKey |
|
||||||
import org.openrs2.http.checkStatusCode |
|
||||||
import java.net.URI |
|
||||||
import java.net.http.HttpClient |
import java.net.http.HttpClient |
||||||
import java.net.http.HttpRequest |
|
||||||
import java.net.http.HttpResponse |
|
||||||
import javax.inject.Inject |
import javax.inject.Inject |
||||||
import javax.inject.Singleton |
import javax.inject.Singleton |
||||||
|
|
||||||
@Singleton |
@Singleton |
||||||
public class OpenOsrsKeyDownloader @Inject constructor( |
public class OpenOsrsKeyDownloader @Inject constructor( |
||||||
private val client: HttpClient, |
client: HttpClient, |
||||||
private val jsonKeyReader: JsonKeyReader |
jsonKeyReader: JsonKeyReader |
||||||
) : KeyDownloader { |
) : JsonKeyDownloader(client, jsonKeyReader) { |
||||||
override suspend fun download(): Sequence<XteaKey> { |
override suspend fun getMissingUrls(seenUrls: Set<String>): Set<String> { |
||||||
val request = HttpRequest.newBuilder(ENDPOINT) |
return setOf(ENDPOINT) |
||||||
.GET() |
|
||||||
.build() |
|
||||||
|
|
||||||
val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()).await() |
|
||||||
response.checkStatusCode() |
|
||||||
|
|
||||||
return withContext(Dispatchers.IO) { |
|
||||||
response.body().use { input -> |
|
||||||
jsonKeyReader.read(input) |
|
||||||
} |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
private companion object { |
private companion object { |
||||||
private val ENDPOINT = URI("https://xtea.openosrs.dev/get") |
private const val ENDPOINT = "https://xtea.openosrs.dev/get" |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -0,0 +1,48 @@ |
|||||||
|
package org.openrs2.archive.key |
||||||
|
|
||||||
|
import kotlinx.coroutines.Dispatchers |
||||||
|
import kotlinx.coroutines.future.await |
||||||
|
import kotlinx.coroutines.withContext |
||||||
|
import org.jsoup.Jsoup |
||||||
|
import org.openrs2.http.charset |
||||||
|
import org.openrs2.http.checkStatusCode |
||||||
|
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 |
||||||
|
|
||||||
|
@Singleton |
||||||
|
public class PolarKeyDownloader @Inject constructor( |
||||||
|
private val client: HttpClient, |
||||||
|
jsonKeyReader: JsonKeyReader |
||||||
|
) : JsonKeyDownloader(client, jsonKeyReader) { |
||||||
|
override suspend fun getMissingUrls(seenUrls: Set<String>): Set<String> { |
||||||
|
val request = HttpRequest.newBuilder(ENDPOINT) |
||||||
|
.GET() |
||||||
|
.build() |
||||||
|
|
||||||
|
val response = client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()).await() |
||||||
|
response.checkStatusCode() |
||||||
|
|
||||||
|
val document = withContext(Dispatchers.IO) { |
||||||
|
Jsoup.parse(response.body(), response.charset?.name(), ENDPOINT.toString()) |
||||||
|
} |
||||||
|
|
||||||
|
val urls = mutableSetOf<String>() |
||||||
|
|
||||||
|
for (element in document.select("a")) { |
||||||
|
val url = element.absUrl("href") |
||||||
|
if (url.endsWith(".json") && url !in seenUrls) { |
||||||
|
urls += url |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return urls |
||||||
|
} |
||||||
|
|
||||||
|
private companion object { |
||||||
|
private val ENDPOINT = URI("https://archive.runestats.com/osrs/xtea/") |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue