Add ability to download icons (#1253)

This commit is contained in:
ligi 2022-06-13 06:53:43 +02:00 committed by GitHub
parent f761550d62
commit 9e35cab9b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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:${KOTLIN_VERSION}"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:${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.JsonAdapter
import com.squareup.moshi.Moshi 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 org.ethereum.lists.chains.model.Chain
import java.time.Duration
val mandatory_fields = listOf( val mandatory_fields = listOf(
"name", "name",
@ -28,3 +32,13 @@ val optionalFields = listOf(
val moshi: Moshi = Moshi.Builder().build() val moshi: Moshi = Moshi.Builder().build()
val chainAdapter: JsonAdapter<Chain> = moshi.adapter(Chain::class.java) 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 java.io.File
import com.beust.klaxon.JsonObject import com.beust.klaxon.JsonObject
import com.beust.klaxon.Klaxon 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.ethereum.lists.chains.model.*
import org.kethereum.erc55.isValid import org.kethereum.erc55.isValid
import org.kethereum.model.Address import org.kethereum.model.Address
import org.kethereum.rpc.HttpEthereumRPC import org.kethereum.rpc.HttpEthereumRPC
import java.time.Duration
val parsedShortNames = mutableSetOf<String>() val parsedShortNames = mutableSetOf<String>()
val parsedNames = mutableSetOf<String>() val parsedNames = mutableSetOf<String>()
@ -15,6 +20,7 @@ val parsedNames = mutableSetOf<String>()
val basePath = File("..") val basePath = File("..")
val dataPath = File(basePath, "_data") val dataPath = File(basePath, "_data")
val iconsPath = File(dataPath, "icons") val iconsPath = File(dataPath, "icons")
val iconsDownloadPath = File(dataPath, "iconsDownload")
val chainsPath = File(dataPath, "chains") val chainsPath = File(dataPath, "chains")
private val allFiles = chainsPath.listFiles() ?: error("${chainsPath.absolutePath} must contain the chain json files - but it does not") 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>) { fun main(args: Array<String>) {
doChecks(doRPCConnect = args.contains("rpcConnect")) doChecks(doRPCConnect = args.contains("rpcConnect"), doIconDownload = args.contains("iconDownload"))
createOutputFiles() createOutputFiles()
} }
@ -84,14 +89,14 @@ private fun createOutputFiles() {
File(buildPath, "CNAME").writeText("chainid.network") File(buildPath, "CNAME").writeText("chainid.network")
} }
private fun doChecks(doRPCConnect: Boolean) { private fun doChecks(doRPCConnect: Boolean, doIconDownload: Boolean) {
allChainFiles.forEach { allChainFiles.forEach {
checkChain(it, doRPCConnect) checkChain(it, doRPCConnect)
} }
val allIcons = iconsPath.listFiles() ?: return val allIcons = iconsPath.listFiles() ?: return
allIcons.forEach { allIcons.forEach {
checkIcon(it) checkIcon(it, doIconDownload)
} }
allFiles.filter { it.isDirectory }.forEach { _ -> allFiles.filter { it.isDirectory }.forEach { _ ->
@ -99,7 +104,7 @@ private fun doChecks(doRPCConnect: Boolean) {
} }
} }
fun checkIcon(icon: File) { fun checkIcon(icon: File, withIconDownload: Boolean) {
println("checking Icon " + icon.name) println("checking Icon " + icon.name)
val obj: JsonArray<*> = Klaxon().parseJsonArray(icon.reader()) val obj: JsonArray<*> = Klaxon().parseJsonArray(icon.reader())
println("found variants " + obj.size) println("found variants " + obj.size)
@ -114,6 +119,25 @@ fun checkIcon(icon: File) {
error("url must start with ipfs://") 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 width = it["width"]
val height = it["height"] val height = it["height"]
if (width != null || height != null) { if (width != null || height != null) {