VLCNetworkServerBrowserPlex.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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)initWithLogin:(VLCNetworkServerLoginInformation *)login
  31. {
  32. return [self initWithName:login.address
  33. host:login.address
  34. portNumber:login.port
  35. path:@""
  36. authentificication:@""];
  37. }
  38. - (instancetype)initWithName:(NSString *)name host:(NSString *)addressOrName portNumber:(NSNumber *)portNumber path:(NSString *)path authentificication:(NSString *)auth
  39. {
  40. self = [super init];
  41. if (self) {
  42. _title = name;
  43. _plexServerAddress = addressOrName;
  44. _plexServerPort = portNumber.intValue > 0 ? portNumber : @(32400);
  45. _plexServerPath = path;
  46. _plexAuthentification = auth;
  47. _plexParser = [[VLCPlexParser alloc] init];
  48. _plexWebAPI = [[VLCPlexWebAPI alloc] init];
  49. _plexQueue = [[NSOperationQueue alloc] init];
  50. _plexQueue.maxConcurrentOperationCount = 1;
  51. _plexQueue.name = @"org.videolan.vlc-ios.plex.update";
  52. _items = [NSArray new];
  53. }
  54. return self;
  55. }
  56. - (instancetype)initWithName:(NSString *)name url:(NSURL *)url auth:(NSString *)auth {
  57. return [self initWithName:name host:url.host portNumber:url.port path:url.path authentificication:auth];
  58. }
  59. - (void)update {
  60. [self.plexQueue addOperationWithBlock:^{
  61. [self loadContents];
  62. }];
  63. }
  64. - (void)loadContents
  65. {
  66. NSError *error = nil;
  67. NSArray *dicts = [self.plexParser PlexMediaServerParser:self.plexServerAddress port:self.plexServerPort navigationPath:self.plexServerPath authentification:self.plexAuthentification error:&error];
  68. if (error) {
  69. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  70. [self.delegate networkServerBrowser:self requestDidFailWithError:error];
  71. }];
  72. return;
  73. }
  74. NSDictionary *firstObject = [dicts firstObject];
  75. NSString *newAuth = firstObject[@"authentification"] ?: @"";
  76. NSURLComponents *components = [[NSURLComponents alloc] init];
  77. components.scheme = @"http";
  78. components.host = self.plexServerAddress;
  79. components.port = self.plexServerPort;
  80. components.path = self.plexServerPath;
  81. NSURL *url = components.URL;
  82. NSMutableArray *newItems = [NSMutableArray new];
  83. for (NSDictionary *dict in dicts) {
  84. VLCNetworkServerBrowserItemPlex *item = [[VLCNetworkServerBrowserItemPlex alloc] initWithDictionary:dict currentURL:url authentificication:newAuth];
  85. [newItems addObject:item];
  86. }
  87. NSString *titleValue = firstObject[@"libTitle"];
  88. if (titleValue.length) {
  89. self.title = titleValue;
  90. }
  91. self.plexAuthentification = newAuth;
  92. @synchronized(_items) {
  93. _items = [newItems copy];
  94. }
  95. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  96. [self.delegate networkServerBrowserDidUpdate:self];
  97. }];
  98. }
  99. - (VLCMediaList *)mediaList
  100. {
  101. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  102. @synchronized(_items) {
  103. NSUInteger count = _items.count;
  104. for (NSInteger i = count - 1; i >= 0; i--) {
  105. VLCMedia *media = [_items[i] media];
  106. if (media)
  107. [mediaList addMedia:media];
  108. }
  109. }
  110. return mediaList;
  111. }
  112. - (NSString *)_urlAuth:(NSString *)url
  113. {
  114. return [self.plexWebAPI urlAuth:url authentification:self.plexAuthentification];
  115. }
  116. @end
  117. @interface VLCNetworkServerBrowserItemPlex()
  118. @property (nonatomic) NSString *plexAuthentification;
  119. @end
  120. @implementation VLCNetworkServerBrowserItemPlex
  121. @synthesize name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes, container = _container;
  122. - (instancetype)initWithDictionary:(NSDictionary *)dictionary currentURL:(NSURL *)currentURL authentificication:(NSString *)auth
  123. {
  124. self = [super init];
  125. if (self) {
  126. _plexAuthentification = auth;
  127. NSURLComponents *components = [[NSURLComponents alloc] initWithURL:currentURL resolvingAgainstBaseURL:NO];
  128. NSString *path = components.path;
  129. components.path = nil;
  130. NSURL *baseURL = components.URL;
  131. _container = ![dictionary[@"container"] isEqualToString:@"item"];
  132. NSString *urlPath;
  133. if (_container) {
  134. NSString *keyPath = nil;
  135. NSString *keyValue = dictionary[@"key"];
  136. if ([keyValue rangeOfString:@"library"].location == NSNotFound) {
  137. keyPath = [path stringByAppendingPathComponent:keyValue];
  138. } else {
  139. keyPath = keyValue;
  140. }
  141. if (keyPath) {
  142. urlPath = [baseURL URLByAppendingPathComponent:keyPath].absoluteString;
  143. }
  144. } else {
  145. urlPath = dictionary[@"keyMedia"];
  146. }
  147. urlPath = [VLCPlexWebAPI urlAuth:urlPath authentification:auth];
  148. _URL = [NSURL URLWithString:urlPath];
  149. _name = dictionary[@"title"];
  150. NSString *thumbPath = dictionary[@"thumb"];
  151. if (thumbPath) {
  152. thumbPath = [VLCPlexWebAPI urlAuth:thumbPath authentification:auth];
  153. }
  154. _thumbnailURL = thumbPath.length ? [NSURL URLWithString:thumbPath] : nil;
  155. _duration = dictionary[@"duration"];
  156. _fileSizeBytes = dictionary[@"size"];
  157. NSString *subtitleURLString = dictionary[@"keySubtitle"];
  158. if (subtitleURLString) {
  159. subtitleURLString = [VLCPlexWebAPI urlAuth:subtitleURLString authentification:auth];
  160. }
  161. _subtitleURL = subtitleURLString.length ? [baseURL URLByAppendingPathComponent:subtitleURLString] : nil;
  162. }
  163. return self;
  164. }
  165. - (VLCMedia *)media
  166. {
  167. if (!_URL)
  168. return nil;
  169. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  170. NSString *title = self.name;
  171. if (title.length) {
  172. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  173. }
  174. return media;
  175. }
  176. - (id<VLCNetworkServerBrowser>)containerBrowser {
  177. return [[VLCNetworkServerBrowserPlex alloc] initWithName:self.name url:self.URL auth:self.plexAuthentification];
  178. }
  179. @end