Skip to content

Instantly share code, notes, and snippets.

@fgoinai
Created April 11, 2018 15:36
Show Gist options
  • Save fgoinai/57eef341b9e76dda96b5299b58beeefa to your computer and use it in GitHub Desktop.
Save fgoinai/57eef341b9e76dda96b5299b58beeefa to your computer and use it in GitHub Desktop.
// Require kotlinx.coroutines jar as dependency, handle it yourself
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.runBlocking
import java.io.FileOutputStream
import java.net.HttpURLConnection
import java.net.URL
import java.nio.channels.Channels
val base = "http://bms.bemaniso.ws/"
val file = { style: Int ->
"$base/files.php?group=iidx&style=$style"
}
fun main(args: Array<String>) {
val dest = "E:\\bmssong"
runBlocking {
(12..21).forEach {
val style = it
launch {
filterDlLink(get(file(it))).forEach {
launch {
download("$base$it", dest)
}
}
}.join()
}
}
}
fun filterDlLink(urls: List<String>) = urls
.filter { it.contains(".7z") }
.map { it.split(">")[0].split("\"")[1].replace("&amp;", "&") }
suspend fun get(url: String): List<String> {
val conn = URL(url).openConnection() as HttpURLConnection
conn.requestMethod = "GET"
if (conn.responseCode != 200) {
println(conn.errorStream.bufferedReader().readLines().reduce { x, y -> x + y })
}
return conn.inputStream.bufferedReader().readLines()
}
suspend fun download(url: String, dest: String) {
val conn = URL(url).openConnection() as HttpURLConnection
val fileName = url.split("&").filter { it.contains("file=") }[0].split("=")[1]
conn.requestMethod = "GET"
if (conn.responseCode != 200) {
println(conn.errorStream.bufferedReader().readLines().reduce { x, y -> x + y })
}
Channels.newChannel(conn.inputStream.buffered()).use {
val rbc = it
FileOutputStream("$dest/$fileName").use {
it.channel.transferFrom(rbc, 0, Long.MAX_VALUE)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment