Improvements for NewPipe

* Added support for content-length (important for downloader)
* Code cleanup
This commit is contained in:
litetex 2022-07-06 20:37:55 +02:00
parent e3c989e183
commit 889b5f2d9e
13 changed files with 74 additions and 14 deletions

View File

@ -4,12 +4,13 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.localization.Localization;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* A base for downloader implementations that NewPipe will use * A base for downloader implementations that NewPipe will use
* to download needed resources during extraction. * to download needed resources during extraction.
@ -148,8 +149,27 @@ public abstract class Downloader {
/** /**
* Do a request using the specified {@link Request} object. * Do a request using the specified {@link Request} object.
* *
* @param request The request to process
* @return the result of the request * @return the result of the request
*/ */
public abstract Response execute(@Nonnull Request request) public abstract Response execute(@Nonnull Request request)
throws IOException, ReCaptchaException; throws IOException, ReCaptchaException;
/**
* Get the size of the content that the url is pointing by firing a HEAD request.
*
* @param url an url pointing to the content
* @return the size of the content, in bytes or -1 if unknown
*/
public long getContentLength(final String url) {
try {
final String contentLengthHeader = head(url).getHeader("Content-Length");
if (contentLengthHeader == null) {
return -1;
}
return Long.parseLong(contentLengthHeader);
} catch (final Exception e) {
return -1;
}
}
} }

View File

@ -102,6 +102,11 @@ public abstract class AbstractYoutubeDashManifestCreator implements DashManifest
this.durationSecondsFallback = durationSecondsFallback; this.durationSecondsFallback = durationSecondsFallback;
} }
@Override
public long getExpectedContentLength(final Downloader downloader) {
return downloader.getContentLength(itagInfo.getStreamUrl());
}
protected boolean isLiveDelivery() { protected boolean isLiveDelivery() {
return false; return false;
} }

View File

@ -29,8 +29,8 @@ import javax.annotation.Nullable;
* Info object for previews of unopened videos, eg search results, related videos * Info object for previews of unopened videos, eg search results, related videos
*/ */
public class StreamInfoItem extends InfoItem { public class StreamInfoItem extends InfoItem {
private final boolean audioOnly;
private final boolean live; private final boolean live;
private final boolean audioOnly;
private String uploaderName; private String uploaderName;
private String shortDescription; private String shortDescription;
@ -47,11 +47,11 @@ public class StreamInfoItem extends InfoItem {
public StreamInfoItem(final int serviceId, public StreamInfoItem(final int serviceId,
final String url, final String url,
final String name, final String name,
final boolean audioOnly, final boolean live,
final boolean live) { final boolean audioOnly) {
super(InfoType.STREAM, serviceId, url, name); super(InfoType.STREAM, serviceId, url, name);
this.audioOnly = audioOnly;
this.live = live; this.live = live;
this.audioOnly = audioOnly;
} }
public boolean isAudioOnly() { public boolean isAudioOnly() {

View File

@ -48,8 +48,8 @@ public class StreamInfoItemsCollector
getServiceId(), getServiceId(),
extractor.getUrl(), extractor.getUrl(),
extractor.getName(), extractor.getName(),
extractor.isAudioOnly(), extractor.isLive(),
extractor.isLive()); extractor.isAudioOnly());
// optional information // optional information
try { try {

View File

@ -1,5 +1,6 @@
package org.schabi.newpipe.extractor.streamdata.delivery; package org.schabi.newpipe.extractor.streamdata.delivery;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.streamdata.delivery.dashmanifestcreator.DashManifestCreator; import org.schabi.newpipe.extractor.streamdata.delivery.dashmanifestcreator.DashManifestCreator;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -9,4 +10,9 @@ public interface DASHManifestDeliveryData extends DASHDeliveryData {
DashManifestCreator dashManifestCreator(); DashManifestCreator dashManifestCreator();
String getCachedDashManifestAsString(); String getCachedDashManifestAsString();
@Override
default long getExpectedContentLength(final Downloader downloader) {
return dashManifestCreator().getExpectedContentLength(downloader);
}
} }

View File

@ -1,5 +1,13 @@
package org.schabi.newpipe.extractor.streamdata.delivery; package org.schabi.newpipe.extractor.streamdata.delivery;
import org.schabi.newpipe.extractor.downloader.Downloader;
public interface DeliveryData { public interface DeliveryData {
// Only a marker so far /**
* Returns the expected content length/size of the data.
*
* @param downloader The downloader that may be used for fetching (HTTP HEAD).
* @return the expected size/content length or <code>-1</code> if unknown
*/
long getExpectedContentLength(Downloader downloader);
} }

View File

@ -1,8 +1,15 @@
package org.schabi.newpipe.extractor.streamdata.delivery; package org.schabi.newpipe.extractor.streamdata.delivery;
import org.schabi.newpipe.extractor.downloader.Downloader;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public interface UrlBasedDeliveryData extends DeliveryData { public interface UrlBasedDeliveryData extends DeliveryData {
@Nonnull @Nonnull
String url(); String url();
@Override
default long getExpectedContentLength(final Downloader downloader) {
return downloader.getContentLength(url());
}
} }

View File

@ -1,14 +1,23 @@
package org.schabi.newpipe.extractor.streamdata.delivery.dashmanifestcreator; package org.schabi.newpipe.extractor.streamdata.delivery.dashmanifestcreator;
import org.schabi.newpipe.extractor.downloader.Downloader;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public interface DashManifestCreator { public interface DashManifestCreator {
/** /**
* Generates the DASH manifest. * Generates the DASH manifest.
*
* @return The dash manifest as string. * @return The dash manifest as string.
* @throws DashManifestCreationException May throw a CreationException * @throws DashManifestCreationException May throw a CreationException
*/ */
@Nonnull @Nonnull
String generateManifest(); String generateManifest();
/**
* See
* {@link org.schabi.newpipe.extractor.streamdata.delivery.DeliveryData#getExpectedContentLength(Downloader)}
*/
long getExpectedContentLength(Downloader downloader);
} }

View File

@ -39,5 +39,10 @@ public interface SubtitleStream extends Stream<SubtitleMediaFormat> {
* *
* @return the {@link Locale locale} of the subtitles * @return the {@link Locale locale} of the subtitles
*/ */
@Nonnull
Locale locale(); Locale locale();
default String getDisplayLanguageName() {
return locale().getDisplayName(locale());
}
} }

View File

@ -10,5 +10,5 @@ import javax.annotation.Nonnull;
*/ */
public interface VideoStream extends Stream<VideoAudioMediaFormat> { public interface VideoStream extends Stream<VideoAudioMediaFormat> {
@Nonnull @Nonnull
VideoQualityData videoQualityData(); VideoQualityData qualityData();
} }

View File

@ -38,7 +38,7 @@ public class SimpleVideoAudioStreamImpl extends AbstractStreamImpl<VideoAudioMed
@Nonnull @Nonnull
@Override @Override
public VideoQualityData videoQualityData() { public VideoQualityData qualityData() {
return videoQualityData; return videoQualityData;
} }

View File

@ -32,7 +32,7 @@ public class SimpleVideoStreamImpl extends AbstractStreamImpl<VideoAudioMediaFor
@Nonnull @Nonnull
@Override @Override
public VideoQualityData videoQualityData() { public VideoQualityData qualityData() {
return videoQualityData; return videoQualityData;
} }
} }

View File

@ -270,7 +270,7 @@ public abstract class DefaultStreamExtractorTest extends DefaultExtractorTest<St
for (final VideoStream stream : videoOnlyStreams) { for (final VideoStream stream : videoOnlyStreams) {
assertNotNull(stream.mediaFormat()); assertNotNull(stream.mediaFormat());
assertNotNull(stream.videoQualityData()); assertNotNull(stream.qualityData());
checkDeliveryData(stream.deliveryData()); checkDeliveryData(stream.deliveryData());
} }
} else { } else {
@ -289,7 +289,7 @@ public abstract class DefaultStreamExtractorTest extends DefaultExtractorTest<St
for (final VideoAudioStream stream : videoAudioStreams) { for (final VideoAudioStream stream : videoAudioStreams) {
assertNotNull(stream.mediaFormat()); assertNotNull(stream.mediaFormat());
assertNotNull(stream.videoQualityData()); assertNotNull(stream.qualityData());
checkDeliveryData(stream.deliveryData()); checkDeliveryData(stream.deliveryData());
} }
} else { } else {