VLCNetworkServerBrowserPlex.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. NSArray *dicts = [self.plexParser PlexMediaServerParser:self.plexServerAddress port:self.plexServerPort navigationPath:self.plexServerPath authentification:self.plexAuthentification];
  59. NSDictionary *firstObject = [dicts firstObject];
  60. NSString *newAuth = firstObject[@"authentification"] ?: @"";
  61. NSURLComponents *components = [[NSURLComponents alloc] init];
  62. components.scheme = @"http";
  63. components.host = self.plexServerAddress;
  64. components.port = self.plexServerPort;
  65. components.path = self.plexServerPath;
  66. NSURL *url = components.URL;
  67. NSMutableArray *newItems = [NSMutableArray new];
  68. for (NSDictionary *dict in dicts) {
  69. VLCNetworkServerBrowserItemPlex *item = [[VLCNetworkServerBrowserItemPlex alloc] initWithDictionary:dict currentURL:url authentificication:newAuth];
  70. [newItems addObject:item];
  71. }
  72. NSString *titleValue = firstObject[@"libTitle"];
  73. if (titleValue.length) {
  74. self.title = titleValue;
  75. }
  76. self.plexAuthentification = newAuth;
  77. self.items = [newItems copy];
  78. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  79. [self.delegate networkServerBrowserDidUpdate:self];
  80. }];
  81. }
  82. - (NSString *)_urlAuth:(NSString *)url
  83. {
  84. return [self.plexWebAPI urlAuth:url authentification:self.plexAuthentification];
  85. }
  86. @end
  87. @interface VLCNetworkServerBrowserItemPlex()
  88. @property (nonatomic) NSString *plexAuthentification;
  89. @end
  90. @implementation VLCNetworkServerBrowserItemPlex
  91. @synthesize name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes, container = _container;
  92. - (instancetype)initWithDictionary:(NSDictionary *)dictionary currentURL:(NSURL *)currentURL authentificication:(NSString *)auth
  93. {
  94. self = [super init];
  95. if (self) {
  96. _plexAuthentification = auth;
  97. NSURLComponents *components = [[NSURLComponents alloc] initWithURL:currentURL resolvingAgainstBaseURL:NO];
  98. NSString *path = components.path;
  99. components.path = nil;
  100. NSURL *baseURL = components.URL;
  101. _container = ![dictionary[@"container"] isEqualToString:@"item"];
  102. NSString *urlPath;
  103. if (_container) {
  104. NSString *keyPath = nil;
  105. NSString *keyValue = dictionary[@"key"];
  106. if ([keyValue rangeOfString:@"library"].location == NSNotFound) {
  107. keyPath = [path stringByAppendingPathComponent:keyValue];
  108. } else {
  109. keyPath = keyValue;
  110. }
  111. urlPath = [baseURL URLByAppendingPathComponent:keyPath].absoluteString;
  112. } else {
  113. urlPath = dictionary[@"keyMedia"];
  114. }
  115. urlPath = [VLCPlexWebAPI urlAuth:urlPath authentification:auth];
  116. _URL = [NSURL URLWithString:urlPath];
  117. _name = dictionary[@"title"];
  118. NSString *thumbPath = dictionary[@"thumb"];
  119. if (thumbPath) {
  120. thumbPath = [VLCPlexWebAPI urlAuth:thumbPath authentification:auth];
  121. }
  122. _thumbnailURL = thumbPath.length ? [baseURL URLByAppendingPathComponent:thumbPath] : nil;
  123. _duration = dictionary[@"duration"];
  124. _fileSizeBytes = dictionary[@"size"];
  125. NSString *subtitleURLString = dictionary[@"keySubtitle"];
  126. if (subtitleURLString) {
  127. subtitleURLString = [VLCPlexWebAPI urlAuth:subtitleURLString authentification:auth];
  128. }
  129. _subtitleURL = subtitleURLString.length ? [baseURL URLByAppendingPathComponent:subtitleURLString] : nil;
  130. }
  131. return self;
  132. }
  133. - (id<VLCNetworkServerBrowser>)containerBrowser {
  134. return [[VLCNetworkServerBrowserPlex alloc] initWithName:self.name url:self.URL auth:self.plexAuthentification];
  135. }
  136. @end