mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
Use IO extensions in ImportHelper.
This commit is contained in:
parent
8629adba29
commit
fa0a993418
@ -15,49 +15,39 @@ import kotlinx.coroutines.CoroutineScope
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import java.io.BufferedReader
|
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.io.InputStreamReader
|
|
||||||
|
|
||||||
class ImportHelper(
|
|
||||||
private val activity: Activity
|
|
||||||
) {
|
|
||||||
|
|
||||||
|
class ImportHelper(private val activity: Activity) {
|
||||||
/**
|
/**
|
||||||
* Import subscriptions by a file uri
|
* Import subscriptions by a file uri
|
||||||
*/
|
*/
|
||||||
fun importSubscriptions(uri: Uri?) {
|
fun importSubscriptions(uri: Uri?) {
|
||||||
if (uri == null) return
|
if (uri == null) return
|
||||||
try {
|
try {
|
||||||
var channels = ArrayList<String>()
|
val channels = when (activity.contentResolver.getType(uri)) {
|
||||||
val fileType = activity.contentResolver.getType(uri)
|
"application/json" -> {
|
||||||
|
// NewPipe subscriptions format
|
||||||
|
val mapper = ObjectMapper()
|
||||||
|
val json = activity.contentResolver.openInputStream(uri)?.use {
|
||||||
|
it.bufferedReader().use { reader -> reader.readText() }
|
||||||
|
}.orEmpty()
|
||||||
|
|
||||||
if (fileType == "application/json") {
|
val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java)
|
||||||
// NewPipe subscriptions format
|
subscriptions.subscriptions.orEmpty().map {
|
||||||
val mapper = ObjectMapper()
|
it.url!!.replace("https://www.youtube.com/channel/", "")
|
||||||
val json = readRawTextFromUri(uri)
|
|
||||||
|
|
||||||
val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java)
|
|
||||||
channels = subscriptions.subscriptions?.map {
|
|
||||||
it.url?.replace("https://www.youtube.com/channel/", "")!!
|
|
||||||
} as ArrayList<String>
|
|
||||||
} else if (
|
|
||||||
fileType == "text/csv" ||
|
|
||||||
fileType == "text/comma-separated-values"
|
|
||||||
) {
|
|
||||||
// import subscriptions from Google/YouTube Takeout
|
|
||||||
val inputStream = activity.contentResolver.openInputStream(uri)
|
|
||||||
BufferedReader(InputStreamReader(inputStream)).use { reader ->
|
|
||||||
var line: String? = reader.readLine()
|
|
||||||
while (line != null) {
|
|
||||||
val channelId = line.substringBefore(",")
|
|
||||||
if (channelId.length == 24) channels.add(channelId)
|
|
||||||
line = reader.readLine()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
inputStream?.close()
|
"text/csv", "text/comma-separated-values" -> {
|
||||||
} else {
|
// import subscriptions from Google/YouTube Takeout
|
||||||
throw IllegalArgumentException("Unsupported file type")
|
activity.contentResolver.openInputStream(uri)?.use {
|
||||||
|
it.bufferedReader().useLines { lines ->
|
||||||
|
lines.map { line -> line.substringBefore(",") }
|
||||||
|
.filter { channelId -> channelId.length == 24 }
|
||||||
|
.toList()
|
||||||
|
}
|
||||||
|
}.orEmpty()
|
||||||
|
}
|
||||||
|
else -> throw IllegalArgumentException("Unsupported file type")
|
||||||
}
|
}
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
@ -75,20 +65,6 @@ class ImportHelper(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun readRawTextFromUri(uri: Uri): String {
|
|
||||||
val stringBuilder = StringBuilder()
|
|
||||||
activity.contentResolver.openInputStream(uri)?.use { inputStream ->
|
|
||||||
BufferedReader(InputStreamReader(inputStream)).use { reader ->
|
|
||||||
var line: String? = reader.readLine()
|
|
||||||
while (line != null) {
|
|
||||||
stringBuilder.append(line)
|
|
||||||
line = reader.readLine()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return stringBuilder.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* write the text to the document
|
* write the text to the document
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user