Skip to content

Commit d75a997

Browse files
committed
[PeerTube] Support searching for channels
1 parent dea6d8c commit d75a997

4 files changed

Lines changed: 86 additions & 1 deletion

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.schabi.newpipe.extractor.Page;
99
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
1010
import org.schabi.newpipe.extractor.exceptions.ParsingException;
11+
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeChannelInfoItemExtractor;
1112
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubePlaylistInfoItemExtractor;
1213
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeSepiaStreamInfoItemExtractor;
1314
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeStreamInfoItemExtractor;
@@ -105,16 +106,18 @@ public static void collectStreamsFrom(final InfoItemsCollector collector,
105106
item = item.getObject("video");
106107
}
107108
final boolean isPlaylistInfoItem = item.has("videosLength");
109+
final boolean isChannelInfoItem = item.has("followersCount");
108110

109111
final InfoItemExtractor extractor;
110112
if (sepia) {
111113
extractor = new PeertubeSepiaStreamInfoItemExtractor(item, baseUrl);
112114
} else {
113115
if (isPlaylistInfoItem) {
114116
extractor = new PeertubePlaylistInfoItemExtractor(item, baseUrl);
117+
} else if (isChannelInfoItem) {
118+
extractor = new PeertubeChannelInfoItemExtractor(item, baseUrl);
115119
} else {
116120
extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
117-
118121
}
119122
}
120123
collector.commit(extractor);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.schabi.newpipe.extractor.services.peertube.extractors;
2+
3+
import com.grack.nanojson.JsonArray;
4+
import com.grack.nanojson.JsonObject;
5+
6+
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
7+
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
8+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
9+
10+
import javax.annotation.Nonnull;
11+
12+
public class PeertubeChannelInfoItemExtractor implements ChannelInfoItemExtractor {
13+
14+
final JsonObject item;
15+
final JsonObject uploader;
16+
final String baseUrl;
17+
public PeertubeChannelInfoItemExtractor(@Nonnull final JsonObject item,
18+
@Nonnull final String baseUrl) {
19+
this.item = item;
20+
this.uploader = item.getObject("uploader");
21+
this.baseUrl = baseUrl;
22+
}
23+
24+
@Override
25+
public String getName() throws ParsingException {
26+
return item.getString("displayName");
27+
}
28+
29+
@Override
30+
public String getUrl() throws ParsingException {
31+
return item.getString("url");
32+
}
33+
34+
@Override
35+
public String getThumbnailUrl() throws ParsingException {
36+
final JsonArray avatars = item.getArray("avatars");
37+
if (avatars.isEmpty()) {
38+
return null;
39+
}
40+
int highestRes = -1;
41+
JsonObject avatar = null;
42+
for (final Object a: avatars) {
43+
if (((JsonObject) a).getInt("width") > highestRes) {
44+
avatar = (JsonObject) a;
45+
highestRes = avatar.getInt("width");
46+
}
47+
}
48+
return baseUrl + avatar.getString("path");
49+
}
50+
51+
@Override
52+
public String getDescription() throws ParsingException {
53+
return item.getString("description");
54+
}
55+
56+
@Override
57+
public long getSubscriberCount() throws ParsingException {
58+
return item.getInt("followersCount");
59+
}
60+
61+
@Override
62+
public long getStreamCount() throws ParsingException {
63+
return ChannelExtractor.ITEM_COUNT_UNKNOWN;
64+
}
65+
66+
@Override
67+
public boolean isVerified() throws ParsingException {
68+
return false;
69+
}
70+
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeSearchQueryHandlerFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerF
1313
public static final String VIDEOS = "videos";
1414
public static final String SEPIA_VIDEOS = "sepia_videos"; // sepia is the global index
1515
public static final String PLAYLISTS = "playlists";
16+
public static final String CHANNELS = "channels";
1617
public static final String SEPIA_BASE_URL = "https://sepiasearch.org";
1718
public static final String SEARCH_ENDPOINT_PLAYLISTS = "/api/v1/search/video-playlists";
1819
public static final String SEARCH_ENDPOINT_VIDEOS = "/api/v1/search/videos";
20+
public static final String SEARCH_ENDPOINT_CHANNELS = "/api/v1/search/video-channels";
1921

2022
private PeertubeSearchQueryHandlerFactory() {
2123
}
@@ -48,6 +50,8 @@ public String getUrl(final String searchString,
4850
|| contentFilters.get(0).equals(VIDEOS)
4951
|| contentFilters.get(0).equals(SEPIA_VIDEOS)) {
5052
endpoint = SEARCH_ENDPOINT_VIDEOS;
53+
} else if (contentFilters.get(0).equals(CHANNELS)) {
54+
endpoint = SEARCH_ENDPOINT_CHANNELS;
5155
} else {
5256
endpoint = SEARCH_ENDPOINT_PLAYLISTS;
5357
}
@@ -62,6 +66,7 @@ public String[] getAvailableContentFilter() {
6266
return new String[]{
6367
VIDEOS,
6468
PLAYLISTS,
69+
CHANNELS,
6570
SEPIA_VIDEOS,
6671
};
6772
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchQHTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ void testPlaylistSearch() throws Exception {
3737
assertEquals("https://peertube.mastodon.host/api/v1/search/video-playlists?search=asdf", PeerTube.getSearchQHFactory().fromQuery("asdf", singletonList(PeertubeSearchQueryHandlerFactory.PLAYLISTS), "").getUrl());
3838
assertEquals("https://peertube.mastodon.host/api/v1/search/video-playlists?search=hans", PeerTube.getSearchQHFactory().fromQuery("hans", singletonList(PeertubeSearchQueryHandlerFactory.PLAYLISTS), "").getUrl());
3939
}
40+
41+
@Test
42+
void testChannelSearch() throws Exception {
43+
assertEquals("https://peertube.mastodon.host/api/v1/search/video-channels?search=asdf", PeerTube.getSearchQHFactory().fromQuery("asdf", singletonList(PeertubeSearchQueryHandlerFactory.CHANNELS), "").getUrl());
44+
assertEquals("https://peertube.mastodon.host/api/v1/search/video-channels?search=hans", PeerTube.getSearchQHFactory().fromQuery("hans", singletonList(PeertubeSearchQueryHandlerFactory.CHANNELS), "").getUrl());
45+
46+
}
4047
}

0 commit comments

Comments
 (0)