13
13
#import " XCDYouTubePlayerScript.h"
14
14
#import " XCDYouTubeLogger+Private.h"
15
15
16
-
17
16
typedef NS_ENUM (NSUInteger , XCDYouTubeRequestType) {
18
17
XCDYouTubeRequestTypeGetVideoInfo = 1 ,
19
18
XCDYouTubeRequestTypeWatchPage,
@@ -150,7 +149,7 @@ - (void) startNextRequest
150
149
NSString *eventLabel = [self .eventLabels objectAtIndex: 0 ];
151
150
[self .eventLabels removeObjectAtIndex: 0 ];
152
151
153
- NSDictionary *query = @{ @" video_id" : self.videoIdentifier , @" hl" : self.languageIdentifier , @" el" : eventLabel, @" ps" : @" default" , @" html5" : @1 };
152
+ NSDictionary *query = @{ @" video_id" : self.videoIdentifier , @" hl" : self.languageIdentifier , @" el" : eventLabel, @" ps" : @" default" , @" html5" : @" 1 " };
154
153
NSString *queryString = XCDQueryStringWithDictionary (query);
155
154
NSURL *videoInfoURL = [NSURL URLWithString: [@" https://www.youtube.com/get_video_info?" stringByAppendingString: queryString]];
156
155
[self startRequestWithURL: videoInfoURL type: XCDYouTubeRequestTypeGetVideoInfo];
@@ -160,7 +159,6 @@ - (void) startNextRequest
160
159
- (void ) startWatchPageRequest
161
160
{
162
161
NSDictionary *query = @{ @" v" : self.videoIdentifier , @" hl" : self.languageIdentifier , @" has_verified" : @YES , @" bpctr" : @9999999999 };
163
-
164
162
NSString *queryString = XCDQueryStringWithDictionary (query);
165
163
NSURL *webpageURL = [NSURL URLWithString: [@" https://www.youtube.com/watch?" stringByAppendingString: queryString]];
166
164
[self startRequestWithURL: webpageURL type: XCDYouTubeRequestTypeWatchPage];
@@ -272,10 +270,49 @@ - (void) handleConnectionError:(NSError *)connectionError requestType:(XCDYouTub
272
270
273
271
#pragma mark - Response Parsing
274
272
273
+ - (void ) initializeConsentWithResponse : (NSURLResponse *)response {
274
+ NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
275
+ if (httpResponse && response.URL ) {
276
+ NSArray <NSHTTPCookie *> *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: httpResponse.allHeaderFields forURL: (NSURL *_Nonnull)response.URL];
277
+
278
+ for (NSHTTPCookie *cookie in cookies) {
279
+ if ([cookie.name isEqualToString: @" __Secure-3PSID" ]) return ;
280
+ }
281
+
282
+ for (NSHTTPCookie *cookie in cookies) {
283
+ if ([cookie.name isEqualToString: @" CONSENT" ]) {
284
+ if ([cookie.value isEqualToString: @" YES" ]) return ;
285
+
286
+ NSString *rawConsentID = [cookie.value stringByReplacingOccurrencesOfString: @" PENDING+" withString: @" " ];
287
+ int consentID = [rawConsentID intValue ];
288
+
289
+ // generate random consent id, if doesn't match expected format
290
+ if (consentID < 100 || consentID > 999 ) {
291
+ consentID = 100 + (int )arc4random_uniform ((uint32_t )(999 - 100 + 1 ));
292
+ }
293
+
294
+ NSString *cookieValue = [[NSString alloc ] initWithFormat: @" YES+cb.20210328-17-p0.en+FX+%i " , consentID];
295
+ NSHTTPCookie *consentCookie = [NSHTTPCookie cookieWithProperties: @{
296
+ NSHTTPCookiePath : @" /" ,
297
+ NSHTTPCookieName : @" CONSENT" ,
298
+ NSHTTPCookieValue : cookieValue,
299
+ NSHTTPCookieDomain :@" .youtube.com" ,
300
+ NSHTTPCookieSecure :@" TRUE"
301
+ }];
302
+ [self .session.configuration.HTTPCookieStorage setCookie: consentCookie];
303
+ return ;
304
+ }
305
+ }
306
+
307
+ }
308
+ }
309
+
275
310
- (void ) handleVideoInfoResponseWithInfo : (NSDictionary *)info response : (NSURLResponse *)response
276
311
{
277
312
XCDYouTubeLogDebug (@" Handling video info response" );
278
313
314
+ [self initializeConsentWithResponse: response];
315
+
279
316
NSError *error = nil ;
280
317
XCDYouTubeVideo *video = [[XCDYouTubeVideo alloc ] initWithIdentifier: self .videoIdentifier info: info playerScript: self .playerScript response: response error: &error];
281
318
if (video)
@@ -360,8 +397,8 @@ - (void) handleJavaScriptPlayerWithScript:(NSString *)script
360
397
{
361
398
NSString *eurl = [@" https://youtube.googleapis.com/v/" stringByAppendingString: self .videoIdentifier];
362
399
NSString *sts = self.embedWebpage .sts ?: self.webpage .sts ?: @" " ;
363
- NSDictionary *query = @{ @" video_id" : self.videoIdentifier , @" hl" : self.languageIdentifier , @" eurl" : eurl, @" sts" : sts, @" html5 " : @ 1 };
364
- NSString *queryString = XCDQueryStringWithDictionary (query);
400
+ NSDictionary *query = @{ @" video_id" : self.videoIdentifier , @" hl" : self.languageIdentifier , @" eurl" : eurl, @" sts" : sts};
401
+ NSString *queryString = XCDQueryStringWithDictionary (query);
365
402
NSURL *videoInfoURL = [NSURL URLWithString: [@" https://www.youtube.com/get_video_info?" stringByAppendingString: queryString]];
366
403
[self startRequestWithURL: videoInfoURL type: XCDYouTubeRequestTypeGetVideoInfo];
367
404
}
0 commit comments