fix csv import

This commit is contained in:
Bnyro 2022-08-08 14:46:38 +02:00
parent 6d1e691f1b
commit ed5dd02abf

View File

@ -15,52 +15,48 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.io.BufferedReader import java.io.BufferedReader
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.InputStream
import java.io.InputStreamReader import java.io.InputStreamReader
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
class ImportHelper( class ImportHelper(
private val activity: Activity private val activity: Activity
) { ) {
private val TAG = "ImportHelper" private val TAG = "ImportHelper"
/**
* Import subscriptions by a file uri
*/
fun importSubscriptions(uri: Uri?) { fun importSubscriptions(uri: Uri?) {
if (uri == null) return if (uri == null) return
try { try {
val type = activity.contentResolver.getType(uri)
var inputStream: InputStream? = activity.contentResolver.openInputStream(uri)
var channels = ArrayList<String>() var channels = ArrayList<String>()
if (type == "application/json") { val fileType = activity.contentResolver.getType(uri)
if (fileType == "application/json") {
// NewPipe subscriptions format
val mapper = ObjectMapper() val mapper = ObjectMapper()
val json = readTextFromUri(uri) val json = readRawTextFromUri(uri)
val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java) val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java)
channels = subscriptions.subscriptions?.map { channels = subscriptions.subscriptions?.map {
it.url?.replace("https://www.youtube.com/channel/", "")!! it.url?.replace("https://www.youtube.com/channel/", "")!!
} as ArrayList<String> } as ArrayList<String>
} else if (type == "application/zip") { } else if (
val zis = ZipInputStream(inputStream) fileType == "text/csv" ||
var entry: ZipEntry? = zis.nextEntry fileType == "text/comma-separated-values"
) {
while (entry != null) { // import subscriptions from Google/YouTube Takeout
if (entry.name.endsWith(".csv")) { val inputStream = activity.contentResolver.openInputStream(uri)
inputStream = zis BufferedReader(InputStreamReader(inputStream)).use { reader ->
break var line: String? = reader.readLine()
} while (line != null) {
entry = zis.nextEntry val channelId = line.substringBefore(",")
inputStream?.bufferedReader()?.readLines()?.forEach { if (channelId.length == 24) channels.add(channelId)
if (it.isNotBlank()) { line = reader.readLine()
val channelId = it.substringBefore(",")
if (channelId.length == 24) {
channels.add(channelId)
}
} }
} }
inputStream?.close() inputStream?.close()
}
} else { } else {
throw IllegalArgumentException("unsupported type") throw IllegalArgumentException("Unsupported file type")
} }
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
@ -78,7 +74,7 @@ class ImportHelper(
} }
} }
private fun readTextFromUri(uri: Uri): String { private fun readRawTextFromUri(uri: Uri): String {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
activity.contentResolver.openInputStream(uri)?.use { inputStream -> activity.contentResolver.openInputStream(uri)?.use { inputStream ->
BufferedReader(InputStreamReader(inputStream)).use { reader -> BufferedReader(InputStreamReader(inputStream)).use { reader ->