Skip to content

Commit

Permalink
Made YT proxyable
Browse files Browse the repository at this point in the history
* allows usage of YouTubeLike services, e.g. invidious
* simplified some classes/code
* Added support for invidious
  * Some credits go to @B0pol's PR TeamNewPipe#555 for that ;)
  • Loading branch information
litetex committed Apr 21, 2022
1 parent fd1ba01 commit 214eae3
Show file tree
Hide file tree
Showing 132 changed files with 3,796 additions and 1,619 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.schabi.newpipe.extractor;

import org.schabi.newpipe.extractor.instance.Instance;

public interface InstanceBasedStreamingService<I extends Instance> {
I getInstance();

void setInstance(I instance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* A list of supported services.
*/
@SuppressWarnings({"ConstantName", "InnerAssignment"}) // keep unusual names and inner assignments
// keep unusual names and inner assignments
public final class ServiceList {
private ServiceList() {
//no instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,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 <chris.schabesberger@mailbox.org>
* StreamingService.java is part of NewPipe.
Expand Down Expand Up @@ -97,13 +98,14 @@ public enum LinkType {
* 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 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(final int id,
final String name,
final List<ServiceInfo.MediaCapability> capabilities) {
protected 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 @@ -192,9 +194,10 @@ public FeedExtractor getFeedExtractor(final String url) throws ExtractionExcepti

/**
* Must create a new instance of a KioskList implementation.
*
* @return a new KioskList instance
*/
public abstract KioskList getKioskList() throws ExtractionException;
public abstract KioskList getKioskList();

/**
* Must create a new instance of a ChannelExtractor implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,29 @@ public ChannelExtractor(final StreamingService service, final ListLinkHandler li
}

public abstract String getAvatarUrl() throws ParsingException;

public abstract String getBannerUrl() throws ParsingException;

public abstract String getFeedUrl() throws ParsingException;

public abstract long getSubscriberCount() throws ParsingException;

public abstract String getDescription() throws ParsingException;
public abstract String getParentChannelName() throws ParsingException;
public abstract String getParentChannelUrl() throws ParsingException;
public abstract String getParentChannelAvatarUrl() throws ParsingException;
public abstract boolean isVerified() throws ParsingException;

public String getParentChannelName() throws ParsingException {
return null;
}

public String getParentChannelUrl() throws ParsingException {
return null;
}

public String getParentChannelAvatarUrl() throws ParsingException {
return null;
}

public boolean isVerified() throws ParsingException {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ public interface ChannelInfoItemExtractor extends InfoItemExtractor {

long getStreamCount() throws ParsingException;

boolean isVerified() throws ParsingException;
default boolean isVerified() throws ParsingException {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeCommentsInfoItemExtractor;
import org.schabi.newpipe.extractor.services.youtube.youtube.extractors.YoutubeCommentsInfoItemExtractor;
import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.utils.Utils;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.schabi.newpipe.extractor.instance;

import org.schabi.newpipe.extractor.utils.Utils;

import java.net.URI;
import java.util.Objects;

import javax.annotation.Nonnull;

public abstract class AbstractInstance implements Instance {

protected Boolean valid = null;

protected final String url;
protected String name;

protected AbstractInstance(final String url, final String name) {
this.url = removeTrailingSlashes(Objects.requireNonNull(url));
this.name = name;
}

@Nonnull
@Override
public String getName() {
return name;
}

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

public void setName(final String name) {
this.name = Objects.requireNonNull(name);
}

@Override
public void fetchMetadata() {
// Default: Do nothing
}

public static String removeTrailingSlashes(final String input) {
return input.replaceAll("/*$", "");
}

public static String tryExtractDomainFromUrl(final String url, final String fallback) {
Objects.requireNonNull(url);
try {
return Utils.removeMAndWWWFromHost(new URI(url).getHost());
} catch (final Exception ignored) {
return fallback;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.schabi.newpipe.extractor.instance;

import javax.annotation.Nonnull;

public interface Instance {

/**
* The name of the instance.
* <br/>
* Note: May only be available after {@link #fetchMetadata()} was called
*
* @return the instance-name
*/
String getName();

/**
* The base url of this instance.
* <br/>
* Note that the url is returned without trailing slashes.
* Use {@link #getUrlWithTrailingSlash()} if you want a trailing slash.
*
* @return base url
*/
@Nonnull
String getUrl();

default String getUrlWithTrailingSlash() {
return getUrl() + "/";
}


/**
* Fetch instance metadata.
*
* @throws InstanceMetaDataFetchException
*/
void fetchMetadata();

/**
* Returns the service name, e.g. "Invidious" for an invidious instance.
* @return service name or <code>null</code> if the name of the streamingservice should be used
*/
default String getServiceName() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.schabi.newpipe.extractor.instance;

public class InstanceMetaDataFetchException extends RuntimeException {
public InstanceMetaDataFetchException(final String message) {
super(message);
}

public InstanceMetaDataFetchException(final String message, final Throwable cause) {
super(message, cause);
}

public InstanceMetaDataFetchException(final Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public KioskList(final StreamingService service) {

public void addKioskEntry(final KioskExtractorFactory extractorFactory,
final ListLinkHandlerFactory handlerFactory,
final String id)
throws Exception {
final String id) {
if (kioskList.get(id) != null) {
throw new Exception("Kiosk with type " + id + " already exists.");
throw new IllegalArgumentException("Kiosk with type " + id + " already exists.");
}
kioskList.put(id, new KioskEntry(extractorFactory, handlerFactory));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.schabi.newpipe.extractor.playlist;

import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;

import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
Expand All @@ -8,18 +10,23 @@

import javax.annotation.Nonnull;

import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;

public abstract class PlaylistExtractor extends ListExtractor<StreamInfoItem> {

public PlaylistExtractor(final StreamingService service, final ListLinkHandler linkHandler) {
super(service, linkHandler);
}

public abstract String getUploaderUrl() throws ParsingException;

public abstract String getUploaderName() throws ParsingException;
public abstract String getUploaderAvatarUrl() throws ParsingException;
public abstract boolean isUploaderVerified() throws ParsingException;

public String getUploaderAvatarUrl() throws ParsingException {
return EMPTY_STRING;
}

public boolean isUploaderVerified() throws ParsingException {
return false;
}

public abstract long getStreamCount() throws ParsingException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
import org.schabi.newpipe.extractor.utils.Utils;

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

import javax.annotation.Nonnull;

public abstract class SearchExtractor extends ListExtractor<InfoItem> {

public static class NothingFoundException extends ExtractionException {
Expand All @@ -27,17 +30,6 @@ public String getSearchString() {
return getLinkHandler().getSearchString();
}

/**
* The search suggestion provided by the service.
* <p>
* This method also returns the corrected query if
* {@link SearchExtractor#isCorrectedSearch()} is true.
*
* @return a suggestion to another query, the corrected query, or an empty String.
*/
@Nonnull
public abstract String getSearchSuggestion() throws ParsingException;

@Nonnull
@Override
public SearchQueryHandler getLinkHandler() {
Expand All @@ -50,6 +42,19 @@ public String getName() {
return getLinkHandler().getSearchString();
}

/**
* The search suggestion provided by the service.
* <p>
* This method also returns the corrected query if
* {@link SearchExtractor#isCorrectedSearch()} is true.
*
* @return a suggestion to another query, the corrected query, or an empty String.
*/
@Nonnull
public String getSearchSuggestion() throws ParsingException {
return Utils.EMPTY_STRING;
}

/**
* Tell if the search was corrected by the service (if it's not exactly the search you typed).
* <p>
Expand All @@ -58,15 +63,20 @@ public String getName() {
*
* @return whether the results comes from a corrected query or not.
*/
public abstract boolean isCorrectedSearch() throws ParsingException;
public boolean isCorrectedSearch() throws ParsingException {
return false;
}

/**
* Meta information about the search query.
* <p>
* Example: on YouTube, if you search for "Covid-19",
* there is a box with information from the WHO about Covid-19 and a link to the WHO's website.
*
* @return additional meta information about the search query
*/
@Nonnull
public abstract List<MetaInfo> getMetaInfo() throws ParsingException;
public List<MetaInfo> getMetaInfo() throws ParsingException {
return Collections.emptyList();
}
}
Loading

0 comments on commit 214eae3

Please sign in to comment.