|
| 1 | +package org.schabi.newpipe.local.playlist |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull |
| 5 | +import org.schabi.newpipe.R |
| 6 | +import org.schabi.newpipe.database.playlist.PlaylistStreamEntry |
| 7 | +import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS |
| 8 | +import org.schabi.newpipe.local.playlist.PlayListShareMode.WITH_TITLES |
| 9 | +import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST |
| 10 | +import java.util.Objects.nonNull |
| 11 | + |
| 12 | +fun export( |
| 13 | + shareMode: PlayListShareMode, |
| 14 | + playlist: List<PlaylistStreamEntry>, |
| 15 | + context: Context |
| 16 | +): String { |
| 17 | + return when (shareMode) { |
| 18 | + WITH_TITLES -> exportWithTitles(playlist, context) |
| 19 | + JUST_URLS -> exportJustUrls(playlist) |
| 20 | + YOUTUBE_TEMP_PLAYLIST -> exportAsYoutubeTempPlaylist(playlist) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fun exportWithTitles( |
| 25 | + playlist: List<PlaylistStreamEntry>, |
| 26 | + context: Context |
| 27 | +): String { |
| 28 | + |
| 29 | + return playlist.asSequence() |
| 30 | + .map { it.streamEntity } |
| 31 | + .map { entity -> |
| 32 | + context.getString( |
| 33 | + R.string.video_details_list_item, |
| 34 | + entity.title, |
| 35 | + entity.url |
| 36 | + ) |
| 37 | + } |
| 38 | + .joinToString(separator = "\n") |
| 39 | +} |
| 40 | + |
| 41 | +fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String { |
| 42 | + |
| 43 | + return playlist.asSequence() |
| 44 | + .map { it.streamEntity.url } |
| 45 | + .joinToString(separator = "\n") |
| 46 | +} |
| 47 | + |
| 48 | +fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String { |
| 49 | + |
| 50 | + val videoIDs = playlist.asReversed().asSequence() |
| 51 | + .map { it.streamEntity } |
| 52 | + .map { getYouTubeId(it.url) } |
| 53 | + .filter(::nonNull) |
| 54 | + .take(50) |
| 55 | + .toList() |
| 56 | + .asReversed() |
| 57 | + .joinToString(separator = ",") |
| 58 | + |
| 59 | + return "http://www.youtube.com/watch_videos?video_ids=$videoIDs" |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Gets the video id from a YouTube URL. |
| 64 | + * |
| 65 | + * @param url YouTube URL |
| 66 | + * @return the video id |
| 67 | + */ |
| 68 | +fun getYouTubeId(url: String): String? { |
| 69 | + val httpUrl = url.toHttpUrlOrNull() |
| 70 | + |
| 71 | + return httpUrl?.queryParameter("v") |
| 72 | +} |
0 commit comments