VLCNetworkServerBrowserPlex.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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, mediaList = _mediaList;
  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. _mediaList = [self buildMediaList];
  96. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  97. [self.delegate networkServerBrowserDidUpdate:self];
  98. }];
  99. }
  100. - (VLCMediaList *)buildMediaList
  101. {
  102. NSMutableArray *mediaArray;
  103. @synchronized(_items) {
  104. mediaArray = [NSMutableArray new];
  105. for (id<VLCNetworkServerBrowserItem> browseritem in _items) {
  106. VLCMedia *media = [browseritem media];
  107. if (media)
  108. [mediaArray addObject:media];
  109. }
  110. }
  111. VLCMediaList *mediaList = [[VLCMediaList alloc] initWithArray:mediaArray];
  112. return mediaList;
  113. }
  114. - (NSString *)_urlAuth:(NSString *)url
  115. {
  116. return [self.plexWebAPI urlAuth:url authentification:self.plexAuthentification];
  117. }
  118. @end
  119. @interface VLCNetworkServerBrowserItemPlex()
  120. @property (nonatomic) NSString *plexAuthentification;
  121. @end
  122. @implementation VLCNetworkServerBrowserItemPlex
  123. @synthesize name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes, container = _container;
  124. - (instancetype)initWithDictionary:(NSDictionary *)dictionary currentURL:(NSURL *)currentURL authentificication:(NSString *)auth
  125. {
  126. self = [super init];
  127. if (self) {
  128. _plexAuthentification = auth;
  129. NSURLComponents *components = [[NSURLComponents alloc] initWithURL:currentURL resolvingAgainstBaseURL:NO];
  130. NSString *path = components.path;
  131. components.path = nil;
  132. NSURL *baseURL = components.URL;
  133. _container = ![dictionary[@"container"] isEqualToString:@"item"];
  134. NSString *urlPath;
  135. if (_container) {
  136. NSString *keyPath = nil;
  137. NSString *keyValue = dictionary[@"key"];
  138. if ([keyValue rangeOfString:@"library"].location == NSNotFound) {
  139. keyPath = [path stringByAppendingPathComponent:keyValue];
  140. } else {
  141. keyPath = keyValue;
  142. }
  143. if (keyPath) {
  144. urlPath = [baseURL URLByAppendingPathComponent:keyPath].absoluteString;
  145. }
  146. } else {
  147. urlPath = dictionary[@"keyMedia"];
  148. }
  149. urlPath = [VLCPlexWebAPI urlAuth:urlPath authentification:auth];
  150. _URL = [NSURL URLWithString:urlPath];
  151. _name = dictionary[@"title"];
  152. NSString *thumbPath = dictionary[@"thumb"];
  153. if (thumbPath) {
  154. thumbPath = [VLCPlexWebAPI urlAuth:thumbPath authentification:auth];
  155. }
  156. _thumbnailURL = thumbPath.length ? [NSURL URLWithString:thumbPath] : nil;
  157. _duration = dictionary[@"duration"];
  158. _fileSizeBytes = dictionary[@"size"];
  159. _filename = dictionary[@"namefile"];
  160. NSString *subtitleURLString = dictionary[@"keySubtitle"];
  161. if (subtitleURLString) {
  162. subtitleURLString = [VLCPlexWebAPI urlAuth:subtitleURLString authentification:auth];
  163. }
  164. _subtitleURL = subtitleURLString.length ? [baseURL URLByAppendingPathComponent:subtitleURLString] : nil;
  165. }
  166. return self;
  167. }
  168. - (BOOL)isDownloadable
  169. {
  170. //VLC also needs an extension in the filename for this to work.
  171. return YES;
  172. }
  173. - (VLCMedia *)media
  174. {
  175. if (!_URL)
  176. return [VLCMedia mediaAsNodeWithName:self.name];
  177. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  178. NSString *title = self.name;
  179. if (title.length) {
  180. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  181. }
  182. return media;
  183. }
  184. - (id<VLCNetworkServerBrowser>)containerBrowser {
  185. return [[VLCNetworkServerBrowserPlex alloc] initWithName:self.name url:self.URL auth:self.plexAuthentification];
  186. }
  187. @end