zoukankan      html  css  js  c++  java
  • 网易云音乐本地数据库的歌单列表导出,一个不丢,支持1000首以上

    主要代码,kotlin写的

    package fuck
    
    import com.google.gson.Gson
    import java.io.File
    import java.sql.Connection
    import java.sql.DriverManager
    import java.sql.Statement
    
    fun main(args: Array<String>) {
        if (args.size < 2) {
            println("Usage: java -jar xxx.jar dbFilePath outputPath")
            return
        }
    
        val dbFilePath = args[0]
        val savePath = args[1]
    
        initDB(dbFilePath)
    
        getPlayLists()?.forEach { playLists ->
            getSonsByPid("${playLists.id}")?.forEach {
                val fromJson = Gson().fromJson<JsonRootBean>(it, JsonRootBean::class.java)
                var arts = ""
                for (line in fromJson.artists) {
                    arts += line.name + ","
                }
                val path = "$savePath${playLists.name.replace("/", "-")}.txt"
                File(savePath).mkdirs()
                File(path).appendText(fromJson.name + " - " + arts + "
    ")
            }
        }
    
        closeDB()
        println("导出完毕")
    }
    
    var c: Connection? = null
    var stmt: Statement? = null
    
    fun initDB(dbFilePath: String) {
        Class.forName("org.sqlite.JDBC")
        c = DriverManager.getConnection("jdbc:sqlite:$dbFilePath")
        stmt = c?.createStatement()
        println("Opened database successfully")
    }
    
    fun getPlayLists(): List<PlayListBean>? {
        val rs = stmt?.executeQuery("SELECT playlist FROM web_playlist;") ?: return null
    
        val pids = mutableListOf<PlayListBean>()
        while (rs.next()) {
            pids.add(Gson().fromJson(rs.getString(1), PlayListBean::class.java))
        }
        return pids
    }
    
    fun getSonsByPid(pid: String): List<String>? {
        val sql = "SELECT track from web_track WHERE tid in " +
                "(SELECT tid from web_playlist_track WHERE pid = '$pid');"
        val songs = mutableListOf<String>()
        val rs = stmt?.executeQuery(sql) ?: return null
        while (rs.next()) {
            songs.add(rs.getString(1))
        }
        return songs
    }
    
    fun closeDB() {
        stmt?.close()
        c?.close()
    }

    仓库地址

    https://github.com/xirtam-ch/NeteaseMusicDBExport

  • 相关阅读:
    kotlin中值范围
    kotlin中集合
    kotlin数据解构
    Java 8 Lambda 表达式
    kotlin 之内联函数
    kotlin之函数的范围和泛型函数
    kotlin函数的参数和返回值
    kotlin 之单表达式函数
    kotlin使用中辍标记法调用函数
    kotlin之函数的基本用法
  • 原文地址:https://www.cnblogs.com/xirtam/p/12564109.html
Copyright © 2011-2022 走看看