VLCNetworkServerBrowserPlex.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserPlex.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCNetworkServerBrowserPlex.h"
  13. #import "VLCPlexParser.h"
  14. #import "VLCPlexWebAPI.h"
  15. @interface VLCNetworkServerBrowserPlex ()
  16. @property (nonatomic, readonly) NSString *addressOrName;
  17. @property (nonatomic, readonly) NSUInteger port;
  18. @property (nonatomic, readwrite) NSString *title;
  19. @property (nonatomic, readonly) NSString *plexServerAddress;
  20. @property (nonatomic, readonly) NSNumber *plexServerPort;
  21. @property (nonatomic, readonly) NSString *plexServerPath;
  22. @property (nonatomic) NSString *plexAuthentification;
  23. @property (nonatomic, readonly) VLCPlexParser *plexParser;
  24. @property (nonatomic, readonly) VLCPlexWebAPI *plexWebAPI;
  25. @property (nonatomic, readonly) NSOperationQueue *plexQueue;
  26. @property (nonatomic, readwrite) NSArray<id<VLCNetworkServerBrowserItem>> *items;
  27. @end
  28. @implementation VLCNetworkServerBrowserPlex
  29. @synthesize title = _title, delegate = _delegate, items = _items;
  30. - (instancetype)initWithName:(NSString *)name host:(NSString *)addressOrName portNumber:(NSNumber *)portNumber path:(NSString *)path authentificication:(NSString *)auth
  31. {
  32. self = [super init];
  33. if (self) {
  34. _title = name;
  35. _plexServerAddress = addressOrName;
  36. _plexServerPort = portNumber;
  37. _plexServerPath = path;
  38. _plexAuthentification = auth;
  39. _plexParser = [[VLCPlexParser alloc] init];
  40. _plexWebAPI = [[VLCPlexWebAPI alloc] init];
  41. _plexQueue = [[NSOperationQueue alloc] init];
  42. _plexQueue.maxConcurrentOperationCount = 1;
  43. _plexQueue.name = @"org.videolan.vlc-ios.plex.update";
  44. _items = [NSArray new];
  45. }
  46. return self;
  47. }
  48. - (instancetype)initWithName:(NSString *)name url:(NSURL *)url auth:(NSString *)auth {
  49. return [self initWithName:name host:url.host portNumber:url.port path:url.path authentificication:auth];
  50. }
  51. - (void)update {
  52. [self.plexQueue addOperationWithBlock:^{
  53. [self loadContents];
  54. }];
  55. }
  56. - (void)loadContents
  57. {
  58. NSError *error = nil;
  59. NSArray *dicts = [self.plexParser PlexMediaServerParser:self.plexServerAddress port:self.plexServerPort navigationPath:self.plexServerPath authentification:self.plexAuthentification error:&error];
  60. if (error) {
  61. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  62. [self.delegate networkServerBrowser:self requestDidFailWithError:error];
  63. }];
  64. return;
  65. }
  66. NSDictionary *firstObject = [dicts firstObject];
  67. NSString *newAuth = firstObject[@"authentification"] ?: @"";
  68. NSURLComponents *components = [[NSURLComponents alloc] init];
  69. components.scheme = @"http";
  70. components.host = self.plexServerAddress;
  71. components.port = self.plexServerPort;
  72. components.path = self.plexServerPath;
  73. NSURL *url = components.URL;
  74. NSMutableArray *newItems = [NSMutableArray new];
  75. for (NSDictionary *dict in dicts) {
  76. VLCNetworkServerBrowserItemPlex *item = [[VLCNetworkServerBrowserItemPlex alloc] initWithDictionary:dict currentURL:url authentificication:newAuth];
  77. [newItems addObject:item];
  78. }
  79. NSString *titleValue = firstObject[@"libTitle"];
  80. if (titleValue.length) {
  81. self.title = titleValue;
  82. }
  83. self.plexAuthentification = newAuth;
  84. @synchronized(_items) {
  85. _items = [newItems copy];
  86. }
  87. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  88. [self.delegate networkServerBrowserDidUpdate:self];
  89. }];
  90. }
  91. - (VLCMediaList *)mediaList
  92. {
  93. NSMutableArray *mediaArray = [NSMutableArray array];
  94. @synchronized(_items) {
  95. NSUInteger count = _items.count;
  96. for (NSUInteger i = 0; i < count; i++) {
  97. [mediaArray addObject:[_items[i] media]];
  98. }
  99. }
  100. return [[VLCMediaList alloc] initWithArray:mediaArray];
  101. }
  102. - (NSString *)_urlAuth:(NSString *)url
  103. {
  104. return [self.plexWebAPI urlAuth:url authentification:self.plexAuthentification];
  105. }
  106. @end
  107. @interface VLCNetworkServerBrowserItemPlex()
  108. @property (nonatomic) NSString *plexAuthentification;
  109. @end
  110. @implementation VLCNetworkServerBrowserItemPlex
  111. @synthesize name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes, container = _container;
  112. - (instancetype)initWithDictionary:(NSDictionary *)dictionary currentURL:(NSURL *)currentURL authentificication:(NSString *)auth
  113. {
  114. self = [super init];
  115. if (self) {
  116. _plexAuthentification = auth;
  117. NSURLComponents *components = [[NSURLComponents alloc] initWithURL:currentURL resolvingAgainstBaseURL:NO];
  118. NSString *path = components.path;
  119. components.path = nil;
  120. NSURL *baseURL = components.URL;
  121. _container = ![dictionary[@"container"] isEqualToString:@"item"];
  122. NSString *urlPath;
  123. if (_container) {
  124. NSString *keyPath = nil;
  125. NSString *keyValue = dictionary[@"key"];
  126. if ([keyValue rangeOfString:@"library"].location == NSNotFound) {
  127. keyPath = [path stringByAppendingPathComponent:keyValue];
  128. } else {
  129. keyPath = keyValue;
  130. }
  131. urlPath = [baseURL URLByAppendingPathComponent:keyPath].absoluteString;
  132. } else {
  133. urlPath = dictionary[@"keyMedia"];
  134. }
  135. urlPath = [VLCPlexWebAPI urlAuth:urlPath authentification:auth];
  136. _URL = [NSURL URLWithString:urlPath];
  137. _name = dictionary[@"title"];
  138. NSString *thumbPath = dictionary[@"thumb"];
  139. if (thumbPath) {
  140. thumbPath = [VLCPlexWebAPI urlAuth:thumbPath authentification:auth];
  141. }
  142. _thumbnailURL = thumbPath.length ? [baseURL URLByAppendingPathComponent:thumbPath] : nil;
  143. _duration = dictionary[@"duration"];
  144. _fileSizeBytes = dictionary[@"size"];
  145. NSString *subtitleURLString = dictionary[@"keySubtitle"];
  146. if (subtitleURLString) {
  147. subtitleURLString = [VLCPlexWebAPI urlAuth:subtitleURLString authentification:auth];
  148. }
  149. _subtitleURL = subtitleURLString.length ? [baseURL URLByAppendingPathComponent:subtitleURLString] : nil;
  150. }
  151. return self;
  152. }
  153. - (VLCMedia *)media
  154. {
  155. if (!_URL)
  156. return nil;
  157. return [VLCMedia mediaWithURL:_URL];
  158. }
  159. - (id<VLCNetworkServerBrowser>)containerBrowser {
  160. return [[VLCNetworkServerBrowserPlex alloc] initWithName:self.name url:self.URL auth:self.plexAuthentification];
  161. }
  162. @end