Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Use different proxy, cleanup no modrinth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverAndro committed Apr 17, 2023
1 parent 1e231d4 commit 1aa829c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import kotlin.time.Duration.Companion.milliseconds

object CurseforgeAPI : APIInterface() {
override val ratelimit = Ratelimit(ModifoldArgs.args.curseforgeSpeed.milliseconds, true)
const val root = "https://api.cfwidget.com"
const val root = "https://api.curse.tools/v1/cf"

private val cache = mutableMapOf<Int, CurseforgeProject?>()

fun getProjectData(id: Int): CurseforgeProject? {
return cache.computeIfAbsent(id) {
try {
getWithoutAuth<ProjectWrapper>("$root/$id").data
getWithoutAuth<ProjectWrapper>("$root/mods/$id").data
} catch (ignored: Exception) {
ignored.printStackTrace()
null
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/com/github/p03w/modifold/NoModrinthFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import com.github.p03w.modifold.cli.ModifoldArgs
import com.github.p03w.modifold.cli.ModifoldArgsContainer.*
import com.github.p03w.modifold.cli.log
import com.github.p03w.modifold.cli.userUnderstandsUsageAlternative
import com.github.p03w.modifold.core.collectCurseforgeProjects
import com.github.p03w.modifold.core.getFilesToTransfer
import com.github.p03w.modifold.core.getInfoToSaveLocally
import com.github.p03w.modifold.core.transferProjectFiles
import com.github.p03w.modifold.core.*
import kotlin.system.exitProcess

fun noModrinthFlow() {
Expand All @@ -30,7 +27,7 @@ fun noModrinthFlow() {
val toTransfer = getFilesToTransfer()

log("Beginning file \"transfer\"")
transferProjectFiles(mutableMapOf(), toSave, toTransfer, true)
backupProjectFiles(curseforgeProjects, toSave, toTransfer)

log("Done!")
}
167 changes: 98 additions & 69 deletions src/main/kotlin/com/github/p03w/modifold/core/TransferProjectFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ package com.github.p03w.modifold.core
import com.github.p03w.modifold.cli.*
import com.github.p03w.modifold.curseforge_api.CurseforgeAPI
import com.github.p03w.modifold.curseforge_schema.CurseforgeProject
import com.github.p03w.modifold.modrinth_schema.ModrinthProject
import com.github.p03w.modifold.modrinth_api.ModrinthAPI
import com.github.p03w.modifold.modrinth_schema.ModrinthProject
import java.io.File
import java.net.URL
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.time.Instant
import java.util.*

private fun File.make(): File {
mkdirs()
return this
}

fun transferProjectFiles(
mapping: MutableMap<CurseforgeProject, ModrinthProject>,
toSave: EnumSet<InformationToSave>,
toTransfer: FileSet,
dummy: Boolean = false
toTransfer: FileSet
) {
fun File.make(): File {
mkdirs()
return this
}

mapping.keys.forEach { project ->
val files = withSpinner("Collecting files for ${project.display()})") {
CurseforgeAPI.getProjectFiles(project.id, toTransfer == FileSet.ALL) {
Expand All @@ -36,80 +35,110 @@ fun transferProjectFiles(
}
}

if (!dummy) {
val modrinthProject = ModrinthAPI.getProjectInfo(mapping[project]!!.id)
val modrinthProject = ModrinthAPI.getProjectInfo(mapping[project]!!.id)

// Save logo
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${project.logo.url} as project_icon.png")
val localCopy = File("ModifoldSaved/${project.display()}/images/project_icon.png").make()
Files.copy(URL(project.logo.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
// Save logo
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${project.logo.url} as project_icon.png")
val localCopy = File("ModifoldSaved/${project.display()}/images/project_icon.png").make()
Files.copy(URL(project.logo.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}

// Transfer screenshots
project.screenshots.forEach nextAttach@{
withSpinner("Transferring ${it.title} to gallery") { spinner ->
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${it.url} as ${it.title}.${it.getExt()}")
val localCopy =
File("ModifoldSaved/${project.display()}/images/${it.title}.${it.getExt()}").make()
Files.copy(URL(it.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
try {
ModrinthAPI.addProjectImage(modrinthProject, it)
} catch (err: Throwable) {
log("Failed to add ${it.title} to gallery! ${err.localizedMessage}".error().toString())
spinner.fail()
}
// Transfer screenshots
project.screenshots.forEach nextAttach@{
withSpinner("Transferring ${it.title} to gallery") { spinner ->
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${it.url} as ${it.title}.${it.getExt()}")
val localCopy = File("ModifoldSaved/${project.display()}/images/${it.title}.${it.getExt()}").make()
Files.copy(URL(it.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
try {
ModrinthAPI.addProjectImage(modrinthProject, it)
} catch (err: Throwable) {
log("Failed to add ${it.title} to gallery! ${err.localizedMessage}".error().toString())
spinner.fail()
}
}

files.forEach { file ->
withSpinner("Transferring ${file.fileName}") { spinner ->
val buffered = CurseforgeAPI.getFileStream(file).buffered()
}

val stream = if (toSave.contains(InformationToSave.VERSIONS)) {
debug("Saving version to disk")
val localCopy = File("ModifoldSaved/${project.display()}/versions/${file.fileName}").make()
Files.copy(buffered, localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
localCopy.inputStream().buffered()
} else {
buffered
}
files.forEach { file ->
withSpinner("Transferring ${file.fileName}") { spinner ->
val buffered = CurseforgeAPI.getFileStream(file).buffered()

try {
ModrinthAPI.makeProjectVersion(
modrinthProject,
file,
stream,
project
)
} catch (err: Throwable) {
log("Failed to upload ${file.fileName}! ${err.localizedMessage}".error().toString())
spinner.fail()
}
val stream = if (toSave.contains(InformationToSave.VERSIONS)) {
debug("Saving version to disk")
val localCopy = File("ModifoldSaved/${project.display()}/versions/${file.fileName}").make()
Files.copy(buffered, localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
localCopy.inputStream().buffered()
} else {
buffered
}

try {
ModrinthAPI.makeProjectVersion(
modrinthProject,
file,
stream,
project
)
} catch (err: Throwable) {
log("Failed to upload ${file.fileName}! ${err.localizedMessage}".error().toString())
spinner.fail()
}
}
println()
} else {
// Save logo
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${project.logo.url} as project_icon.png")
val localCopy = File("ModifoldSaved/${project.display()}/images/project_icon.png").make()
Files.copy(URL(project.logo.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
println()
}
}

fun backupProjectFiles(
projects: List<CurseforgeProject>,
toSave: EnumSet<InformationToSave>,
toTransfer: FileSet
) {
projects.forEach { project ->
val files = withSpinner("Collecting files for ${project.display()})") {
CurseforgeAPI.getProjectFiles(project.id, toTransfer == FileSet.ALL) {
error("Could not get curseforge files for project ${project.display()}")
}.sortedBy { Instant.parse(it.fileDate) }
}.let {
if (ModifoldArgs.args.fileLimit > -1) {
it.takeLast(ModifoldArgs.args.fileLimit)
} else {
it
}
}

// Save logo
if (toSave.contains(InformationToSave.IMAGES)) {
debug("Saving ${project.logo.url} as project_icon.png")
val localCopy = File("ModifoldSaved/${project.display()}/images/project_icon.png").make()
Files.copy(URL(project.logo.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}

// Transfer screenshots
// Transfer screenshots
project.screenshots.forEach nextAttach@{
if (toSave.contains(InformationToSave.IMAGES)) {
project.screenshots.forEach {
withSpinner("Saving gallery image ${it.title}") { spinner ->
debug("Saving ${it.url} as ${it.title}.${it.getExt()}")
val localCopy =
File("ModifoldSaved/${project.display()}/images/${it.title}.${it.getExt()}").make()
Files.copy(URL(it.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
withSpinner("Saving gallery image ${it.title}") { _ ->
debug("Saving ${it.url} as ${it.title}.${it.getExt()}")
val localCopy = File("ModifoldSaved/${project.display()}/images/${it.title}.${it.getExt()}").make()
Files.copy(URL(it.url).openStream(), localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
}
}
}

files.forEach { file ->
if (toSave.contains(InformationToSave.VERSIONS)) {
withSpinner("Saving ${file.fileName}") { _ ->
debug("Saving version ${file.fileName} to disk")
val buffered = CurseforgeAPI.getFileStream(file).buffered()
val localCopy = File("ModifoldSaved/${project.display()}/versions/${file.fileName}").make()
Files.copy(buffered, localCopy.toPath(), StandardCopyOption.REPLACE_EXISTING)
localCopy.inputStream().buffered()
}
}
}
println()
}
}
}

0 comments on commit 1aa829c

Please sign in to comment.