[PeerTube] Support searching for channels

This commit is contained in:
TobiGr 2022-12-04 21:44:00 +01:00
parent dea6d8ce4c
commit d75a997611
4 changed files with 86 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.InfoItemsCollector;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeChannelInfoItemExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubePlaylistInfoItemExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeSepiaStreamInfoItemExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeStreamInfoItemExtractor;
@ -105,6 +106,7 @@ public final class PeertubeParsingHelper {
item = item.getObject("video");
}
final boolean isPlaylistInfoItem = item.has("videosLength");
final boolean isChannelInfoItem = item.has("followersCount");
final InfoItemExtractor extractor;
if (sepia) {
@ -112,9 +114,10 @@ public final class PeertubeParsingHelper {
} else {
if (isPlaylistInfoItem) {
extractor = new PeertubePlaylistInfoItemExtractor(item, baseUrl);
} else if (isChannelInfoItem) {
extractor = new PeertubeChannelInfoItemExtractor(item, baseUrl);
} else {
extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
}
}
collector.commit(extractor);

View File

@ -0,0 +1,70 @@
package org.schabi.newpipe.extractor.services.peertube.extractors;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import javax.annotation.Nonnull;
public class PeertubeChannelInfoItemExtractor implements ChannelInfoItemExtractor {
final JsonObject item;
final JsonObject uploader;
final String baseUrl;
public PeertubeChannelInfoItemExtractor(@Nonnull final JsonObject item,
@Nonnull final String baseUrl) {
this.item = item;
this.uploader = item.getObject("uploader");
this.baseUrl = baseUrl;
}
@Override
public String getName() throws ParsingException {
return item.getString("displayName");
}
@Override
public String getUrl() throws ParsingException {
return item.getString("url");
}
@Override
public String getThumbnailUrl() throws ParsingException {
final JsonArray avatars = item.getArray("avatars");
if (avatars.isEmpty()) {
return null;
}
int highestRes = -1;
JsonObject avatar = null;
for (final Object a: avatars) {
if (((JsonObject) a).getInt("width") > highestRes) {
avatar = (JsonObject) a;
highestRes = avatar.getInt("width");
}
}
return baseUrl + avatar.getString("path");
}
@Override
public String getDescription() throws ParsingException {
return item.getString("description");
}
@Override
public long getSubscriberCount() throws ParsingException {
return item.getInt("followersCount");
}
@Override
public long getStreamCount() throws ParsingException {
return ChannelExtractor.ITEM_COUNT_UNKNOWN;
}
@Override
public boolean isVerified() throws ParsingException {
return false;
}
}

View File

@ -13,9 +13,11 @@ public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerF
public static final String VIDEOS = "videos";
public static final String SEPIA_VIDEOS = "sepia_videos"; // sepia is the global index
public static final String PLAYLISTS = "playlists";
public static final String CHANNELS = "channels";
public static final String SEPIA_BASE_URL = "https://sepiasearch.org";
public static final String SEARCH_ENDPOINT_PLAYLISTS = "/api/v1/search/video-playlists";
public static final String SEARCH_ENDPOINT_VIDEOS = "/api/v1/search/videos";
public static final String SEARCH_ENDPOINT_CHANNELS = "/api/v1/search/video-channels";
private PeertubeSearchQueryHandlerFactory() {
}
@ -48,6 +50,8 @@ public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerF
|| contentFilters.get(0).equals(VIDEOS)
|| contentFilters.get(0).equals(SEPIA_VIDEOS)) {
endpoint = SEARCH_ENDPOINT_VIDEOS;
} else if (contentFilters.get(0).equals(CHANNELS)) {
endpoint = SEARCH_ENDPOINT_CHANNELS;
} else {
endpoint = SEARCH_ENDPOINT_PLAYLISTS;
}
@ -62,6 +66,7 @@ public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerF
return new String[]{
VIDEOS,
PLAYLISTS,
CHANNELS,
SEPIA_VIDEOS,
};
}

View File

@ -37,4 +37,11 @@ public class PeertubeSearchQHTest {
assertEquals("https://peertube.mastodon.host/api/v1/search/video-playlists?search=asdf", PeerTube.getSearchQHFactory().fromQuery("asdf", singletonList(PeertubeSearchQueryHandlerFactory.PLAYLISTS), "").getUrl());
assertEquals("https://peertube.mastodon.host/api/v1/search/video-playlists?search=hans", PeerTube.getSearchQHFactory().fromQuery("hans", singletonList(PeertubeSearchQueryHandlerFactory.PLAYLISTS), "").getUrl());
}
@Test
void testChannelSearch() throws Exception {
assertEquals("https://peertube.mastodon.host/api/v1/search/video-channels?search=asdf", PeerTube.getSearchQHFactory().fromQuery("asdf", singletonList(PeertubeSearchQueryHandlerFactory.CHANNELS), "").getUrl());
assertEquals("https://peertube.mastodon.host/api/v1/search/video-channels?search=hans", PeerTube.getSearchQHFactory().fromQuery("hans", singletonList(PeertubeSearchQueryHandlerFactory.CHANNELS), "").getUrl());
}
}