Skip to content

Instantly share code, notes, and snippets.

@2BAB
Created January 25, 2021 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2BAB/940d60e69a82bfb1e94edb6ec79baae4 to your computer and use it in GitHub Desktop.
Save 2BAB/940d60e69a82bfb1e94edb6ec79baae4 to your computer and use it in GitHub Desktop.
Blog Images Replacement
@file:DependsOn("com.squareup.okhttp3:okhttp:4.9.0")
import okhttp3.*
import okhttp3.internal.closeQuietly
import okio.IOException
import java.io.File
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicInteger
// Please specify the post directory, static-host blog usually puts all posts in one dir like "./_post"
val dirPath = "xxx/_posts"
// Please specify the regex to filter your images, you can use an easy one or take from https://stackoverflow.com/questions/44227270/regex-to-parse-image-link-in-markdown
val regexString = "http://xxx.com/blog/.+(?=\\?imageslim)"
val inputDir = File(dirPath)
val outputDir = File(inputDir.parent, "fetched_images")
outputDir.mkdir()
var fileCount = 0
val allImages = mutableListOf<String>()
inputDir.walk().forEach {
if (it.extension == "md") {
println("processing ${it.name}")
allImages.addAll(extractImages(it))
fileCount++
}
}
println("processed $fileCount files")
println("adding ${allImages.size} image download tasks")
val countDown = CountDownLatch(allImages.size)
var successDownloaded = AtomicInteger(0)
val client = OkHttpClient()
allImages.forEach {
val fileName = it.split("/").last()
downloadImage(client, it, fileName)
}
countDown.await()
println("Done, successfully downloaded ${successDownloaded.get()}")
System.exit(0)
fun extractImages(mdFile: File): List<String> {
val content = mdFile.bufferedReader().use { it.readText() }
val regex = Regex(regexString)
return regex.findAll(content).map { it.value }.toList()
}
fun downloadImage(client: OkHttpClient, url: String, fileName: String) {
val req = Request.Builder().url(url).build()
client.newCall(req).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
println("onFailure: $fileName")
countDown.countDown()
}
override fun onResponse(call: Call, response: Response) {
val inputStream = response.body!!.byteStream()
val output = File(outputDir, fileName)
output.createNewFile()
output.outputStream().use { inputStream.copyTo(it) }
successDownloaded.incrementAndGet()
println("onSuccess: $fileName")
response.closeQuietly()
countDown.countDown()
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment