Skip to content
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

[PeerTube] Implement sepia search #495

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.PeertubeSepiaStreamInfoItemExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeStreamInfoItemExtractor;
import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Parser;
Expand Down Expand Up @@ -63,6 +64,19 @@ public static Page getNextPage(final String prevPageUrl, final long total) {
}

public static void collectStreamsFrom(final InfoItemsCollector collector, final JsonObject json, final String baseUrl) throws ParsingException {
collectStreamsFrom(collector, json, baseUrl, false);
}

/**
* Collect stream from json with collector
*
* @param collector the collector used to collect information
* @param json the file to retrieve data from
* @param baseUrl the base Url of the instance
* @param sepia if we should use PeertubeSepiaStreamInfoItemExtractor
* @throws ParsingException
*/
public static void collectStreamsFrom(final InfoItemsCollector collector, final JsonObject json, final String baseUrl, boolean sepia) throws ParsingException {
B0pol marked this conversation as resolved.
Show resolved Hide resolved
final JsonArray contents;
try {
contents = (JsonArray) JsonUtils.getValue(json, "data");
Expand All @@ -73,9 +87,15 @@ public static void collectStreamsFrom(final InfoItemsCollector collector, final
for (final Object c : contents) {
if (c instanceof JsonObject) {
final JsonObject item = (JsonObject) c;
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
PeertubeStreamInfoItemExtractor extractor;
if (sepia) {
extractor = new PeertubeSepiaStreamInfoItemExtractor(item, baseUrl);
} else {
extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
}
collector.commit(extractor);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;

import java.util.List;

import static java.util.Arrays.asList;
import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.COMMENTS;
import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.VIDEO;
Expand Down Expand Up @@ -59,7 +61,12 @@ public ListLinkHandlerFactory getCommentsLHFactory() {

@Override
public SearchExtractor getSearchExtractor(SearchQueryHandler queryHandler) {
return new PeertubeSearchExtractor(this, queryHandler);
final List<String> contentFilters = queryHandler.getContentFilters();
boolean external = false;
if (contentFilters.size() > 0 && contentFilters.get(0).startsWith("sepia_")) {
B0pol marked this conversation as resolved.
Show resolved Hide resolved
external = true;
}
return new PeertubeSearchExtractor(this, queryHandler, external);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public class PeertubeSearchExtractor extends SearchExtractor {

// if we should use PeertubeSepiaStreamInfoItemExtractor
private boolean sepia;

public PeertubeSearchExtractor(StreamingService service, SearchQueryHandler linkHandler) {
this(service, linkHandler, false);
}

public PeertubeSearchExtractor(StreamingService service, SearchQueryHandler linkHandler, boolean sepia) {
super(service, linkHandler);
this.sepia = sepia;
}

@Nonnull
Expand Down Expand Up @@ -79,7 +88,7 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, Extr
final long total = json.getLong("total");

final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
collectStreamsFrom(collector, json, getBaseUrl());
collectStreamsFrom(collector, json, getBaseUrl(), sepia);

return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.schabi.newpipe.extractor.services.peertube.extractors;

import com.grack.nanojson.JsonObject;

/**
* A StreamInfoItem collected from SepiaSearch
*/
public class PeertubeSepiaStreamInfoItemExtractor extends PeertubeStreamInfoItemExtractor {

public PeertubeSepiaStreamInfoItemExtractor(JsonObject item, String baseUrl) {
super(item, baseUrl);
final String embedUrl = super.item.getString("embedUrl");
final String embedPath = super.item.getString("embedPath");
final String itemBaseUrl = embedUrl.replace(embedPath, "");
setBaseUrl(itemBaseUrl);

// Usually, all videos, pictures and other content are hosted on the instance,
// or can be accessed by the same URL path if the instance with baseUrl federates the one where the video is actually uploaded
// But it can't be accessed with Sepiasearch, so we use the item's instance as base URL
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.schabi.newpipe.extractor.services.peertube.extractors;

import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.localization.DateWrapper;
Expand All @@ -11,8 +10,9 @@
import org.schabi.newpipe.extractor.utils.JsonUtils;

public class PeertubeStreamInfoItemExtractor implements StreamInfoItemExtractor {

protected final JsonObject item;
private final String baseUrl;
private String baseUrl;

public PeertubeStreamInfoItemExtractor(final JsonObject item, final String baseUrl) {
this.item = item;
Expand Down Expand Up @@ -84,4 +84,8 @@ public StreamType getStreamType() {
public long getDuration() {
return item.getLong("duration");
}

protected void setBaseUrl(final String baseUrl) {
this.baseUrl = baseUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory

public static final String CHARSET_UTF_8 = "UTF-8";
public static final String VIDEOS = "videos";
public static final String SEPIA_VIDEOS = "sepia_videos"; // sepia is the global index
public static final String SEPIA_BASE_URL = "https://sepiasearch.org";
public static final String SEARCH_ENDPOINT = "/api/v1/search/videos";

public static PeertubeSearchQueryHandlerFactory getInstance() {
Expand All @@ -20,7 +22,12 @@ public static PeertubeSearchQueryHandlerFactory getInstance() {

@Override
public String getUrl(String searchString, List<String> contentFilters, String sortFilter) throws ParsingException {
String baseUrl = ServiceList.PeerTube.getBaseUrl();
String baseUrl;
if (contentFilters.size() > 0 && contentFilters.get(0).startsWith("sepia_")) {
B0pol marked this conversation as resolved.
Show resolved Hide resolved
baseUrl = SEPIA_BASE_URL;
} else {
baseUrl = ServiceList.PeerTube.getBaseUrl();
}
return getUrl(searchString, contentFilters, sortFilter, baseUrl);
}

Expand All @@ -38,6 +45,9 @@ public String getUrl(String searchString, List<String> contentFilters, String so

@Override
public String[] getAvailableContentFilter() {
return new String[]{VIDEOS};
return new String[]{
VIDEOS,
SEPIA_VIDEOS
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest;
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeSearchQueryHandlerFactory;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -43,6 +44,29 @@ public static void setUp() throws Exception {
@Nullable @Override public String expectedSearchSuggestion() { return null; }
}

public static class SepiaSearch extends DefaultSearchExtractorTest {
private static SearchExtractor extractor;
private static final String QUERY = "kde";

@BeforeClass
public static void setUp() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance());
// setting instance might break test when running in parallel
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
extractor = PeerTube.getSearchExtractor(QUERY, singletonList(PeertubeSearchQueryHandlerFactory.SEPIA_VIDEOS), "");
extractor.fetchPage();
}

@Override public SearchExtractor extractor() { return extractor; }
@Override public StreamingService expectedService() { return PeerTube; }
@Override public String expectedName() { return QUERY; }
@Override public String expectedId() { return QUERY; }
@Override public String expectedUrlContains() { return "/search/videos?search=" + QUERY; }
@Override public String expectedOriginalUrlContains() { return "/search/videos?search=" + QUERY; }
@Override public String expectedSearchString() { return QUERY; }
@Nullable @Override public String expectedSearchSuggestion() { return null; }
}

public static class PagingTest {
@Test
public void duplicatedItemsCheck() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeSearchQueryHandlerFactory;

import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube;

Expand All @@ -22,5 +24,7 @@ public void testRegularValues() throws Exception {
assertEquals("https://peertube.mastodon.host/api/v1/search/videos?search=Poifj%26jaijf", PeerTube.getSearchQHFactory().fromQuery("Poifj&jaijf").getUrl());
assertEquals("https://peertube.mastodon.host/api/v1/search/videos?search=G%C3%BCl%C3%BCm", PeerTube.getSearchQHFactory().fromQuery("Gülüm").getUrl());
assertEquals("https://peertube.mastodon.host/api/v1/search/videos?search=%3Fj%24%29H%C2%A7B", PeerTube.getSearchQHFactory().fromQuery("?j$)H§B").getUrl());
assertEquals("https://sepiasearch.org/api/v1/search/videos?search=%3Fj%24%29H%C2%A7B", PeerTube.getSearchQHFactory().fromQuery("?j$)H§B", singletonList(PeertubeSearchQueryHandlerFactory.SEPIA_VIDEOS), "").getUrl());
assertEquals("https://anotherpeertubeindex.com/api/v1/search/videos?search=%3Fj%24%29H%C2%A7B", PeerTube.getSearchQHFactory().fromQuery("?j$)H§B", singletonList(PeertubeSearchQueryHandlerFactory.SEPIA_VIDEOS), "", "https://anotherpeertubeindex.com").getUrl());
}
}