Skip to content
Open
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 @@ -7,6 +7,7 @@
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -15,6 +16,8 @@
import java.util.Objects;

public abstract class Extractor {
private final String TAG = getClass().getSimpleName() + "@" + hashCode();

/**
* {@link StreamingService} currently related to this extractor.<br>
* Useful for getting other things from a service (like the url handlers for
Expand Down Expand Up @@ -54,7 +57,9 @@ public LinkHandler getLinkHandler() {
* @throws ExtractionException if the pages content is not understood
*/
public void fetchPage() throws IOException, ExtractionException {
ExtractorLogger.d(TAG, "base fetchPage called");
if (pageFetched) {
ExtractorLogger.d(TAG, "Page already fetched");
return;
}
onFetchPage(downloader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -10,6 +11,7 @@

public abstract class Info implements Serializable {

private static final String TAG = "Info";
private final int serviceId;
/**
* Id of this Info object <br>
Expand Down Expand Up @@ -52,6 +54,7 @@ public Info(final int serviceId,
this.url = url;
this.originalUrl = originalUrl;
this.name = name;
ExtractorLogger.d(TAG, "Base Created " + this);
}

public Info(final int serviceId, final LinkHandler linkHandler, final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

import java.util.List;

Expand All @@ -34,6 +35,7 @@
* Provides access to streaming services supported by NewPipe.
*/
public final class NewPipe {
private static final String TAG = NewPipe.class.getSimpleName();
private static Downloader downloader;
private static Localization preferredLocalization;
private static ContentCountry preferredContentCountry;
Expand All @@ -42,15 +44,19 @@ private NewPipe() {
}

public static void init(final Downloader d) {
ExtractorLogger.d(TAG, "Default init called");
init(d, Localization.DEFAULT);
}

public static void init(final Downloader d, final Localization l) {
ExtractorLogger.d(TAG, "Default init called with localization");
init(d, l, l.getCountryCode().isEmpty()
? ContentCountry.DEFAULT : new ContentCountry(l.getCountryCode()));
}

public static void init(final Downloader d, final Localization l, final ContentCountry c) {
ExtractorLogger.d(TAG, "Initializing with downloader: "
+ d.getClass().getSimpleName() + ", " + l + ", " + c);
downloader = d;
preferredLocalization = l;
preferredContentCountry = c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.utils.ExtractorHelper;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

import java.io.IOException;
import java.util.List;
Expand All @@ -44,7 +45,7 @@
* Info object for opened contents, i.e. the content ready to play.
*/
public class StreamInfo extends Info {

private static final String TAG = StreamInfo.class.getSimpleName();
public static class StreamExtractException extends ExtractionException {
StreamExtractException(final String message) {
super(message);
Expand All @@ -61,19 +62,37 @@ public StreamInfo(final int serviceId,
super(serviceId, id, url, originalUrl, name);
this.streamType = streamType;
this.ageLimit = ageLimit;
ExtractorLogger.d(TAG, "Created " + this);

}

@Override
public String toString() {
return TAG + "["
+ "serviceId=" + getServiceId()
+ ", url='" + getUrl() + '\''
+ ", originalUrl='" + getOriginalUrl() + '\''
+ ", id='" + getId() + '\''
+ ", name='" + getName() + '\''
+ ", streamType=" + streamType
+ ", ageLimit=" + ageLimit
+ ']';
}

public static StreamInfo getInfo(final String url) throws IOException, ExtractionException {
ExtractorLogger.d(TAG, "getInfo(" + url + ")");
return getInfo(NewPipe.getServiceByUrl(url), url);
}

public static StreamInfo getInfo(@Nonnull final StreamingService service,
final String url) throws IOException, ExtractionException {
ExtractorLogger.d(TAG, "getInfo(" + service.getClass().getSimpleName() + ", " + url + ")");
return getInfo(service.getStreamExtractor(url));
}

public static StreamInfo getInfo(@Nonnull final StreamExtractor extractor)
throws ExtractionException, IOException {
ExtractorLogger.d(TAG, "getInfo(" + extractor.getClass().getSimpleName() + ")");
extractor.fetchPage();
final StreamInfo streamInfo;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.schabi.newpipe.extractor.utils;

public final class ExtractorLogger {

private ExtractorLogger() { }

private static Logger logger = new EmptyLogger();

public static void setLogger(final Logger customLogger) {
logger = customLogger;
}

public static void d(final String tag, final String msg) {
logger.debug(tag, msg);
}

public static void d(final String tag, final String msg, final Throwable t) {
logger.debug(tag, msg, t);
}

public static void w(final String tag, final String msg) {
logger.warn(tag, msg);
}

public static void w(final String tag, final String msg, final Throwable t) {
logger.warn(tag, msg, t);
}

public static void e(final String tag, final String msg) {
logger.error(tag, msg);
}

public static void e(final String tag, final String msg, final Throwable t) {
logger.error(tag, msg, t);
}


private static final class EmptyLogger implements Logger {
public void debug(final String tag, final String msg) { }

@Override
public void debug(final String tag, final String msg, final Throwable throwable) { }

public void warn(final String tag, final String msg) { }

@Override
public void warn(final String tag, final String msg, final Throwable t) { }

public void error(final String tag, final String msg) { }

public void error(final String tag, final String msg, final Throwable t) { }
}

/**
* Logger that prints to stdout
*/
public static final class ConsoleLogger implements Logger {
public void debug(final String tag, final String msg) {
System.out.println("[DEBUG][" + tag + "] " + msg);
}

@Override
public void debug(final String tag, final String msg, final Throwable throwable) {
debug(tag, msg);
throwable.printStackTrace(System.err);
}

public void warn(final String tag, final String msg) {
System.out.println("[WARN ][" + tag + "] " + msg);
}

@Override
public void warn(final String tag, final String msg, final Throwable t) {
warn(tag, msg);
t.printStackTrace(System.err);
}

public void error(final String tag, final String msg) {
System.err.println("[ERROR][" + tag + "] " + msg);
}

public void error(final String tag, final String msg, final Throwable t) {
System.err.println("[ERROR][" + tag + "] " + msg);
t.printStackTrace(System.err);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.schabi.newpipe.extractor.utils;

public interface Logger {
void debug(String tag, String message);
void debug(String tag, String message, Throwable throwable);
void warn(String tag, String message);
void warn(String tag, String message, Throwable throwable);
void error(String tag, String message);
void error(String tag, String message, Throwable t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.schabi.newpipe.extractor;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.schabi.newpipe.extractor.utils.ExtractorLogger;

public class LoggerExtension implements BeforeAllCallback {
private static boolean set = false;

@Override
public void beforeAll(ExtensionContext context) {
if (set) return;
set = true;
ExtractorLogger.setLogger(new ExtractorLogger.ConsoleLogger());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.schabi.newpipe.extractor.LoggerExtension
1 change: 1 addition & 0 deletions extractor/src/test/resources/junit-platform.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
junit.jupiter.extensions.autodetection.enabled = true
Loading