added logic for fetching related streams

This commit is contained in:
Ritvik Saraf 2018-12-26 14:29:23 +05:30
parent bc82a53f2c
commit b2c6928459
3 changed files with 40 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package org.schabi.newpipe.extractor.services.peertube.extractors; package org.schabi.newpipe.extractor.services.peertube.extractors;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -17,7 +19,7 @@ import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandler; import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory; import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeTrendingLinkHandlerFactory; import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeSearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.AudioStream;
import org.schabi.newpipe.extractor.stream.Stream; import org.schabi.newpipe.extractor.stream.Stream;
import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamExtractor;
@ -191,13 +193,37 @@ public class PeertubeStreamExtractor extends StreamExtractor {
@Override @Override
public StreamInfoItemsCollector getRelatedStreams() throws IOException, ExtractionException { public StreamInfoItemsCollector getRelatedStreams() throws IOException, ExtractionException {
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
List<String> tags = getTags();
String apiUrl = null;
if(!tags.isEmpty()) {
apiUrl = getRelatedStreamsUrl(tags);
//TODO fetch related videos not trending }else {
String apiUrl = new PeertubeTrendingLinkHandlerFactory().getUrl(PeertubeTrendingLinkHandlerFactory.KIOSK_TRENDING); apiUrl = getUploaderUrl() + "/videos?start=0&count=8";
getStreamsFromApi(collector, apiUrl); }
if(!StringUtil.isBlank(apiUrl)) getStreamsFromApi(collector, apiUrl);
return collector; return collector;
} }
private List<String> getTags(){
try {
return (List) JsonUtils.getArray(json, "tags");
} catch (Exception e) {
return Collections.emptyList();
}
}
private String getRelatedStreamsUrl(List<String> tags) throws UnsupportedEncodingException {
String url = ServiceList.PeerTube.getBaseUrl() + PeertubeSearchQueryHandlerFactory.SEARCH_ENDPOINT;
StringBuilder params = new StringBuilder();
params.append("start=0&count=8&sort=-createdAt");
for(String tag : tags) {
params.append("&tagsOneOf=");
params.append(URLEncoder.encode(tag, "UTF-8"));
}
return url + "?" + params.toString();
}
private void getStreamsFromApi(StreamInfoItemsCollector collector, String apiUrl) throws ReCaptchaException, IOException, ParsingException { private void getStreamsFromApi(StreamInfoItemsCollector collector, String apiUrl) throws ReCaptchaException, IOException, ParsingException {
DownloadResponse response = getDownloader().get(apiUrl); DownloadResponse response = getDownloader().get(apiUrl);
JsonObject relatedVideosJson = null; JsonObject relatedVideosJson = null;
@ -226,7 +252,8 @@ public class PeertubeStreamExtractor extends StreamExtractor {
if(c instanceof JsonObject) { if(c instanceof JsonObject) {
final JsonObject item = (JsonObject) c; final JsonObject item = (JsonObject) c;
PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item); PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item);
collector.commit(extractor); //do not add the same stream in related streams
if(!extractor.getUrl().equals(getUrl())) collector.commit(extractor);
} }
} }

View File

@ -12,7 +12,7 @@ public class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory
public static final String CHARSET_UTF_8 = "UTF-8"; public static final String CHARSET_UTF_8 = "UTF-8";
public static final String VIDEOS = "videos"; public static final String VIDEOS = "videos";
private static final String SEARCH_ENDPOINT = "/api/v1/search/videos"; public static final String SEARCH_ENDPOINT = "/api/v1/search/videos";
public static PeertubeSearchQueryHandlerFactory getInstance() { public static PeertubeSearchQueryHandlerFactory getInstance() {
return new PeertubeSearchQueryHandlerFactory(); return new PeertubeSearchQueryHandlerFactory();

View File

@ -12,6 +12,8 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
public class PeertubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory { public class PeertubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory {
private static final PeertubeTrendingLinkHandlerFactory instance = new PeertubeTrendingLinkHandlerFactory();
public static final Map<String, String> KIOSK_MAP; public static final Map<String, String> KIOSK_MAP;
public static final Map<String, String> REVERSE_KIOSK_MAP; public static final Map<String, String> REVERSE_KIOSK_MAP;
public static final String KIOSK_TRENDING = "Trending"; public static final String KIOSK_TRENDING = "Trending";
@ -32,6 +34,10 @@ public class PeertubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory {
REVERSE_KIOSK_MAP = Collections.unmodifiableMap(reverseMap); REVERSE_KIOSK_MAP = Collections.unmodifiableMap(reverseMap);
} }
public static PeertubeTrendingLinkHandlerFactory getInstance() {
return instance;
}
public String getUrl(String id, List<String> contentFilters, String sortFilter) { public String getUrl(String id, List<String> contentFilters, String sortFilter) {
String baseUrl = ServiceList.PeerTube.getBaseUrl(); String baseUrl = ServiceList.PeerTube.getBaseUrl();
return String.format(KIOSK_MAP.get(id), baseUrl); return String.format(KIOSK_MAP.get(id), baseUrl);