[#11930] Extracting to a separate file
This commit is contained in:
parent
ec48da7b3a
commit
4038ecdfd2
3 changed files with 97 additions and 79 deletions
|
|
@ -0,0 +1,90 @@
|
||||||
|
package org.schabi.newpipe.local.playlist;
|
||||||
|
|
||||||
|
import static com.google.common.collect.Streams.stream;
|
||||||
|
import static org.apache.commons.collections4.IterableUtils.reversedIterable;
|
||||||
|
import static java.util.Collections.reverse;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import org.schabi.newpipe.R;
|
||||||
|
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
|
||||||
|
import org.schabi.newpipe.database.stream.model.StreamEntity;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
final class ExportPlaylist {
|
||||||
|
|
||||||
|
private ExportPlaylist() {
|
||||||
|
}
|
||||||
|
|
||||||
|
static String export(final PlayListShareMode shareMode,
|
||||||
|
final List<PlaylistStreamEntry> playlist,
|
||||||
|
final Context context) {
|
||||||
|
|
||||||
|
return switch (shareMode) {
|
||||||
|
|
||||||
|
case WITH_TITLES -> exportWithTitles(playlist, context);
|
||||||
|
case JUST_URLS -> exportJustUrls(playlist);
|
||||||
|
case YOUTUBE_TEMP_PLAYLIST -> exportAsYoutubeTempPlaylist(playlist);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static String exportWithTitles(final List<PlaylistStreamEntry> playlist,
|
||||||
|
final Context context) {
|
||||||
|
|
||||||
|
return playlist.stream()
|
||||||
|
.map(PlaylistStreamEntry::getStreamEntity)
|
||||||
|
.map(entity -> context.getString(R.string.video_details_list_item,
|
||||||
|
entity.getTitle(),
|
||||||
|
entity.getUrl()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.collect(Collectors.joining("\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static String exportJustUrls(final List<PlaylistStreamEntry> playlist) {
|
||||||
|
|
||||||
|
return playlist.stream()
|
||||||
|
.map(PlaylistStreamEntry::getStreamEntity)
|
||||||
|
.map(StreamEntity::getUrl)
|
||||||
|
.collect(Collectors.joining("\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static String exportAsYoutubeTempPlaylist(final List<PlaylistStreamEntry> playlist) {
|
||||||
|
|
||||||
|
final List<String> videoIDs =
|
||||||
|
stream(reversedIterable(playlist))
|
||||||
|
.map(PlaylistStreamEntry::getStreamEntity)
|
||||||
|
.map(entity -> getYouTubeId(entity.getUrl()))
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.limit(50)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
reverse(videoIDs);
|
||||||
|
|
||||||
|
final String commaSeparatedVideoIDs = videoIDs.stream()
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
|
||||||
|
return "http://www.youtube.com/watch_videos?video_ids=" + commaSeparatedVideoIDs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the video id from a YouTube URL.
|
||||||
|
*
|
||||||
|
* @param url YouTube URL
|
||||||
|
* @return the video id
|
||||||
|
*/
|
||||||
|
static String getYouTubeId(final String url) {
|
||||||
|
|
||||||
|
final HttpUrl httpUrl = HttpUrl.parse(url);
|
||||||
|
|
||||||
|
return httpUrl == null ? null
|
||||||
|
: httpUrl.queryParameter("v");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,15 +1,13 @@
|
||||||
package org.schabi.newpipe.local.playlist;
|
package org.schabi.newpipe.local.playlist;
|
||||||
|
|
||||||
import static com.google.common.collect.Streams.stream;
|
|
||||||
import static org.apache.commons.collections4.IterableUtils.reversedIterable;
|
|
||||||
import static org.schabi.newpipe.error.ErrorUtil.showUiErrorSnackbar;
|
import static org.schabi.newpipe.error.ErrorUtil.showUiErrorSnackbar;
|
||||||
import static org.schabi.newpipe.ktx.ViewUtils.animate;
|
import static org.schabi.newpipe.ktx.ViewUtils.animate;
|
||||||
|
import static org.schabi.newpipe.local.playlist.ExportPlaylist.export;
|
||||||
import static org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS;
|
import static org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS;
|
||||||
import static org.schabi.newpipe.local.playlist.PlayListShareMode.WITH_TITLES;
|
import static org.schabi.newpipe.local.playlist.PlayListShareMode.WITH_TITLES;
|
||||||
import static org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST;
|
import static org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST;
|
||||||
import static org.schabi.newpipe.util.ThemeHelper.shouldUseGridLayout;
|
import static org.schabi.newpipe.util.ThemeHelper.shouldUseGridLayout;
|
||||||
|
|
||||||
import static java.util.Collections.reverse;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
@ -34,10 +32,6 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||||
import androidx.viewbinding.ViewBinding;
|
import androidx.viewbinding.ViewBinding;
|
||||||
|
|
||||||
import com.evernote.android.state.State;
|
import com.evernote.android.state.State;
|
||||||
import com.google.common.collect.Streams;
|
|
||||||
|
|
||||||
import org.apache.commons.collections4.IterableUtils;
|
|
||||||
import org.apache.commons.collections4.queue.CircularFifoQueue;
|
|
||||||
import org.reactivestreams.Subscriber;
|
import org.reactivestreams.Subscriber;
|
||||||
import org.reactivestreams.Subscription;
|
import org.reactivestreams.Subscription;
|
||||||
import org.schabi.newpipe.NewPipeDatabase;
|
import org.schabi.newpipe.NewPipeDatabase;
|
||||||
|
|
@ -72,17 +66,14 @@ import org.schabi.newpipe.util.external_communication.ShareUtils;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||||
import io.reactivex.rxjava3.core.Single;
|
import io.reactivex.rxjava3.core.Single;
|
||||||
import io.reactivex.rxjava3.disposables.CompositeDisposable;
|
import io.reactivex.rxjava3.disposables.CompositeDisposable;
|
||||||
import io.reactivex.rxjava3.disposables.Disposable;
|
import io.reactivex.rxjava3.disposables.Disposable;
|
||||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||||
import okhttp3.HttpUrl;
|
|
||||||
|
|
||||||
public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistStreamEntry>, Void>
|
public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistStreamEntry>, Void>
|
||||||
implements PlaylistControlViewHolder, DebounceSavable {
|
implements PlaylistControlViewHolder, DebounceSavable {
|
||||||
|
|
@ -435,70 +426,6 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static String export(final PlayListShareMode shareMode,
|
|
||||||
final List<PlaylistStreamEntry> playlist,
|
|
||||||
final Context context) {
|
|
||||||
|
|
||||||
return switch (shareMode) {
|
|
||||||
|
|
||||||
case WITH_TITLES -> exportWithTitles(playlist, context);
|
|
||||||
case JUST_URLS -> exportJustUrls(playlist);
|
|
||||||
case YOUTUBE_TEMP_PLAYLIST -> exportAsYoutubeTempPlaylist(playlist);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static String exportWithTitles(final List<PlaylistStreamEntry> playlist, final Context context) {
|
|
||||||
|
|
||||||
return playlist.stream()
|
|
||||||
.map(PlaylistStreamEntry::getStreamEntity)
|
|
||||||
.map(entity -> context.getString(R.string.video_details_list_item,
|
|
||||||
entity.getTitle(),
|
|
||||||
entity.getUrl()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.collect(Collectors.joining("\n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
static String exportJustUrls(final List<PlaylistStreamEntry> playlist) {
|
|
||||||
|
|
||||||
return playlist.stream()
|
|
||||||
.map(PlaylistStreamEntry::getStreamEntity)
|
|
||||||
.map(StreamEntity::getUrl)
|
|
||||||
.collect(Collectors.joining("\n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
static String exportAsYoutubeTempPlaylist(final List<PlaylistStreamEntry> playlist) {
|
|
||||||
|
|
||||||
final List<String> videoIDs =
|
|
||||||
stream(reversedIterable(playlist))
|
|
||||||
.map(PlaylistStreamEntry::getStreamEntity)
|
|
||||||
.map(entity -> getYouTubeId(entity.getUrl()))
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.limit(50)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
reverse(videoIDs);
|
|
||||||
|
|
||||||
final String commaSeparatedVideoIDs = videoIDs.stream()
|
|
||||||
.collect(Collectors.joining(","));
|
|
||||||
|
|
||||||
return "http://www.youtube.com/watch_videos?video_ids=" + commaSeparatedVideoIDs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the video id from a YouTube URL.
|
|
||||||
*
|
|
||||||
* @param url YouTube URL
|
|
||||||
* @return the video id
|
|
||||||
*/
|
|
||||||
static String getYouTubeId(final String url) {
|
|
||||||
|
|
||||||
final HttpUrl httpUrl = HttpUrl.parse(url);
|
|
||||||
|
|
||||||
return httpUrl == null ? null
|
|
||||||
: httpUrl.queryParameter("v");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeWatchedStreams(final boolean removePartiallyWatched) {
|
public void removeWatchedStreams(final boolean removePartiallyWatched) {
|
||||||
if (isRewritingPlaylist) {
|
if (isRewritingPlaylist) {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package org.schabi.newpipe.local.playlist;
|
package org.schabi.newpipe.local.playlist;
|
||||||
|
|
||||||
|
import static org.schabi.newpipe.local.playlist.ExportPlaylist.export;
|
||||||
import static org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS;
|
import static org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS;
|
||||||
import static org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST;
|
import static org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST;
|
||||||
|
|
||||||
|
|
@ -14,7 +15,7 @@ import org.schabi.newpipe.extractor.stream.StreamType;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class LocalPlaylistFragmentTest {
|
public class ExportPlaylistTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void exportAsYouTubeTempPlaylist() {
|
public void exportAsYouTubeTempPlaylist() {
|
||||||
|
|
@ -27,7 +28,7 @@ public class LocalPlaylistFragmentTest {
|
||||||
"https://www.youtube.com/watch?v=3"
|
"https://www.youtube.com/watch?v=3"
|
||||||
);
|
);
|
||||||
|
|
||||||
final String url = LocalPlaylistFragment.export(YOUTUBE_TEMP_PLAYLIST, playlist, null);
|
final String url = export(YOUTUBE_TEMP_PLAYLIST, playlist, null);
|
||||||
|
|
||||||
Assert.assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url);
|
Assert.assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url);
|
||||||
}
|
}
|
||||||
|
|
@ -55,7 +56,7 @@ public class LocalPlaylistFragmentTest {
|
||||||
.map(id -> "https://www.youtube.com/watch?v=" + id)
|
.map(id -> "https://www.youtube.com/watch?v=" + id)
|
||||||
);
|
);
|
||||||
|
|
||||||
final String url = LocalPlaylistFragment.export(YOUTUBE_TEMP_PLAYLIST, playlist, null);
|
final String url = export(YOUTUBE_TEMP_PLAYLIST, playlist, null);
|
||||||
|
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
|
|
||||||
|
|
@ -80,7 +81,7 @@ public class LocalPlaylistFragmentTest {
|
||||||
"https://www.youtube.com/watch?v=3"
|
"https://www.youtube.com/watch?v=3"
|
||||||
);
|
);
|
||||||
|
|
||||||
final String exported = LocalPlaylistFragment.export(JUST_URLS, playlist, null);
|
final String exported = export(JUST_URLS, playlist, null);
|
||||||
|
|
||||||
Assert.assertEquals("""
|
Assert.assertEquals("""
|
||||||
https://www.youtube.com/watch?v=1
|
https://www.youtube.com/watch?v=1
|
||||||
|
|
@ -98,7 +99,7 @@ public class LocalPlaylistFragmentTest {
|
||||||
static List<PlaylistStreamEntry> asPlaylist(final Stream<String> urls) {
|
static List<PlaylistStreamEntry> asPlaylist(final Stream<String> urls) {
|
||||||
|
|
||||||
return urls
|
return urls
|
||||||
.map(LocalPlaylistFragmentTest::newPlaylistStreamEntry)
|
.map(ExportPlaylistTest::newPlaylistStreamEntry)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue