Skip to content
Closed
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
45 changes: 45 additions & 0 deletions extractor/src/main/java/org/schabi/newpipe/extractor/Instance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.schabi.newpipe.extractor;

import org.schabi.newpipe.extractor.exceptions.InvalidInstanceException;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/*
* Copyright (C) 2020 Team NewPipe <[email protected]>
* Instance.java is part of NewPipe Extractor.
*
* NewPipe Extractor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe Extractor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe Extractor. If not, see <https://www.gnu.org/licenses/>.
*/

public interface Instance {

@Nullable
String getName();

@Nonnull
String getUrl();

boolean isValid();

/**
* Fetch instance metadata.
* <p>
* Currently, it only fetches the name and save it in the name attribute.
*
* @throws InvalidInstanceException
*/
void fetchInstanceMetaData() throws InvalidInstanceException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

/**
* Provides access to streaming services supported by NewPipe.
Expand All @@ -36,6 +37,7 @@ public class NewPipe {
private static Downloader downloader;
private static Localization preferredLocalization;
private static ContentCountry preferredContentCountry;
private static boolean useInvidiousForYoutube;

private NewPipe() {
}
Expand Down Expand Up @@ -154,4 +156,12 @@ public static ContentCountry getPreferredContentCountry() {
public static void setPreferredContentCountry(ContentCountry preferredContentCountry) {
NewPipe.preferredContentCountry = preferredContentCountry;
}

public static boolean getUseInvidiousForYoutube() {
return useInvidiousForYoutube;
}

public static void setUseInvidiousForYoutube(final boolean useInvidiousForYoutube) {
NewPipe.useInvidiousForYoutube = useInvidiousForYoutube;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.schabi.newpipe.extractor.services.peertube.PeertubeService;
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudService;
import org.schabi.newpipe.extractor.services.youtube.YoutubeService;
import org.schabi.newpipe.extractor.services.youtube.invidious.InvidiousService;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -36,6 +37,7 @@ private ServiceList() {
}

public static final YoutubeService YouTube;
public static final InvidiousService Invidious;
public static final SoundcloudService SoundCloud;
public static final MediaCCCService MediaCCC;
public static final PeertubeService PeerTube;
Expand All @@ -52,12 +54,24 @@ private ServiceList() {
PeerTube = new PeertubeService(3)
));

private static final List<StreamingService> SERVICES_WITH_INVIDIOUS = Collections.unmodifiableList(
Arrays.asList(
Invidious = new InvidiousService(0),
SoundCloud,
MediaCCC,
PeerTube
));

/**
* Get all the supported services.
*
* @return a unmodifiable list of all the supported services
*/
public static List<StreamingService> all() {
return SERVICES;
if (NewPipe.getUseInvidiousForYoutube()) {
return SERVICES_WITH_INVIDIOUS;
} else {
return SERVICES;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.feed.FeedExtractor;
import org.schabi.newpipe.extractor.kiosk.KioskList;
import org.schabi.newpipe.extractor.linkhandler.*;
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
Expand All @@ -18,10 +23,11 @@
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
import org.schabi.newpipe.extractor.utils.Utils;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nullable;

/*
* Copyright (C) Christian Schabesberger 2018 <[email protected]>
* StreamingService.java is part of NewPipe.
Expand Down Expand Up @@ -69,7 +75,7 @@ public List<MediaCapability> getMediaCapabilities() {
}

public enum MediaCapability {
AUDIO, VIDEO, LIVE, COMMENTS
AUDIO, VIDEO, LIVE, COMMENTS, INSTANCES
}
}

Expand All @@ -87,17 +93,19 @@ public enum LinkType {
private final int serviceId;
private final ServiceInfo serviceInfo;

private Instance instance;

/**
* Creates a new Streaming service.
* If you Implement one do not set id within your implementation of this extractor, instead
* set the id when you put the extractor into
* <a href="https://teamnewpipe.github.io/NewPipeExtractor/javadoc/org/schabi/newpipe/extractor/ServiceList.html">ServiceList</a>.
* Creates a new StreamingService.
* If you implement one do not set id within your implementation of this extractor,
* instead set the id when you put the extractor into {@link ServiceList}].
* All other parameters can be set directly from the overriding constructor.
* @param id the number of the service to identify him within the NewPipe frontend
* @param name the name of the service
* @param capabilities the type of media this service can handle
*/
public StreamingService(int id, String name, List<ServiceInfo.MediaCapability> capabilities) {
public StreamingService(final int id, final String name,
final List<ServiceInfo.MediaCapability> capabilities) {
this.serviceId = id;
this.serviceInfo = new ServiceInfo(name, capabilities);
}
Expand Down Expand Up @@ -387,4 +395,11 @@ public TimeAgoParser getTimeAgoParser(Localization localization) {
throw new IllegalArgumentException("Localization is not supported (\"" + localization.toString() + "\")");
}

public Instance getInstance() {
return this.instance;
}

public void setInstance(final Instance instance) {
this.instance = instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.schabi.newpipe.extractor.exceptions;

/*
* Copyright (C) 2020 Team NewPipe <[email protected]>
* InvalidInstanceException.java is part of NewPipe Extractor.
*
* NewPipe Extractor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe Extractor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe Extractor. If not, see <https://www.gnu.org/licenses/>.
*/

public class InvalidInstanceException extends ExtractionException {
public InvalidInstanceException(String message) {
super(message);
}

public InvalidInstanceException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public String getUrl(String id, String baseUrl) throws ParsingException {
}

/**
* Will returns content filter the corresponding extractor can handle like "channels", "videos", "music", etc.
* Will return content filter the corresponding extractor can handle like "channels", "videos", "music", etc.
*
* @return filter that can be applied when building a query for getting a list
*/
Expand All @@ -95,7 +95,7 @@ public String[] getAvailableContentFilter() {
}

/**
* Will returns sort filter the corresponding extractor can handle like "A-Z", "oldest first", "size", etc.
* Will return sort filter the corresponding extractor can handle like "A-Z", "oldest first", "size", etc.
*
* @return filter that can be applied when building a query for getting a list
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class SearchQueryHandlerFactory extends ListLinkHandlerFactory {
///////////////////////////////////

@Override
public abstract String getUrl(String querry, List<String> contentFilter, String sortFilter) throws ParsingException;
public abstract String getUrl(String query, List<String> contentFilter, String sortFilter) throws ParsingException;

public String getSearchString(String url) {
return "";
Expand All @@ -28,14 +28,14 @@ public String getId(String url) {
}

@Override
public SearchQueryHandler fromQuery(String querry,
public SearchQueryHandler fromQuery(String query,
List<String> contentFilter,
String sortFilter) throws ParsingException {
return new SearchQueryHandler(super.fromQuery(querry, contentFilter, sortFilter));
return new SearchQueryHandler(super.fromQuery(query, contentFilter, sortFilter));
}

public SearchQueryHandler fromQuery(String querry) throws ParsingException {
return fromQuery(querry, new ArrayList<String>(0), "");
public SearchQueryHandler fromQuery(String query) throws ParsingException {
return fromQuery(query, new ArrayList<String>(0), "");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,81 @@
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;

import org.schabi.newpipe.extractor.Instance;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.InvalidInstanceException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Utils;

import javax.annotation.Nonnull;
import java.io.IOException;

public class PeertubeInstance {
public class PeertubeInstance implements Instance {

private final String url;
private String name;
public static final PeertubeInstance defaultInstance = new PeertubeInstance("https://framatube.org", "FramaTube");
private static final PeertubeInstance defaultInstance = new PeertubeInstance("https://framatube.org", "FramaTube");

private Boolean isValid = null;

public PeertubeInstance(String url) {
this.url = url;
this.name = "PeerTube";
this(url, "PeerTube");
}

public PeertubeInstance(String url, String name) {
this.url = url;
this.name = name;
}

@Nonnull
public String getUrl() {
return url;
}

public void fetchInstanceMetaData() throws Exception {
@Override
public boolean isValid() {
if (isValid != null) {
return isValid;
}

try {
fetchInstanceMetaData();
return isValid = true;
} catch (InvalidInstanceException e) {
return isValid = false;
}
}

public void fetchInstanceMetaData() throws InvalidInstanceException {
Downloader downloader = NewPipe.getDownloader();
Response response = null;

try {
response = downloader.get(url + "/api/v1/config");
} catch (ReCaptchaException | IOException e) {
throw new Exception("unable to configure instance " + url, e);
throw new InvalidInstanceException("unable to configure instance " + url, e);
}

if (response == null || Utils.isBlank(response.responseBody())) {
throw new Exception("unable to configure instance " + url);
throw new InvalidInstanceException("unable to configure instance " + url);
}

try {
JsonObject json = JsonParser.object().from(response.responseBody());
this.name = JsonUtils.getString(json, "instance.name");
} catch (JsonParserException | ParsingException e) {
throw new Exception("unable to parse instance config", e);
throw new InvalidInstanceException("unable to parse instance config", e);
}
}

public static Instance getDefaultInstance() {
return defaultInstance;
}

public String getName() {
return name;
}
Expand Down
Loading