Add ability to download icons

This commit is contained in:
ligi 2022-06-13 06:26:41 +02:00
parent 33b37d0113
commit 8bd6563b9a
No known key found for this signature in database
GPG Key ID: 8E81894010ABF23D
3 changed files with 74 additions and 34 deletions

View File

@ -18,5 +18,7 @@ dependencies {
testImplementation "org.jetbrains.kotlin:kotlin-test:${KOTLIN_VERSION}"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:${KOTLIN_VERSION}"
implementation 'com.github.ligi:ipfs-api-kotlin:0.15'
}

View File

@ -2,7 +2,11 @@ package org.ethereum.lists.chains
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import io.ipfs.kotlin.IPFS
import io.ipfs.kotlin.IPFSConfiguration
import okhttp3.OkHttpClient
import org.ethereum.lists.chains.model.Chain
import java.time.Duration
val mandatory_fields = listOf(
"name",
@ -28,3 +32,13 @@ val optionalFields = listOf(
val moshi: Moshi = Moshi.Builder().build()
val chainAdapter: JsonAdapter<Chain> = moshi.adapter(Chain::class.java)
val ipfs by lazy {
IPFS(
IPFSConfiguration(
"http://127.0.0.1:5001/api/v0/",
OkHttpClient.Builder().readTimeout(Duration.ofMinutes(1)).build(),
Moshi.Builder().build()
)
)
}

View File

@ -4,10 +4,15 @@ import com.beust.klaxon.JsonArray
import java.io.File
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Klaxon
import com.squareup.moshi.Moshi
import io.ipfs.kotlin.IPFS
import io.ipfs.kotlin.IPFSConfiguration
import okhttp3.OkHttpClient
import org.ethereum.lists.chains.model.*
import org.kethereum.erc55.isValid
import org.kethereum.model.Address
import org.kethereum.rpc.HttpEthereumRPC
import java.time.Duration
val parsedShortNames = mutableSetOf<String>()
val parsedNames = mutableSetOf<String>()
@ -15,6 +20,7 @@ val parsedNames = mutableSetOf<String>()
val basePath = File("..")
val dataPath = File(basePath, "_data")
val iconsPath = File(dataPath, "icons")
val iconsDownloadPath = File(dataPath, "iconsDownload")
val chainsPath = File(dataPath, "chains")
private val allFiles = chainsPath.listFiles() ?: error("${chainsPath.absolutePath} must contain the chain json files - but it does not")
@ -22,8 +28,7 @@ private val allChainFiles = allFiles.filter { !it.isDirectory }
fun main(args: Array<String>) {
doChecks(doRPCConnect = args.contains("rpcConnect"))
doChecks(doRPCConnect = args.contains("rpcConnect"), doIconDownload = args.contains("iconDownload"))
createOutputFiles()
}
@ -83,14 +88,14 @@ private fun createOutputFiles() {
File(buildPath, "CNAME").writeText("chainid.network")
}
private fun doChecks(doRPCConnect: Boolean) {
private fun doChecks(doRPCConnect: Boolean, doIconDownload: Boolean) {
allChainFiles.forEach {
checkChain(it, doRPCConnect)
}
val allIcons = iconsPath.listFiles() ?: return
allIcons.forEach {
checkIcon(it)
checkIcon(it, doIconDownload)
}
allFiles.filter { it.isDirectory }.forEach { _ ->
@ -98,7 +103,7 @@ private fun doChecks(doRPCConnect: Boolean) {
}
}
fun checkIcon(icon: File) {
fun checkIcon(icon: File, withIconDownload: Boolean) {
println("checking Icon " + icon.name)
val obj: JsonArray<*> = Klaxon().parseJsonArray(icon.reader())
println("found variants " + obj.size)
@ -113,6 +118,25 @@ fun checkIcon(icon: File) {
error("url must start with ipfs://")
}
if (withIconDownload) {
val iconCID = url.removePrefix("ipfs://")
try {
println("fetching Icon from IPFS $iconCID")
val iconBytes = ipfs.get.catBytes(iconCID)
println("Icon size" + iconBytes.size)
val outFile = File(iconsDownloadPath, iconCID)
outFile.createNewFile()
outFile.writeBytes(iconBytes)
} catch (e: Exception) {
println("could not fetch icon from IPFS")
}
}
val width = it["width"]
val height = it["height"]
if (width != null || height != null) {