youpipe: add support for importing from newpipe or google takeout #98
- Commit
0621d5c6717287d1a2b2e8ddab9b2dca806ab4af- Author
- Vayun Mathur <[email protected]>
- When
- 2026-05-03 20:45 UTC (7 months ago)
- Parents
777b33a
Diff
youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionsPage.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionsPage.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionsPage.ktindex 66899f2fd1..d221346476 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionsPage.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SubscriptionsPage.kt@@ -12,10 +12,12 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.CircleShape import androidx.compose.material3.CircularProgressIndicator+import androidx.compose.material3.DropdownMenu+import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.ExperimentalMaterial3Api-import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.ListItem+import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar@@ -30,10 +32,8 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.platform.LocalContext-import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp-import com.vayunmathur.library.util.NavBackStack import coil.compose.AsyncImage import com.vayunmathur.library.ui.IconAdd import com.vayunmathur.library.ui.IconBackup@@ -41,20 +41,27 @@ import com.vayunmathur.library.ui.IconEdit import com.vayunmathur.library.ui.IconRestore import com.vayunmathur.library.util.BottomNavBar import com.vayunmathur.library.util.DatabaseViewModel+import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.youpipe.MAIN_BOTTOM_BAR_ITEMS import com.vayunmathur.youpipe.R import com.vayunmathur.youpipe.Route-import com.vayunmathur.youpipe.util.channelURLtoID+import com.vayunmathur.youpipe.data.HistoryVideo import com.vayunmathur.youpipe.data.Subscription import com.vayunmathur.youpipe.data.SubscriptionCategory-import com.vayunmathur.youpipe.util.getChannelInfo+import com.vayunmathur.youpipe.util.getChannelInfoFromURL+import com.vayunmathur.youpipe.util.setupHourlyTask+import com.vayunmathur.youpipe.util.videoURLtoID+import java.util.zip.ZipInputStream+import kotlin.time.Clock+import kotlin.time.Instant import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch+import kotlinx.serialization.decodeFromString+import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive-import com.vayunmathur.youpipe.util.setupHourlyTask @OptIn(ExperimentalMaterial3Api::class) @Composable@@ -68,42 +75,212 @@ fun SubscriptionsPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewMod var isLoading by remember { mutableStateOf(false) } var progress by remember { mutableStateOf(0f) } - val filePickerActivityContract = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->- if(uri != null) {- try {- val channelURLs = Json.parseToJsonElement(context.contentResolver.openInputStream(uri)!!.bufferedReader().readText()).jsonObject["subscriptions"]!!.jsonArray.map {- it.jsonObject["url"]!!.jsonPrimitive.content+ val youtubeLauncher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->+ if (uri != null) {+ coroutineScope.launch(Dispatchers.IO) {+ isLoading = true+ progress = 0f+ try {+ val zipInputStream = ZipInputStream(context.contentResolver.openInputStream(uri))+ var entry = zipInputStream.nextEntry+ val subs = mutableListOf<Subscription>()+ val history = mutableListOf<HistoryVideo>()+ + while (entry != null) {+ if (entry.name.endsWith("subscriptions/subscriptions.csv")) {+ val content = zipInputStream.readBytes().decodeToString()+ val lines = content.lines().drop(1) // Header+ val total = lines.size+ lines.forEachIndexed { index, line ->+ if (line.isNotBlank()) {+ val parts = line.split(",")+ if (parts.size >= 2) {+ val url = parts[1]+ try {+ val channelInfo = getChannelInfoFromURL(url)+ subs.add(channelInfo.toSubscription())+ } catch (e: Exception) {+ Log.e("SubscriptionsPage", "Error fetching channel info for $url", e)+ }+ }+ }+ progress = (index + 1).toFloat() / total+ }+ } else if (entry.name.endsWith("history/watch-history.json")) {+ val jsonString = zipInputStream.readBytes().decodeToString()+ val jsonArray = Json.parseToJsonElement(jsonString).jsonArray+ jsonArray.forEach { element ->+ try {+ val title = element.jsonObject["title"]?.jsonPrimitive?.content?.removePrefix("Watched ") ?: ""+ val url = element.jsonObject["titleUrl"]?.jsonPrimitive?.content ?: ""+ val time = element.jsonObject["time"]?.jsonPrimitive?.content?.let { Instant.parse(it) } ?: Clock.System.now()+ val author = element.jsonObject["subtitles"]?.jsonArray?.firstOrNull()?.jsonObject?.get("name")?.jsonPrimitive?.content ?: ""+ + if (url.contains("watch?v=")) {+ val videoID = videoURLtoID(url)+ history.add(+ HistoryVideo(+ id = videoID,+ progress = 0,+ videoItem = VideoInfo(title, videoID, 0, 0, time, "", author),+ timestamp = time+ )+ )+ }+ } catch (e: Exception) {+ Log.e("SubscriptionsPage", "Error parsing history item", e)+ }+ }+ } else if (entry.name.endsWith("history/watch-history.html")) {+ val html = zipInputStream.readBytes().decodeToString()+ // Simple regex-based parsing for HTML takeout+ val regex = Regex("<div class=\"content-cell mdl-cell mdl-cell--6-col mdl-typography--body-1\">Watched <a href=\"(.*?)\">(.*?)</a><br><a href=\"(.*?)\">(.*?)</a><br>(.*?)</div>", RegexOption.DOT_MATCHES_ALL)+ val matches = regex.findAll(html)+ matches.forEach { match ->+ try {+ val url = match.groupValues[1]+ val title = match.groupValues[2]+ val author = match.groupValues[4]+ + if (url.contains("watch?v=")) {+ val videoID = videoURLtoID(url)+ history.add(+ HistoryVideo(+ id = videoID,+ progress = 0,+ videoItem = VideoInfo(title, videoID, 0, 0, Clock.System.now(), "", author),+ timestamp = Clock.System.now()+ )+ )+ }+ } catch (e: Exception) {+ Log.e("SubscriptionsPage", "Error parsing HTML history item", e)+ }+ }+ }+ entry = zipInputStream.nextEntry+ }+ + if (subs.isNotEmpty()) viewModel.upsertAll(subs)+ if (history.isNotEmpty()) viewModel.upsertAll(history)+ + } catch (e: Exception) {+ Log.e("SubscriptionsPage", "Error importing YouTube Takeout", e)+ }+ isLoading = false+ setupHourlyTask(context)+ }+ }+ }++ val exportLauncher = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("application/json")) { uri ->+ if (uri != null) {+ coroutineScope.launch(Dispatchers.IO) {+ try {+ val subs = viewModel.getAll<Subscription>()+ val json = Json.encodeToString(subs)+ context.contentResolver.openOutputStream(uri)?.use { + it.write(json.toByteArray())+ }+ } catch (e: Exception) {+ Log.e("SubscriptionsPage", "Error exporting subscriptions", e)+ }+ }+ }+ }++ val restoreLauncher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->+ if (uri != null) {+ coroutineScope.launch(Dispatchers.IO) {+ isLoading = true+ try {+ val json = context.contentResolver.openInputStream(uri)!!.bufferedReader().readText()+ val subs = Json.decodeFromString<List<Subscription>>(json)+ viewModel.replaceAll(subs)+ } catch (e: Exception) {+ Log.e("SubscriptionsPage", "Error restoring subscriptions", e) }- coroutineScope.launch(Dispatchers.IO) {- progress = 0f- isLoading = true- try {- val subs = channelURLs.map(::channelURLtoID).map { getChannelInfo(it) }.map(ChannelInfo::toSubscription)+ isLoading = false+ setupHourlyTask(context)+ }+ }+ }++ val newPipeLauncher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->+ if (uri != null) {+ coroutineScope.launch(Dispatchers.IO) {+ isLoading = true+ progress = 0f+ try {+ val jsonString = context.contentResolver.openInputStream(uri)!!.bufferedReader().readText()+ val json = Json.parseToJsonElement(jsonString).jsonObject+ val subsArray = json["subscriptions"]?.jsonArray+ if (subsArray != null) {+ val total = subsArray.size+ val subs = mutableListOf<Subscription>()+ subsArray.forEachIndexed { index, element ->+ try {+ var url = element.jsonObject["url"]?.jsonPrimitive?.content ?: ""+ if (!url.startsWith("http")) {+ url = "https://$url"+ }+ val channelInfo = getChannelInfoFromURL(url)+ subs.add(channelInfo.toSubscription())+ } catch (e: Exception) {+ Log.e("SubscriptionsPage", "Error importing channel", e)+ }+ progress = (index + 1).toFloat() / total+ } viewModel.replaceAll(subs)- } catch (e: Exception) {- Log.e("SubscriptionsPage", "Error processing subscriptions from file", e) }- isLoading = false- setupHourlyTask(context)+ } catch (e: Exception) {+ Log.e("SubscriptionsPage", "Error importing NewPipe subscriptions", e) }- } catch (e: Exception) {- Log.e("SubscriptionsPage", "Error reading subscriptions file from URI: $uri", e)+ isLoading = false+ setupHourlyTask(context) } } }+ Scaffold(topBar = { TopAppBar({Text(stringResource(R.string.title_subscriptions))}, actions = { if(!isLoading) { IconButton({- // save subscription to file+ exportLauncher.launch("youpipe_subscriptions.json") }) { IconBackup() }- IconButton({- // open file then- filePickerActivityContract.launch("application/json")- }) {- IconRestore()+ + var showRestoreMenu by remember { mutableStateOf(false) }+ Box {+ IconButton({+ showRestoreMenu = true+ }) {+ IconRestore()+ }+ DropdownMenu(expanded = showRestoreMenu, onDismissRequest = { showRestoreMenu = false }) {+ DropdownMenuItem(+ text = { Text("Restore from YouPipe") },+ onClick = {+ showRestoreMenu = false+ restoreLauncher.launch("application/json")+ }+ )+ DropdownMenuItem(+ text = { Text("Import NewPipe subscriptions") },+ onClick = {+ showRestoreMenu = false+ newPipeLauncher.launch("application/json")+ }+ )+ DropdownMenuItem(+ text = { Text("Import from YouTube") },+ onClick = {+ showRestoreMenu = false+ youtubeLauncher.launch("application/zip")+ }+ )+ } } } })@@ -164,4 +341,4 @@ fun SubscriptionsPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewMod } } }-}\ No newline at end of file+}
youpipe/src/main/java/com/vayunmathur/youpipe/util/Extractor.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/util/Extractor.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/util/Extractor.ktindex c8db217bb6..157bbc19c7 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/util/Extractor.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/util/Extractor.kt@@ -33,8 +33,19 @@ fun channelIDtoURL(id: String): String { return "https://www.youtube.com/channel/$id" } -fun channelURLtoID(url: String): String {- return url.toUri().lastPathSegment!!+suspend fun getVideoInfo(videoId: Long): VideoInfo = coroutineScope {+ val idString = decodeVideoID(videoId)+ val ex = ServiceList.YouTube.getStreamExtractor("https://www.youtube.com/watch?v=$idString")+ ex.fetchPage()+ VideoInfo(+ ex.name,+ videoId,+ ex.duration,+ ex.viewCount,+ ex.uploadDate!!.instant.toKotlinInstant(),+ ex.thumbnails.first().url,+ ex.uploaderName+ ) } fun getChannelVideos(channelId: String): Sequence<VideoInfo> = sequence {@@ -62,14 +73,16 @@ fun getChannelVideos(channelId: String): Sequence<VideoInfo> = sequence { } } -suspend fun getChannelInfo(channelId: String): ChannelInfo = coroutineScope {- val ex = ServiceList.YouTube.getChannelExtractor(channelIDtoURL(channelId))+suspend fun getChannelInfo(channelId: String): ChannelInfo = getChannelInfoFromURL(channelIDtoURL(channelId))++suspend fun getChannelInfoFromURL(url: String): ChannelInfo = coroutineScope {+ val ex = ServiceList.YouTube.getChannelExtractor(url) ex.fetchPage() ChannelInfo( ex.name,- channelId,+ ex.id, ex.subscriberCount, 0,- ex.avatars.first().url,+ ex.avatars.firstOrNull()?.url ?: "", ) }\ No newline at end of file