Skip to content

Commit b8e050f

Browse files
committed
Adapt YoutubeHttpDataSource to extractor changes and improve requests
Always use POST requests and the same body that official HTML5 clients use for a while.
1 parent 46d0bc1 commit b8e050f

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/datasource/YoutubeHttpDataSource.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
import static com.google.android.exoplayer2.util.Util.castNonNull;
1515
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getAndroidUserAgent;
1616
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getIosUserAgent;
17+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTvHtml5UserAgent;
1718
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isAndroidStreamingUrl;
1819
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isIosStreamingUrl;
20+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isTvHtml5StreamingUrl;
1921
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isWebStreamingUrl;
20-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isTvHtml5SimplyEmbeddedPlayerStreamingUrl;
22+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isWebEmbeddedPlayerStreamingUrl;
2123
import static java.lang.Math.min;
2224

2325
import android.net.Uri;
@@ -270,6 +272,7 @@ public YoutubeHttpDataSource createDataSource() {
270272

271273
private static final String RN_PARAMETER = "&rn=";
272274
private static final String YOUTUBE_BASE_URL = "https://www.youtube.com";
275+
private static final byte[] POST_BODY = new byte[] {0x78, 0};
273276

274277
private final boolean allowCrossProtocolRedirects;
275278
private final boolean rangeParameterEnabled;
@@ -658,8 +661,11 @@ private HttpURLConnection makeConnection(
658661
}
659662
}
660663

664+
final boolean isTvHtml5StreamingUrl = isTvHtml5StreamingUrl(requestUrl);
665+
661666
if (isWebStreamingUrl(requestUrl)
662-
|| isTvHtml5SimplyEmbeddedPlayerStreamingUrl(requestUrl)) {
667+
|| isTvHtml5StreamingUrl
668+
|| isWebEmbeddedPlayerStreamingUrl(requestUrl)) {
663669
httpURLConnection.setRequestProperty(HttpHeaders.ORIGIN, YOUTUBE_BASE_URL);
664670
httpURLConnection.setRequestProperty(HttpHeaders.REFERER, YOUTUBE_BASE_URL);
665671
httpURLConnection.setRequestProperty(HttpHeaders.SEC_FETCH_DEST, "empty");
@@ -679,6 +685,9 @@ private HttpURLConnection makeConnection(
679685
} else if (isIosStreamingUrl) {
680686
httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT,
681687
getIosUserAgent(null));
688+
} else if (isTvHtml5StreamingUrl) {
689+
httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT,
690+
getTvHtml5UserAgent());
682691
} else {
683692
// non-mobile user agent
684693
httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, DownloaderImpl.USER_AGENT);
@@ -687,22 +696,16 @@ private HttpURLConnection makeConnection(
687696
httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_ENCODING,
688697
allowGzip ? "gzip" : "identity");
689698
httpURLConnection.setInstanceFollowRedirects(followRedirects);
690-
httpURLConnection.setDoOutput(httpBody != null);
691-
692-
// Mobile clients uses POST requests to fetch contents
693-
httpURLConnection.setRequestMethod(isAndroidStreamingUrl || isIosStreamingUrl
694-
? "POST"
695-
: DataSpec.getStringForHttpMethod(httpMethod));
696-
697-
if (httpBody != null) {
698-
httpURLConnection.setFixedLengthStreamingMode(httpBody.length);
699-
httpURLConnection.connect();
700-
final OutputStream os = httpURLConnection.getOutputStream();
701-
os.write(httpBody);
702-
os.close();
703-
} else {
704-
httpURLConnection.connect();
705-
}
699+
// Most clients use POST requests to fetch contents
700+
httpURLConnection.setRequestMethod("POST");
701+
httpURLConnection.setDoOutput(true);
702+
httpURLConnection.setFixedLengthStreamingMode(POST_BODY.length);
703+
httpURLConnection.connect();
704+
705+
final OutputStream os = httpURLConnection.getOutputStream();
706+
os.write(POST_BODY);
707+
os.close();
708+
706709
return httpURLConnection;
707710
}
708711

0 commit comments

Comments
 (0)