VLCNetworkServerBrowserPlex.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserPlex.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015-2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. * Pierre Sagaspe <pierre.sagaspe # me.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCNetworkServerBrowserPlex.h"
  14. #import "VLCPlexParser.h"
  15. #import "VLCPlexWebAPI.h"
  16. @interface VLCNetworkServerBrowserPlex ()
  17. @property (nonatomic, readonly) NSString *addressOrName;
  18. @property (nonatomic, readonly) NSUInteger port;
  19. @property (nonatomic, readwrite) NSString *title;
  20. @property (nonatomic, readonly) NSString *plexServerAddress;
  21. @property (nonatomic, readonly) NSNumber *plexServerPort;
  22. @property (nonatomic, readonly) NSString *plexServerPath;
  23. @property (nonatomic) NSString *plexAuthentification;
  24. @property (nonatomic, readonly) VLCPlexParser *plexParser;
  25. @property (nonatomic, readonly) VLCPlexWebAPI *plexWebAPI;
  26. @property (nonatomic, readonly) NSOperationQueue *plexQueue;
  27. @property (nonatomic, readwrite) NSArray<id<VLCNetworkServerBrowserItem>> *items;
  28. @end
  29. @implementation VLCNetworkServerBrowserPlex
  30. @synthesize title = _title, delegate = _delegate, items = _items, mediaList = _mediaList;
  31. - (instancetype)initWithLogin:(VLCNetworkServerLoginInformation *)login
  32. {
  33. return [self initWithName:login.address
  34. host:login.address
  35. portNumber:login.port
  36. path:@""
  37. authentificication:@""];
  38. }
  39. - (instancetype)initWithName:(NSString *)name host:(NSString *)addressOrName portNumber:(NSNumber *)portNumber path:(NSString *)path authentificication:(NSString *)auth
  40. {
  41. self = [super init];
  42. if (self) {
  43. _title = name;
  44. _plexServerAddress = addressOrName;
  45. _plexServerPort = portNumber.intValue > 0 ? portNumber : @(32400);
  46. _plexServerPath = path;
  47. _plexAuthentification = auth;
  48. _plexParser = [[VLCPlexParser alloc] init];
  49. _plexWebAPI = [[VLCPlexWebAPI alloc] init];
  50. _plexQueue = [[NSOperationQueue alloc] init];
  51. _plexQueue.maxConcurrentOperationCount = 1;
  52. _plexQueue.name = @"org.videolan.vlc-ios.plex.update";
  53. _items = [NSArray new];
  54. }
  55. return self;
  56. }
  57. - (instancetype)initWithName:(NSString *)name url:(NSURL *)url auth:(NSString *)auth {
  58. return [self initWithName:name host:url.host portNumber:url.port path:url.path authentificication:auth];
  59. }
  60. - (void)update {
  61. [self.plexQueue addOperationWithBlock:^{
  62. [self loadContents];
  63. }];
  64. }
  65. - (void)loadContents
  66. {
  67. NSError *error = nil;
  68. NSArray *dicts = [self.plexParser PlexMediaServerParser:self.plexServerAddress port:self.plexServerPort navigationPath:self.plexServerPath authentification:self.plexAuthentification error:&error];
  69. if (error) {
  70. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  71. [self.delegate networkServerBrowser:self requestDidFailWithError:error];
  72. }];
  73. return;
  74. }
  75. NSDictionary *firstObject = [dicts firstObject];
  76. NSString *newAuth = firstObject[@"authentification"] ?: @"";
  77. NSURLComponents *components = [[NSURLComponents alloc] init];
  78. components.scheme = @"http";
  79. components.host = self.plexServerAddress;
  80. components.port = self.plexServerPort;
  81. components.path = self.plexServerPath;
  82. NSURL *url = components.URL;
  83. NSMutableArray *newItems = [NSMutableArray new];
  84. for (NSDictionary *dict in dicts) {
  85. VLCNetworkServerBrowserItemPlex *item = [[VLCNetworkServerBrowserItemPlex alloc] initWithDictionary:dict currentURL:url authentificication:newAuth];
  86. /* warning: we make a http request per item and if the library is big or a weak network, it can slow down the process. */
  87. if (item.URLcontainer) {
  88. NSArray *dictItem = [self.plexParser PlexMediaServerParser:self.plexServerAddress port:self.plexServerPort navigationPath:item.URLcontainer.path authentification:self.plexAuthentification error:&error];
  89. for (NSDictionary *dictx in dictItem) {
  90. item = [[VLCNetworkServerBrowserItemPlex alloc] initWithDictionary:dictx currentURL:url authentificication:newAuth];
  91. [newItems addObject:item];
  92. }
  93. } else {
  94. [newItems addObject:item];
  95. }
  96. }
  97. NSString *titleValue = firstObject[@"libTitle"];
  98. if (titleValue.length) {
  99. self.title = titleValue;
  100. }
  101. self.plexAuthentification = newAuth;
  102. @synchronized(_items) {
  103. _items = [newItems copy];
  104. }
  105. _mediaList = [self buildMediaList];
  106. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  107. [self.delegate networkServerBrowserDidUpdate:self];
  108. }];
  109. }
  110. - (VLCMediaList *)buildMediaList
  111. {
  112. NSMutableArray *mediaArray;
  113. @synchronized(_items) {
  114. mediaArray = [NSMutableArray new];
  115. for (id<VLCNetworkServerBrowserItem> browseritem in _items) {
  116. VLCMedia *media = [browseritem media];
  117. if (media)
  118. [mediaArray addObject:media];
  119. }
  120. }
  121. VLCMediaList *mediaList = [[VLCMediaList alloc] initWithArray:mediaArray];
  122. return mediaList;
  123. }
  124. - (NSString *)_urlAuth:(NSString *)url
  125. {
  126. return [self.plexWebAPI urlAuth:url authentification:self.plexAuthentification];
  127. }
  128. @end
  129. @interface VLCNetworkServerBrowserItemPlex()
  130. @property (nonatomic) NSString *plexAuthentification;
  131. @end
  132. @implementation VLCNetworkServerBrowserItemPlex
  133. @synthesize name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes, container = _container;
  134. - (instancetype)initWithDictionary:(NSDictionary *)dictionary currentURL:(NSURL *)currentURL authentificication:(NSString *)auth
  135. {
  136. self = [super init];
  137. if (self) {
  138. _plexAuthentification = auth;
  139. NSURLComponents *components = [[NSURLComponents alloc] initWithURL:currentURL resolvingAgainstBaseURL:NO];
  140. NSString *path = components.path;
  141. components.path = nil;
  142. NSURL *baseURL = components.URL;
  143. _container = ![dictionary[@"container"] isEqualToString:@"item"];
  144. NSString *urlPath;
  145. if (_container) {
  146. NSString *keyPath = nil;
  147. NSString *keyValue = dictionary[@"key"];
  148. if ([keyValue rangeOfString:@"library"].location == NSNotFound)
  149. keyPath = [path stringByAppendingPathComponent:keyValue];
  150. else
  151. keyPath = keyValue;
  152. if (keyPath)
  153. urlPath = [baseURL URLByAppendingPathComponent:keyPath].absoluteString;
  154. } else {
  155. NSString *keyPath = nil;
  156. NSString *keyValue = dictionary[@"key"];
  157. if ([keyValue rangeOfString:@"library"].location == NSNotFound)
  158. keyPath = [path stringByAppendingPathComponent:keyValue];
  159. else
  160. keyPath = keyValue;
  161. if (keyPath)
  162. urlPath = [baseURL URLByAppendingPathComponent:keyPath].absoluteString;
  163. urlPath = [VLCPlexWebAPI urlAuth:urlPath authentification:auth];
  164. _URLcontainer = [NSURL URLWithString:urlPath];
  165. urlPath = dictionary[@"keyMedia"];
  166. }
  167. urlPath = [VLCPlexWebAPI urlAuth:urlPath authentification:auth];
  168. _URL = [NSURL URLWithString:urlPath];
  169. _name = dictionary[@"title"];
  170. NSString *thumbPath = dictionary[@"thumb"];
  171. if (thumbPath) {
  172. thumbPath = [VLCPlexWebAPI urlAuth:thumbPath authentification:auth];
  173. }
  174. _thumbnailURL = thumbPath.length ? [NSURL URLWithString:thumbPath] : nil;
  175. _duration = dictionary[@"duration"];
  176. _fileSizeBytes = dictionary[@"size"];
  177. _filename = dictionary[@"namefile"];
  178. NSString *subtitleURLString = dictionary[@"keySubtitle"];
  179. if (subtitleURLString)
  180. subtitleURLString = [VLCPlexWebAPI urlAuth:subtitleURLString authentification:auth];
  181. _subtitleURL = subtitleURLString.length ? [NSURL URLWithString:subtitleURLString] : nil;
  182. NSString *subType = dictionary[@"codecSubtitle"];
  183. _subtitleType = subType.length ? subType : nil;
  184. }
  185. return self;
  186. }
  187. - (BOOL)isDownloadable
  188. {
  189. //VLC also needs an extension in the filename for this to work.
  190. return YES;
  191. }
  192. - (VLCMedia *)media
  193. {
  194. if (!_URL)
  195. return [VLCMedia mediaAsNodeWithName:self.name];
  196. VLCMedia *media = [VLCMedia mediaWithURL:_URL];
  197. NSString *title = self.name;
  198. if (title.length) {
  199. [media setMetadata:self.name forKey:VLCMetaInformationTitle];
  200. }
  201. return media;
  202. }
  203. - (id<VLCNetworkServerBrowser>)containerBrowser {
  204. return [[VLCNetworkServerBrowserPlex alloc] initWithName:self.name url:self.URL auth:self.plexAuthentification];
  205. }
  206. @end