Skip to content

Commit 66ec54d

Browse files
authored
Update XCDYouTubeVideoOperation.m
1 parent ff6f3ca commit 66ec54d

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

XCDYouTubeKit/XCDYouTubeVideoOperation.m

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#import "XCDYouTubePlayerScript.h"
1414
#import "XCDYouTubeLogger+Private.h"
1515

16-
1716
typedef NS_ENUM(NSUInteger, XCDYouTubeRequestType) {
1817
XCDYouTubeRequestTypeGetVideoInfo = 1,
1918
XCDYouTubeRequestTypeWatchPage,
@@ -150,7 +149,7 @@ - (void) startNextRequest
150149
NSString *eventLabel = [self.eventLabels objectAtIndex:0];
151150
[self.eventLabels removeObjectAtIndex:0];
152151

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" };
154153
NSString *queryString = XCDQueryStringWithDictionary(query);
155154
NSURL *videoInfoURL = [NSURL URLWithString:[@"https://www.youtube.com/get_video_info?" stringByAppendingString:queryString]];
156155
[self startRequestWithURL:videoInfoURL type:XCDYouTubeRequestTypeGetVideoInfo];
@@ -160,7 +159,6 @@ - (void) startNextRequest
160159
- (void) startWatchPageRequest
161160
{
162161
NSDictionary *query = @{ @"v": self.videoIdentifier, @"hl": self.languageIdentifier, @"has_verified": @YES, @"bpctr": @9999999999 };
163-
164162
NSString *queryString = XCDQueryStringWithDictionary(query);
165163
NSURL *webpageURL = [NSURL URLWithString:[@"https://www.youtube.com/watch?" stringByAppendingString:queryString]];
166164
[self startRequestWithURL:webpageURL type:XCDYouTubeRequestTypeWatchPage];
@@ -272,10 +270,49 @@ - (void) handleConnectionError:(NSError *)connectionError requestType:(XCDYouTub
272270

273271
#pragma mark - Response Parsing
274272

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+
275310
- (void) handleVideoInfoResponseWithInfo:(NSDictionary *)info response:(NSURLResponse *)response
276311
{
277312
XCDYouTubeLogDebug(@"Handling video info response");
278313

314+
[self initializeConsentWithResponse:response];
315+
279316
NSError *error = nil;
280317
XCDYouTubeVideo *video = [[XCDYouTubeVideo alloc] initWithIdentifier:self.videoIdentifier info:info playerScript:self.playerScript response:response error:&error];
281318
if (video)
@@ -360,8 +397,8 @@ - (void) handleJavaScriptPlayerWithScript:(NSString *)script
360397
{
361398
NSString *eurl = [@"https://youtube.googleapis.com/v/" stringByAppendingString:self.videoIdentifier];
362399
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);
365402
NSURL *videoInfoURL = [NSURL URLWithString:[@"https://www.youtube.com/get_video_info?" stringByAppendingString:queryString]];
366403
[self startRequestWithURL:videoInfoURL type:XCDYouTubeRequestTypeGetVideoInfo];
367404
}

0 commit comments

Comments
 (0)