mirror of
				https://github.com/TeamNewPipe/NewPipe
				synced 2025-10-30 23:03:00 +00:00 
			
		
		
		
	Add comments and use downloader user agent in YT data source
YoutubeHttpDataSource
This commit is contained in:
		| @@ -44,6 +44,8 @@ import com.google.common.collect.ImmutableMap; | |||||||
| import com.google.common.collect.Sets; | import com.google.common.collect.Sets; | ||||||
| import com.google.common.net.HttpHeaders; | import com.google.common.net.HttpHeaders; | ||||||
|  |  | ||||||
|  | import org.schabi.newpipe.DownloaderImpl; | ||||||
|  |  | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.io.InputStream; | import java.io.InputStream; | ||||||
| import java.io.InterruptedIOException; | import java.io.InterruptedIOException; | ||||||
| @@ -69,6 +71,10 @@ import java.util.zip.GZIPInputStream; | |||||||
|  * (only where it's relevant) and also more parameters, such as {@code rn} and replaces the use of |  * (only where it's relevant) and also more parameters, such as {@code rn} and replaces the use of | ||||||
|  * the {@code Range} header by the corresponding parameter ({@code range}), if enabled. |  * the {@code Range} header by the corresponding parameter ({@code range}), if enabled. | ||||||
|  * </p> |  * </p> | ||||||
|  |  * | ||||||
|  |  * There are many unused methods in this class because everything was copied from {@link | ||||||
|  |  * com.google.android.exoplayer2.upstream.DefaultHttpDataSource} with as little changes as possible. | ||||||
|  |  * SonarQube warnings were also suppressed for the same reason. | ||||||
|  */ |  */ | ||||||
| @SuppressWarnings({"squid:S3011", "squid:S4738"}) | @SuppressWarnings({"squid:S3011", "squid:S4738"}) | ||||||
| public final class YoutubeHttpDataSource extends BaseDataSource implements HttpDataSource { | public final class YoutubeHttpDataSource extends BaseDataSource implements HttpDataSource { | ||||||
| @@ -89,8 +95,6 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|         private boolean allowCrossProtocolRedirects; |         private boolean allowCrossProtocolRedirects; | ||||||
|         private boolean keepPostFor302Redirects; |         private boolean keepPostFor302Redirects; | ||||||
|  |  | ||||||
|         @Nullable |  | ||||||
|         private String userAgentForNonMobileStreams; |  | ||||||
|         private boolean rangeParameterEnabled; |         private boolean rangeParameterEnabled; | ||||||
|         private boolean rnParameterEnabled; |         private boolean rnParameterEnabled; | ||||||
|  |  | ||||||
| @@ -111,25 +115,6 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|             return this; |             return this; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         /** |  | ||||||
|          * Sets the user agent that will be used, only for non-mobile streams. |  | ||||||
|          * |  | ||||||
|          * <p> |  | ||||||
|          * The default is {@code null}, which causes the default user agent of the underlying |  | ||||||
|          * platform to be used. |  | ||||||
|          * </p> |  | ||||||
|          * |  | ||||||
|          * @param userAgentForNonMobileStreamsValue The user agent that will be used for non-mobile |  | ||||||
|          *                                          streams, or {@code null} to use the default |  | ||||||
|          *                                          user agent of the underlying platform. |  | ||||||
|          * @return This factory. |  | ||||||
|          */ |  | ||||||
|         public Factory setUserAgentForNonMobileStreams( |  | ||||||
|                 @Nullable final String userAgentForNonMobileStreamsValue) { |  | ||||||
|             userAgentForNonMobileStreams = userAgentForNonMobileStreamsValue; |  | ||||||
|             return this; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         /** |         /** | ||||||
|          * Sets the connect timeout, in milliseconds. |          * Sets the connect timeout, in milliseconds. | ||||||
|          * |          * | ||||||
| @@ -262,7 +247,6 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|         @Override |         @Override | ||||||
|         public YoutubeHttpDataSource createDataSource() { |         public YoutubeHttpDataSource createDataSource() { | ||||||
|             final YoutubeHttpDataSource dataSource = new YoutubeHttpDataSource( |             final YoutubeHttpDataSource dataSource = new YoutubeHttpDataSource( | ||||||
|                     userAgentForNonMobileStreams, |  | ||||||
|                     connectTimeoutMs, |                     connectTimeoutMs, | ||||||
|                     readTimeoutMs, |                     readTimeoutMs, | ||||||
|                     allowCrossProtocolRedirects, |                     allowCrossProtocolRedirects, | ||||||
| @@ -294,8 +278,6 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|     private final int connectTimeoutMillis; |     private final int connectTimeoutMillis; | ||||||
|     private final int readTimeoutMillis; |     private final int readTimeoutMillis; | ||||||
|     @Nullable |     @Nullable | ||||||
|     private final String userAgent; |  | ||||||
|     @Nullable |  | ||||||
|     private final RequestProperties defaultRequestProperties; |     private final RequestProperties defaultRequestProperties; | ||||||
|     private final RequestProperties requestProperties; |     private final RequestProperties requestProperties; | ||||||
|     private final boolean keepPostFor302Redirects; |     private final boolean keepPostFor302Redirects; | ||||||
| @@ -316,8 +298,7 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|     private long requestNumber; |     private long requestNumber; | ||||||
|  |  | ||||||
|     @SuppressWarnings("checkstyle:ParameterNumber") |     @SuppressWarnings("checkstyle:ParameterNumber") | ||||||
|     private YoutubeHttpDataSource(@Nullable final String userAgent, |     private YoutubeHttpDataSource(final int connectTimeoutMillis, | ||||||
|                                   final int connectTimeoutMillis, |  | ||||||
|                                   final int readTimeoutMillis, |                                   final int readTimeoutMillis, | ||||||
|                                   final boolean allowCrossProtocolRedirects, |                                   final boolean allowCrossProtocolRedirects, | ||||||
|                                   final boolean rangeParameterEnabled, |                                   final boolean rangeParameterEnabled, | ||||||
| @@ -326,7 +307,6 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|                                   @Nullable final Predicate<String> contentTypePredicate, |                                   @Nullable final Predicate<String> contentTypePredicate, | ||||||
|                                   final boolean keepPostFor302Redirects) { |                                   final boolean keepPostFor302Redirects) { | ||||||
|         super(true); |         super(true); | ||||||
|         this.userAgent = userAgent; |  | ||||||
|         this.connectTimeoutMillis = connectTimeoutMillis; |         this.connectTimeoutMillis = connectTimeoutMillis; | ||||||
|         this.readTimeoutMillis = readTimeoutMillis; |         this.readTimeoutMillis = readTimeoutMillis; | ||||||
|         this.allowCrossProtocolRedirects = allowCrossProtocolRedirects; |         this.allowCrossProtocolRedirects = allowCrossProtocolRedirects; | ||||||
| @@ -637,6 +617,8 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|             final boolean allowGzip, |             final boolean allowGzip, | ||||||
|             final boolean followRedirects, |             final boolean followRedirects, | ||||||
|             final Map<String, String> requestParameters) throws IOException { |             final Map<String, String> requestParameters) throws IOException { | ||||||
|  |         // This is the method that contains breaking changes with respect to DefaultHttpDataSource! | ||||||
|  |  | ||||||
|         String requestUrl = url.toString(); |         String requestUrl = url.toString(); | ||||||
|  |  | ||||||
|         // Don't add the request number parameter if it has been already added (for instance in |         // Don't add the request number parameter if it has been already added (for instance in | ||||||
| @@ -687,18 +669,19 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|  |  | ||||||
|         httpURLConnection.setRequestProperty(HttpHeaders.TE, "trailers"); |         httpURLConnection.setRequestProperty(HttpHeaders.TE, "trailers"); | ||||||
|  |  | ||||||
|         final boolean isAnAndroidStreamingUrl = isAndroidStreamingUrl(requestUrl); |         final boolean isAndroidStreamingUrl = isAndroidStreamingUrl(requestUrl); | ||||||
|         final boolean isAnIosStreamingUrl = isIosStreamingUrl(requestUrl); |         final boolean isIosStreamingUrl = isIosStreamingUrl(requestUrl); | ||||||
|         if (isAnAndroidStreamingUrl) { |         if (isAndroidStreamingUrl) { | ||||||
|             // Improvement which may be done: find the content country used to request YouTube |             // Improvement which may be done: find the content country used to request YouTube | ||||||
|             // contents to add it in the user agent instead of using the default |             // contents to add it in the user agent instead of using the default | ||||||
|             httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, |             httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, | ||||||
|                     getAndroidUserAgent(null)); |                     getAndroidUserAgent(null)); | ||||||
|         } else if (isAnIosStreamingUrl) { |         } else if (isIosStreamingUrl) { | ||||||
|             httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, |             httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, | ||||||
|                     getIosUserAgent(null)); |                     getIosUserAgent(null)); | ||||||
|         } else if (userAgent != null) { |         } else { | ||||||
|             httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, userAgent); |             // non-mobile user agent | ||||||
|  |             httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, DownloaderImpl.USER_AGENT); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_ENCODING, |         httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_ENCODING, | ||||||
| @@ -707,7 +690,7 @@ public final class YoutubeHttpDataSource extends BaseDataSource implements HttpD | |||||||
|         httpURLConnection.setDoOutput(httpBody != null); |         httpURLConnection.setDoOutput(httpBody != null); | ||||||
|  |  | ||||||
|         // Mobile clients uses POST requests to fetch contents |         // Mobile clients uses POST requests to fetch contents | ||||||
|         httpURLConnection.setRequestMethod(isAnAndroidStreamingUrl || isAnIosStreamingUrl |         httpURLConnection.setRequestMethod(isAndroidStreamingUrl || isIosStreamingUrl | ||||||
|                 ? "POST" |                 ? "POST" | ||||||
|                 : DataSpec.getStringForHttpMethod(httpMethod)); |                 : DataSpec.getStringForHttpMethod(httpMethod)); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -93,11 +93,11 @@ public class PlayerDataSource { | |||||||
|  |  | ||||||
|         // YouTube-specific data source factories use getYoutubeHttpDataSourceFactory() |         // YouTube-specific data source factories use getYoutubeHttpDataSourceFactory() | ||||||
|         ytHlsCacheDataSourceFactory = new CacheFactory(context, transferListener, cache, |         ytHlsCacheDataSourceFactory = new CacheFactory(context, transferListener, cache, | ||||||
|                 getYoutubeHttpDataSourceFactory(false, false, userAgent)); |                 getYoutubeHttpDataSourceFactory(false, false)); | ||||||
|         ytDashCacheDataSourceFactory = new CacheFactory(context, transferListener, cache, |         ytDashCacheDataSourceFactory = new CacheFactory(context, transferListener, cache, | ||||||
|                 getYoutubeHttpDataSourceFactory(true, true, userAgent)); |                 getYoutubeHttpDataSourceFactory(true, true)); | ||||||
|         ytProgressiveDashCacheDataSourceFactory = new CacheFactory(context, transferListener, cache, |         ytProgressiveDashCacheDataSourceFactory = new CacheFactory(context, transferListener, cache, | ||||||
|                 getYoutubeHttpDataSourceFactory(false, true, userAgent)); |                 getYoutubeHttpDataSourceFactory(false, true)); | ||||||
|  |  | ||||||
|         // set the maximum size to manifest creators |         // set the maximum size to manifest creators | ||||||
|         YoutubeProgressiveDashManifestCreator.getCache().setMaximumSize(MAX_MANIFEST_CACHE_SIZE); |         YoutubeProgressiveDashManifestCreator.getCache().setMaximumSize(MAX_MANIFEST_CACHE_SIZE); | ||||||
| @@ -187,12 +187,10 @@ public class PlayerDataSource { | |||||||
|  |  | ||||||
|     private static YoutubeHttpDataSource.Factory getYoutubeHttpDataSourceFactory( |     private static YoutubeHttpDataSource.Factory getYoutubeHttpDataSourceFactory( | ||||||
|             final boolean rangeParameterEnabled, |             final boolean rangeParameterEnabled, | ||||||
|             final boolean rnParameterEnabled, |             final boolean rnParameterEnabled) { | ||||||
|             final String userAgent) { |  | ||||||
|         return new YoutubeHttpDataSource.Factory() |         return new YoutubeHttpDataSource.Factory() | ||||||
|                 .setRangeParameterEnabled(rangeParameterEnabled) |                 .setRangeParameterEnabled(rangeParameterEnabled) | ||||||
|                 .setRnParameterEnabled(rnParameterEnabled) |                 .setRnParameterEnabled(rnParameterEnabled); | ||||||
|                 .setUserAgentForNonMobileStreams(userAgent); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static void instantiateCacheIfNeeded(final Context context) { |     private static void instantiateCacheIfNeeded(final Context context) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Stypox
					Stypox