Skip to content

Commit 889b5f2

Browse files
committed
Improvements for NewPipe
* Added support for content-length (important for downloader) * Code cleanup
1 parent e3c989e commit 889b5f2

File tree

13 files changed

+74
-14
lines changed

13 files changed

+74
-14
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Downloader.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
55
import org.schabi.newpipe.extractor.localization.Localization;
66

7-
import javax.annotation.Nonnull;
8-
import javax.annotation.Nullable;
97
import java.io.IOException;
108
import java.util.List;
119
import java.util.Map;
1210

11+
import javax.annotation.Nonnull;
12+
import javax.annotation.Nullable;
13+
1314
/**
1415
* A base for downloader implementations that NewPipe will use
1516
* to download needed resources during extraction.
@@ -148,8 +149,27 @@ public Response post(final String url,
148149
/**
149150
* Do a request using the specified {@link Request} object.
150151
*
152+
* @param request The request to process
151153
* @return the result of the request
152154
*/
153155
public abstract Response execute(@Nonnull Request request)
154156
throws IOException, ReCaptchaException;
157+
158+
/**
159+
* Get the size of the content that the url is pointing by firing a HEAD request.
160+
*
161+
* @param url an url pointing to the content
162+
* @return the size of the content, in bytes or -1 if unknown
163+
*/
164+
public long getContentLength(final String url) {
165+
try {
166+
final String contentLengthHeader = head(url).getHeader("Content-Length");
167+
if (contentLengthHeader == null) {
168+
return -1;
169+
}
170+
return Long.parseLong(contentLengthHeader);
171+
} catch (final Exception e) {
172+
return -1;
173+
}
174+
}
155175
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/dashmanifestcreator/AbstractYoutubeDashManifestCreator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ protected AbstractYoutubeDashManifestCreator(
102102
this.durationSecondsFallback = durationSecondsFallback;
103103
}
104104

105+
@Override
106+
public long getExpectedContentLength(final Downloader downloader) {
107+
return downloader.getContentLength(itagInfo.getStreamUrl());
108+
}
109+
105110
protected boolean isLiveDelivery() {
106111
return false;
107112
}

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
* Info object for previews of unopened videos, eg search results, related videos
3030
*/
3131
public class StreamInfoItem extends InfoItem {
32-
private final boolean audioOnly;
3332
private final boolean live;
33+
private final boolean audioOnly;
3434

3535
private String uploaderName;
3636
private String shortDescription;
@@ -47,11 +47,11 @@ public class StreamInfoItem extends InfoItem {
4747
public StreamInfoItem(final int serviceId,
4848
final String url,
4949
final String name,
50-
final boolean audioOnly,
51-
final boolean live) {
50+
final boolean live,
51+
final boolean audioOnly) {
5252
super(InfoType.STREAM, serviceId, url, name);
53-
this.audioOnly = audioOnly;
5453
this.live = live;
54+
this.audioOnly = audioOnly;
5555
}
5656

5757
public boolean isAudioOnly() {

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public StreamInfoItem extract(final StreamInfoItemExtractor extractor) throws Pa
4848
getServiceId(),
4949
extractor.getUrl(),
5050
extractor.getName(),
51-
extractor.isAudioOnly(),
52-
extractor.isLive());
51+
extractor.isLive(),
52+
extractor.isAudioOnly());
5353

5454
// optional information
5555
try {

extractor/src/main/java/org/schabi/newpipe/extractor/streamdata/delivery/DASHManifestDeliveryData.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.schabi.newpipe.extractor.streamdata.delivery;
22

3+
import org.schabi.newpipe.extractor.downloader.Downloader;
34
import org.schabi.newpipe.extractor.streamdata.delivery.dashmanifestcreator.DashManifestCreator;
45

56
import javax.annotation.Nonnull;
@@ -9,4 +10,9 @@ public interface DASHManifestDeliveryData extends DASHDeliveryData {
910
DashManifestCreator dashManifestCreator();
1011

1112
String getCachedDashManifestAsString();
13+
14+
@Override
15+
default long getExpectedContentLength(final Downloader downloader) {
16+
return dashManifestCreator().getExpectedContentLength(downloader);
17+
}
1218
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package org.schabi.newpipe.extractor.streamdata.delivery;
22

3+
import org.schabi.newpipe.extractor.downloader.Downloader;
4+
35
public interface DeliveryData {
4-
// Only a marker so far
6+
/**
7+
* Returns the expected content length/size of the data.
8+
*
9+
* @param downloader The downloader that may be used for fetching (HTTP HEAD).
10+
* @return the expected size/content length or <code>-1</code> if unknown
11+
*/
12+
long getExpectedContentLength(Downloader downloader);
513
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package org.schabi.newpipe.extractor.streamdata.delivery;
22

3+
import org.schabi.newpipe.extractor.downloader.Downloader;
4+
35
import javax.annotation.Nonnull;
46

57
public interface UrlBasedDeliveryData extends DeliveryData {
68
@Nonnull
79
String url();
10+
11+
@Override
12+
default long getExpectedContentLength(final Downloader downloader) {
13+
return downloader.getContentLength(url());
14+
}
815
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package org.schabi.newpipe.extractor.streamdata.delivery.dashmanifestcreator;
22

3+
import org.schabi.newpipe.extractor.downloader.Downloader;
4+
35
import javax.annotation.Nonnull;
46

57
public interface DashManifestCreator {
68

79
/**
810
* Generates the DASH manifest.
11+
*
912
* @return The dash manifest as string.
1013
* @throws DashManifestCreationException May throw a CreationException
1114
*/
1215
@Nonnull
1316
String generateManifest();
17+
18+
/**
19+
* See
20+
* {@link org.schabi.newpipe.extractor.streamdata.delivery.DeliveryData#getExpectedContentLength(Downloader)}
21+
*/
22+
long getExpectedContentLength(Downloader downloader);
1423
}

extractor/src/main/java/org/schabi/newpipe/extractor/streamdata/stream/SubtitleStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@ default boolean autoGenerated() {
3939
*
4040
* @return the {@link Locale locale} of the subtitles
4141
*/
42+
@Nonnull
4243
Locale locale();
44+
45+
default String getDisplayLanguageName() {
46+
return locale().getDisplayName(locale());
47+
}
4348
}

extractor/src/main/java/org/schabi/newpipe/extractor/streamdata/stream/VideoStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
*/
1111
public interface VideoStream extends Stream<VideoAudioMediaFormat> {
1212
@Nonnull
13-
VideoQualityData videoQualityData();
13+
VideoQualityData qualityData();
1414
}

0 commit comments

Comments
 (0)