Skip to content

Commit

Permalink
Add expirationDate property to XCDYouTubeVideo
Browse files Browse the repository at this point in the history
Fixes 0xced#96
  • Loading branch information
0xced committed Dec 5, 2014
1 parent d0bab52 commit e346864
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### Version 2.1.0

* New `expirationDate` property on the `XCDYouTubeVideo` class. (#96)

#### Version 2.0.3

* Adaptation to YouTube API change. (#94)
Expand Down
3 changes: 3 additions & 0 deletions XCDYouTubeKit Tests/XCDYouTubeClientTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ - (void) testMobileRestrictedVideo
{
XCTAssertNil(error);
XCTAssertNotNil(video.title);
XCTAssertNotNil(video.expirationDate);
XCTAssertNotNil(video.smallThumbnailURL);
XCTAssertNotNil(video.mediumThumbnailURL);
XCTAssertNotNil(video.largeThumbnailURL);
Expand All @@ -67,6 +68,7 @@ - (void) testLiveVideo_offline
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:@"xrM34fdmloc" completionHandler:^(XCDYouTubeVideo *video, NSError *error)
{
XCTAssertNil(error);
XCTAssertNil(video.expirationDate);
XCTAssertNotNil(video.title);
XCTAssertNotNil(video.smallThumbnailURL);
XCTAssertNotNil(video.mediumThumbnailURL);
Expand All @@ -85,6 +87,7 @@ - (void) testDVRVideo
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:@"H7iQ4sAf0OE" completionHandler:^(XCDYouTubeVideo *video, NSError *error)
{
XCTAssertNil(error);
XCTAssertNil(video.expirationDate);
XCTAssertNotNil(video.title);
XCTAssertNotNil(video.smallThumbnailURL);
XCTAssertNotNil(video.mediumThumbnailURL);
Expand Down
5 changes: 5 additions & 0 deletions XCDYouTubeKit Tests/XCDYouTubeProtectedVideosTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ - (void) testAgeRestrictedVideo
{
XCTAssertNil(error);
XCTAssertNotNil(video.title);
XCTAssertNotNil(video.expirationDate);
XCTAssertNotNil(video.smallThumbnailURL);
XCTAssertNotNil(video.mediumThumbnailURL);
XCTAssertNotNil(video.largeThumbnailURL);
Expand All @@ -39,6 +40,7 @@ - (void) testProtectedVEVOVideo1
{
XCTAssertNil(error);
XCTAssertNotNil(video.title);
XCTAssertNotNil(video.expirationDate);
XCTAssertNotNil(video.smallThumbnailURL);
XCTAssertNotNil(video.mediumThumbnailURL);
XCTAssertNotNil(video.largeThumbnailURL);
Expand All @@ -60,6 +62,7 @@ - (void) testProtectedVEVOVideo2
{
XCTAssertNil(error);
XCTAssertNotNil(video.title);
XCTAssertNotNil(video.expirationDate);
XCTAssertNotNil(video.smallThumbnailURL);
XCTAssertNotNil(video.mediumThumbnailURL);
XCTAssertNotNil(video.largeThumbnailURL);
Expand All @@ -81,6 +84,7 @@ - (void) testProtectedVEVOVideo3
{
XCTAssertNil(error);
XCTAssertNotNil(video.title);
XCTAssertNotNil(video.expirationDate);
XCTAssertNotNil(video.smallThumbnailURL);
XCTAssertNotNil(video.mediumThumbnailURL);
XCTAssertNotNil(video.largeThumbnailURL);
Expand All @@ -102,6 +106,7 @@ - (void) testAgeRestrictedVEVOVideo
{
XCTAssertNil(error);
XCTAssertNotNil(video.title);
XCTAssertNotNil(video.expirationDate);
XCTAssertNotNil(video.smallThumbnailURL);
XCTAssertNotNil(video.mediumThumbnailURL);
XCTAssertNotNil(video.largeThumbnailURL);
Expand Down
10 changes: 9 additions & 1 deletion XCDYouTubeKit/XCDYouTubeVideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ extern NSString *const XCDYouTubeVideoQualityHTTPLiveStreaming;
*
* The keys are the YouTube [itag](https://en.wikipedia.org/wiki/YouTube#Quality_and_codecs) values as `NSNumber` objects. The values are the video URLs as `NSURL` objects. There is also the special `XCDYouTubeVideoQualityHTTPLiveStreaming` key for live videos.
*
* You must not store the URLs for later use since they have a limited lifetime.
* You should not store the URLs for later use since they have a limited lifetime and are bound to an IP address.
*
* @see XCDYouTubeVideoQuality
* @see expirationDate
*/
@property (nonatomic, readonly) NSDictionary *streamURLs;

/**
* The expiration date of the video.
*
* After this date, the stream URLs will not be playable. May be nil if it can not be determined, for example in live videos.
*/
@property (nonatomic, readonly) NSDate *expirationDate;

@end
12 changes: 11 additions & 1 deletion XCDYouTubeKit/XCDYouTubeVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@

@implementation XCDYouTubeVideo

static NSDate * ExpirationDate(NSURL *streamURL)
{
NSDictionary *query = XCDDictionaryWithQueryString(streamURL.query, NSUTF8StringEncoding);
NSTimeInterval expire = [query[@"expire"] doubleValue];
return expire > 0 ? [NSDate dateWithTimeIntervalSince1970:expire] : nil;
}

- (instancetype) initWithIdentifier:(NSString *)identifier info:(NSDictionary *)info playerScript:(XCDYouTubePlayerScript *)playerScript response:(NSURLResponse *)response error:(NSError * __autoreleasing *)error
{
if (!(self = [super init]))
Expand Down Expand Up @@ -113,6 +120,9 @@ - (instancetype) initWithIdentifier:(NSString *)identifier info:(NSDictionary *)
if (urlString && itag)
{
NSURL *streamURL = [NSURL URLWithString:urlString];
if (!_expirationDate)
_expirationDate = ExpirationDate(streamURL);

if (signature)
streamURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@&signature=%@", urlString, [signature stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

Expand Down Expand Up @@ -188,7 +198,7 @@ - (NSString *) description
- (NSString *) debugDescription
{
NSString *thumbnailDescription = [NSString stringWithFormat:@"Small thumbnail: %@\nMedium thumbnail: %@\nLarge thumbnail: %@", self.smallThumbnailURL, self.mediumThumbnailURL, self.largeThumbnailURL];
return [NSString stringWithFormat:@"<%@: %p> %@\nDuration: %@ seconds\n%@\nVideo Streams: %@", self.class, self, self.description, @(self.duration), thumbnailDescription, self.streamURLs];
return [NSString stringWithFormat:@"<%@: %p> %@\nDuration: %@ seconds\nExpiration date: %@\n%@\nVideo Streams: %@", self.class, self, self.description, @(self.duration), self.expirationDate, thumbnailDescription, self.streamURLs];
}

#pragma mark - NSCopying
Expand Down

0 comments on commit e346864

Please sign in to comment.