diff --git a/API-Curseforge/src/main/kotlin/com.github.p03w.modifold.curseforge_api/CurseforgeAPI.kt b/API-Curseforge/src/main/kotlin/com.github.p03w.modifold.curseforge_api/CurseforgeAPI.kt index a607744..c33822f 100644 --- a/API-Curseforge/src/main/kotlin/com.github.p03w.modifold.curseforge_api/CurseforgeAPI.kt +++ b/API-Curseforge/src/main/kotlin/com.github.p03w.modifold.curseforge_api/CurseforgeAPI.kt @@ -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() fun getProjectData(id: Int): CurseforgeProject? { return cache.computeIfAbsent(id) { try { - getWithoutAuth("$root/$id").data + getWithoutAuth("$root/mods/$id").data } catch (ignored: Exception) { ignored.printStackTrace() null diff --git a/src/main/kotlin/com/github/p03w/modifold/NoModrinthFlow.kt b/src/main/kotlin/com/github/p03w/modifold/NoModrinthFlow.kt index 4a4c242..bb2c444 100644 --- a/src/main/kotlin/com/github/p03w/modifold/NoModrinthFlow.kt +++ b/src/main/kotlin/com/github/p03w/modifold/NoModrinthFlow.kt @@ -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() { @@ -30,7 +27,7 @@ fun noModrinthFlow() { val toTransfer = getFilesToTransfer() log("Beginning file \"transfer\"") - transferProjectFiles(mutableMapOf(), toSave, toTransfer, true) + backupProjectFiles(curseforgeProjects, toSave, toTransfer) log("Done!") } \ No newline at end of file diff --git a/src/main/kotlin/com/github/p03w/modifold/core/TransferProjectFiles.kt b/src/main/kotlin/com/github/p03w/modifold/core/TransferProjectFiles.kt index e5780d6..47fb5d2 100644 --- a/src/main/kotlin/com/github/p03w/modifold/core/TransferProjectFiles.kt +++ b/src/main/kotlin/com/github/p03w/modifold/core/TransferProjectFiles.kt @@ -3,8 +3,8 @@ 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 @@ -12,17 +12,16 @@ import java.nio.file.StandardCopyOption import java.time.Instant import java.util.* +private fun File.make(): File { + mkdirs() + return this +} + fun transferProjectFiles( mapping: MutableMap, toSave: EnumSet, - 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) { @@ -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, + toSave: EnumSet, + 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() } -} +} \ No newline at end of file