Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading premium exclusive albums for premium accounts #1636

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/java/com/zionhuang/music/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.zionhuang.music.constants.ProxyEnabledKey
import com.zionhuang.music.constants.ProxyTypeKey
import com.zionhuang.music.constants.ProxyUrlKey
import com.zionhuang.music.constants.SYSTEM_DEFAULT
import com.zionhuang.music.constants.UseLoginForBrowse
import com.zionhuang.music.constants.VisitorDataKey
import com.zionhuang.music.extensions.toEnum
import com.zionhuang.music.extensions.toInetSocketAddress
Expand Down Expand Up @@ -71,6 +72,10 @@ class App : Application(), ImageLoaderFactory {
}
}

if (dataStore[UseLoginForBrowse] == true) {
YouTube.useLoginForBrowse = true
}

GlobalScope.launch {
dataStore.data
.map { it[VisitorDataKey] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ val MaxSongCacheSizeKey = intPreferencesKey("maxSongCacheSize")

val PauseListenHistoryKey = booleanPreferencesKey("pauseListenHistory")
val PauseSearchHistoryKey = booleanPreferencesKey("pauseSearchHistory")
val UseLoginForBrowse = booleanPreferencesKey("useLoginForBrowse")
val DisableScreenshotKey = booleanPreferencesKey("disableScreenshot")

val DiscordTokenKey = stringPreferencesKey("discordToken")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.zionhuang.innertube.YouTube
import com.zionhuang.music.LocalDatabase
import com.zionhuang.music.LocalPlayerAwareWindowInsets
import com.zionhuang.music.R
import com.zionhuang.music.constants.DisableScreenshotKey
import com.zionhuang.music.constants.PauseListenHistoryKey
import com.zionhuang.music.constants.PauseSearchHistoryKey
import com.zionhuang.music.constants.UseLoginForBrowse
import com.zionhuang.music.ui.component.DefaultDialog
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.PreferenceEntry
Expand All @@ -48,6 +50,7 @@ fun PrivacySettings(
val database = LocalDatabase.current
val (pauseListenHistory, onPauseListenHistoryChange) = rememberPreference(key = PauseListenHistoryKey, defaultValue = false)
val (pauseSearchHistory, onPauseSearchHistoryChange) = rememberPreference(key = PauseSearchHistoryKey, defaultValue = false)
val (useLoginForBrowse, onUseLoginForBrowseChange) = rememberPreference(key = UseLoginForBrowse, defaultValue = false)
val (disableScreenshot, onDisableScreenshotChange) = rememberPreference(key = DisableScreenshotKey, defaultValue = false)

var showClearListenHistoryDialog by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -155,6 +158,21 @@ fun PrivacySettings(
onClick = { showClearSearchHistoryDialog = true }
)

PreferenceGroupTitle(
title = stringResource(R.string.account)
)

SwitchPreference(
title = { Text(stringResource(R.string.use_login_for_browse)) },
description = stringResource(R.string.use_login_for_browse_desc),
icon = { Icon(painterResource(R.drawable.person), null) },
checked = useLoginForBrowse,
onCheckedChange = {
YouTube.useLoginForBrowse = it
onUseLoginForBrowseChange(it)
}
)

PreferenceGroupTitle(
title = stringResource(R.string.misc)
)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@
<string name="pause_search_history">Pause search history</string>
<string name="clear_search_history">Clear search history</string>
<string name="clear_search_history_confirm">Are you sure you want to clear all search history?</string>
<string name="use_login_for_browse">Use login for browsing content</string>
<string name="use_login_for_browse_desc">This can influence what content you see and for example shows premium-only albums if you are logged in with a Premium account</string>
<string name="disable_screenshot">Disable screenshot</string>
<string name="disable_screenshot_desc">When this option is on, screenshots and the app\'s view in Recents are disabled.</string>
<string name="enable_lrclib">Enable LrcLib lyrics provider</string>
Expand Down
6 changes: 4 additions & 2 deletions innertube/src/main/java/com/zionhuang/innertube/InnerTube.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class InnerTube {
httpClient = createClient()
}

var useLoginForBrowse: Boolean = false

@OptIn(ExperimentalSerializationApi::class)
private fun createClient() = HttpClient(OkHttp) {
expectSuccess = true
Expand Down Expand Up @@ -107,7 +109,7 @@ class InnerTube {
params: String? = null,
continuation: String? = null,
) = httpClient.post("search") {
ytClient(client)
ytClient(client, setLogin = useLoginForBrowse)
setBody(
SearchBody(
context = client.toContext(locale, visitorData),
Expand Down Expand Up @@ -154,7 +156,7 @@ class InnerTube {
continuation: String? = null,
setLogin: Boolean = false,
) = httpClient.post("browse") {
ytClient(client, setLogin)
ytClient(client, setLogin = setLogin || useLoginForBrowse)
setBody(
BrowseBody(
context = client.toContext(locale, visitorData),
Expand Down
6 changes: 5 additions & 1 deletion innertube/src/main/java/com/zionhuang/innertube/YouTube.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ object YouTube {
set(value) {
innerTube.proxy = value
}
var useLoginForBrowse: Boolean
get() = innerTube.useLoginForBrowse
set(value) {
innerTube.useLoginForBrowse = value
}

suspend fun searchSuggestions(query: String): Result<SearchSuggestions> = runCatching {
val response = innerTube.getSearchSuggestions(WEB_REMIX, query).body<GetSearchSuggestionsResponse>()
Expand Down Expand Up @@ -194,7 +199,6 @@ object YouTube {
response = innerTube.browse(
client = WEB_REMIX,
continuation = continuation,
setLogin = true
).body<BrowseResponse>()
songs += response.continuationContents?.musicPlaylistShelfContinuation?.contents?.mapNotNull {
AlbumPage.fromMusicResponsiveListItemRenderer(it.musicResponsiveListItemRenderer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.zionhuang.innertube.models

import com.zionhuang.innertube.models.BrowseEndpoint.BrowseEndpointContextSupportedConfigs.BrowseEndpointContextMusicConfig.Companion.MUSIC_PAGE_TYPE_ALBUM
import com.zionhuang.innertube.models.BrowseEndpoint.BrowseEndpointContextSupportedConfigs.BrowseEndpointContextMusicConfig.Companion.MUSIC_PAGE_TYPE_ARTIST
import com.zionhuang.innertube.models.BrowseEndpoint.BrowseEndpointContextSupportedConfigs.BrowseEndpointContextMusicConfig.Companion.MUSIC_PAGE_TYPE_AUDIOBOOK
import com.zionhuang.innertube.models.BrowseEndpoint.BrowseEndpointContextSupportedConfigs.BrowseEndpointContextMusicConfig.Companion.MUSIC_PAGE_TYPE_PLAYLIST
import kotlinx.serialization.Serializable

Expand Down Expand Up @@ -43,7 +44,8 @@ data class BrowseEndpoint(
val isArtistEndpoint: Boolean
get() = browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType == MUSIC_PAGE_TYPE_ARTIST
val isAlbumEndpoint: Boolean
get() = browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType == MUSIC_PAGE_TYPE_ALBUM
get() = browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType == MUSIC_PAGE_TYPE_ALBUM ||
browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType == MUSIC_PAGE_TYPE_AUDIOBOOK
val isPlaylistEndpoint: Boolean
get() = browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType == MUSIC_PAGE_TYPE_PLAYLIST

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ data class NavigationEndpoint(
?: searchEndpoint
?: queueAddEndpoint
?: shareEntityEndpoint

val anyWatchEndpoint: WatchEndpoint?
get() = watchEndpoint
?: watchPlaylistEndpoint
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ data class ArtistItemsPage(
browseId = renderer.navigationEndpoint.browseEndpoint?.browseId ?: return null,
playlistId = renderer.thumbnailOverlay?.musicItemThumbnailOverlayRenderer
?.content?.musicPlayButtonRenderer?.playNavigationEndpoint
?.watchPlaylistEndpoint?.playlistId ?: return null,
?.anyWatchEndpoint?.playlistId ?: return null,
title = renderer.title.runs?.firstOrNull()?.text ?: return null,
artists = null,
year = renderer.subtitle?.runs?.lastOrNull()?.text?.toIntOrNull(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ data class ArtistPage(
browseId = renderer.navigationEndpoint.browseEndpoint?.browseId ?: return null,
playlistId = renderer.thumbnailOverlay?.musicItemThumbnailOverlayRenderer?.content
?.musicPlayButtonRenderer?.playNavigationEndpoint
?.watchPlaylistEndpoint?.playlistId ?: return null,
?.anyWatchEndpoint?.playlistId ?: return null,
title = renderer.title.runs?.firstOrNull()?.text ?: return null,
artists = null,
year = renderer.subtitle?.runs?.lastOrNull()?.text?.toIntOrNull(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object SearchPage {
renderer.isAlbum -> {
AlbumItem(
browseId = renderer.navigationEndpoint?.browseEndpoint?.browseId ?: return null,
playlistId = renderer.overlay?.musicItemThumbnailOverlayRenderer?.content?.musicPlayButtonRenderer?.playNavigationEndpoint?.watchPlaylistEndpoint?.playlistId ?: return null,
playlistId = renderer.overlay?.musicItemThumbnailOverlayRenderer?.content?.musicPlayButtonRenderer?.playNavigationEndpoint?.anyWatchEndpoint?.playlistId ?: return null,
title = renderer.flexColumns.firstOrNull()
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs
?.firstOrNull()?.text ?: return null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ data class SearchSummaryPage(
renderer.onTap.browseEndpoint?.isAlbumEndpoint == true -> {
AlbumItem(
browseId = renderer.onTap.browseEndpoint.browseId,
playlistId = renderer.buttons.firstOrNull()?.buttonRenderer?.command?.watchPlaylistEndpoint?.playlistId ?: return null,
playlistId = renderer.buttons.firstOrNull()?.buttonRenderer?.command?.anyWatchEndpoint?.playlistId ?: return null,
title = renderer.title.runs?.firstOrNull()?.text ?: return null,
artists = subtitle?.getOrNull(1)?.oddElements()?.map {
Artist(
Expand Down