-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Share as YouTube temporary playlist #12065
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2339f51
[#11930] Share as YouTube temporary playlist
tfga 94d4c21
[#11930] @Test export_justUrls()
tfga c6b87cd
[#11930] Making CheckStyle happy
tfga acac50a
[#11930] Non-Youtube URLs should be ignored
tfga b1f995a
[#11930] Playlist with more than 50 items
tfga 24bb71a
[#11930] Making it more efficient: Reverse iteration + limit(50) + re…
tfga 76a02d5
[#11930] Extracting to a separate file
tfga 998d84d
[#11930] Converting to Kotlin
tfga 3c7b026
[#11930] Updating javadoc
tfga 0fd2d4f
[#11930] Removing Apache Commons Collections
tfga d81244e
YT temp playlist URL: http => https
tfga c28478a
getYouTubeId(): Changing implementation to use YoutubeStreamLinkHandler
tfga f96b8f7
Comment: maximum length of 50 items
tfga 8830e87
YouTube video IDs are 11 characters long
tfga 587df09
YouTube video IDs are 11 characters long
tfga 599d861
Making ktLint happy
tfga f3b3d5c
R.string.share_playlist_as_youtube_temporary_playlist
tfga eb05680
R.string.share_playlist_as_youtube_temporary_playlist: pt-BR
tfga 098f60d
Don't add the title when sharing as YouTube temp playlist
tfga be097f2
Deleting the "explanatory text" bellow the title
tfga 2ceb702
sharePlaylist(): converting javadoc from Markdown back to "classic ja…
tfga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
app/src/main/java/org/schabi/newpipe/local/playlist/ExportPlaylist.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package org.schabi.newpipe.local.playlist | ||
|
|
||
| import android.content.Context | ||
| import okhttp3.HttpUrl.Companion.toHttpUrlOrNull | ||
| import org.schabi.newpipe.R | ||
| import org.schabi.newpipe.database.playlist.PlaylistStreamEntry | ||
| import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS | ||
| import org.schabi.newpipe.local.playlist.PlayListShareMode.WITH_TITLES | ||
| import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST | ||
| import java.util.Objects.nonNull | ||
|
|
||
| fun export( | ||
| shareMode: PlayListShareMode, | ||
| playlist: List<PlaylistStreamEntry>, | ||
| context: Context | ||
| ): String { | ||
| return when (shareMode) { | ||
| WITH_TITLES -> exportWithTitles(playlist, context) | ||
| JUST_URLS -> exportJustUrls(playlist) | ||
| YOUTUBE_TEMP_PLAYLIST -> exportAsYoutubeTempPlaylist(playlist) | ||
| } | ||
| } | ||
|
|
||
| fun exportWithTitles( | ||
| playlist: List<PlaylistStreamEntry>, | ||
| context: Context | ||
| ): String { | ||
|
|
||
| return playlist.asSequence() | ||
| .map { it.streamEntity } | ||
| .map { entity -> | ||
| context.getString( | ||
| R.string.video_details_list_item, | ||
| entity.title, | ||
| entity.url | ||
| ) | ||
| } | ||
| .joinToString(separator = "\n") | ||
| } | ||
|
|
||
| fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String { | ||
|
|
||
| return playlist.asSequence() | ||
| .map { it.streamEntity.url } | ||
| .joinToString(separator = "\n") | ||
| } | ||
|
|
||
| fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String { | ||
|
|
||
| val videoIDs = playlist.asReversed().asSequence() | ||
| .map { it.streamEntity } | ||
| .map { getYouTubeId(it.url) } | ||
| .filter(::nonNull) | ||
| .take(50) | ||
| .toList() | ||
| .asReversed() | ||
| .joinToString(separator = ",") | ||
|
|
||
| return "http://www.youtube.com/watch_videos?video_ids=$videoIDs" | ||
|
tfga marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Gets the video id from a YouTube URL. | ||
| * | ||
| * @param url YouTube URL | ||
| * @return the video id | ||
| */ | ||
| fun getYouTubeId(url: String): String? { | ||
| val httpUrl = url.toHttpUrlOrNull() | ||
|
|
||
| return httpUrl?.queryParameter("v") | ||
| } | ||
|
Stypox marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/src/main/java/org/schabi/newpipe/local/playlist/PlayListShareMode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package org.schabi.newpipe.local.playlist; | ||
|
|
||
| public enum PlayListShareMode { | ||
|
|
||
| JUST_URLS, | ||
| WITH_TITLES, | ||
| YOUTUBE_TEMP_PLAYLIST | ||
| } |
110 changes: 110 additions & 0 deletions
110
app/src/test/java/org/schabi/newpipe/local/playlist/ExportPlaylistTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package org.schabi.newpipe.local.playlist | ||
|
|
||
| import android.content.Context | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
| import org.mockito.Mockito.mock | ||
| import org.schabi.newpipe.database.playlist.PlaylistStreamEntry | ||
| import org.schabi.newpipe.database.stream.model.StreamEntity | ||
| import org.schabi.newpipe.extractor.stream.StreamType | ||
| import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS | ||
| import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST | ||
| import java.util.stream.Stream | ||
|
|
||
| class ExportPlaylistTest { | ||
|
|
||
| @Test | ||
| fun exportAsYouTubeTempPlaylist() { | ||
| val playlist = asPlaylist( | ||
| "https://www.youtube.com/watch?v=1", | ||
| "https://soundcloud.com/cautious-clayofficial/cold-war-2", // non-Youtube URLs should be ignored | ||
| "https://www.youtube.com/watch?v=2", | ||
| "https://www.youtube.com/watch?v=3" | ||
| ) | ||
|
|
||
| val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java)) | ||
|
|
||
| assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url) | ||
| } | ||
|
|
||
| @Test | ||
| fun exportMoreThan50Items() { | ||
| /* | ||
| * Playlist has more than 50 items => take the last 50 | ||
|
Stypox marked this conversation as resolved.
|
||
| * (YouTube limitation) | ||
| */ | ||
|
|
||
| val ids = listOf( | ||
| -1, 0, | ||
| 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
| 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | ||
| 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, | ||
| 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | ||
| 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 | ||
| ) | ||
|
|
||
| val playlist = asPlaylist( | ||
| ids.stream() | ||
| .map { id: Int -> "https://www.youtube.com/watch?v=$id" } | ||
| ) | ||
|
Stypox marked this conversation as resolved.
Outdated
|
||
|
|
||
| val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java)) | ||
|
|
||
| assertEquals( | ||
| "http://www.youtube.com/watch_videos?video_ids=" + | ||
| "1,2,3,4,5,6,7,8,9,10," + | ||
| "11,12,13,14,15,16,17,18,19,20," + | ||
| "21,22,23,24,25,26,27,28,29,30," + | ||
| "31,32,33,34,35,36,37,38,39,40," + | ||
| "41,42,43,44,45,46,47,48,49,50", | ||
|
Stypox marked this conversation as resolved.
Outdated
|
||
|
|
||
| url | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun exportJustUrls() { | ||
| val playlist = asPlaylist( | ||
| "https://www.youtube.com/watch?v=1", | ||
| "https://www.youtube.com/watch?v=2", | ||
| "https://www.youtube.com/watch?v=3" | ||
|
tfga marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| val exported = export(JUST_URLS, playlist, mock(Context::class.java)) | ||
|
|
||
| assertEquals( | ||
| """ | ||
| https://www.youtube.com/watch?v=1 | ||
| https://www.youtube.com/watch?v=2 | ||
| https://www.youtube.com/watch?v=3 | ||
|
tfga marked this conversation as resolved.
Outdated
|
||
| """.trimIndent(), | ||
| exported | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| fun asPlaylist(vararg urls: String): List<PlaylistStreamEntry> { | ||
| return asPlaylist(Stream.of(*urls)) | ||
| } | ||
|
|
||
| fun asPlaylist(urls: Stream<String>): List<PlaylistStreamEntry> { | ||
| return urls | ||
| .map { url: String -> newPlaylistStreamEntry(url) } | ||
| .toList() | ||
| } | ||
|
|
||
| fun newPlaylistStreamEntry(url: String): PlaylistStreamEntry { | ||
| return PlaylistStreamEntry(newStreamEntity(url), 0, 0, 0) | ||
| } | ||
|
|
||
| fun newStreamEntity(url: String): StreamEntity { | ||
| return StreamEntity( | ||
| 0, | ||
| 1, | ||
| url, | ||
| "Title", | ||
| StreamType.VIDEO_STREAM, | ||
| 100, | ||
| "Uploader" | ||
| ) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.