VLCPlexParser.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*****************************************************************************
  2. * VLCPlexParser.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-2015 VideoLAN. All rights reserved.
  6. *
  7. * Authors: Pierre Sagaspe <pierre.sagaspe # me.com>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCPlexParser.h"
  12. #import "VLCPlexWebAPI.h"
  13. #import "SSKeychain.h"
  14. static NSString *const kPlexMediaServerDirInit = @"/library/sections";
  15. static NSString *const kPlexVLCDeviceName = @"VLC for iOS";
  16. @interface VLCPlexParser () <NSXMLParserDelegate>
  17. {
  18. NSMutableArray *_containerInfo;
  19. NSMutableDictionary *_dicoInfo;
  20. NSString *_PlexMediaServerUrl;
  21. }
  22. @end
  23. @implementation VLCPlexParser
  24. - (NSArray *)PlexMediaServerParser:(NSString *)address port:(NSNumber *)port navigationPath:(NSString *)path authentification:(NSString *)auth error:(NSError *__autoreleasing *)outError
  25. {
  26. _containerInfo = [[NSMutableArray alloc] init];
  27. _dicoInfo = [[NSMutableDictionary alloc] init];
  28. NSURLComponents *urlComponents = [[NSURLComponents alloc] init];
  29. urlComponents.scheme = @"http";
  30. urlComponents.host = address;
  31. urlComponents.port = port;
  32. _PlexMediaServerUrl = [urlComponents URL].absoluteString;
  33. NSString *mediaServerUrl;
  34. if ([path length] == 0) {
  35. urlComponents.path = kPlexMediaServerDirInit;
  36. } else {
  37. if ([path rangeOfString:@"library"].location != NSNotFound) {
  38. urlComponents.path = [@"/" stringByAppendingPathComponent:path];
  39. } else {
  40. urlComponents.path = [kPlexMediaServerDirInit stringByAppendingPathComponent:path];
  41. }
  42. }
  43. mediaServerUrl = [[urlComponents URL].absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  44. VLCPlexWebAPI *PlexWebAPI = [[VLCPlexWebAPI alloc] init];
  45. NSURL *url = [[NSURL alloc] initWithString:[PlexWebAPI urlAuth:mediaServerUrl authentification:auth]];
  46. NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
  47. NSHTTPURLResponse *response = nil;
  48. NSError *error = nil;
  49. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  50. if ([response statusCode] != 200) {
  51. NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  52. if([responseString rangeOfString:@"Unauthorized"].location != NSNotFound) {
  53. NSString *serviceString = [NSString stringWithFormat:@"plex://%@%@", address, port];
  54. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  55. if (!accounts) {
  56. serviceString = @"plex://Account";
  57. accounts = [SSKeychain accountsForService:serviceString];
  58. }
  59. if (accounts) {
  60. NSDictionary *account = [accounts firstObject];
  61. NSString *username = [account objectForKey:@"acct"];
  62. NSString *password = [SSKeychain passwordForService:serviceString account:username];
  63. auth = [PlexWebAPI PlexAuthentification:username password:password];
  64. url = [NSURL URLWithString:[PlexWebAPI urlAuth:mediaServerUrl authentification:auth]];
  65. request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
  66. response = nil;
  67. error = nil;
  68. data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  69. if ([response statusCode] != 200) {
  70. if (outError) {
  71. NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:
  72. @{
  73. NSLocalizedDescriptionKey : NSLocalizedString(@"PLEX_ERROR_ACCOUNT", nil),
  74. NSLocalizedFailureReasonErrorKey : NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil),
  75. }];
  76. if (error) {
  77. userInfo[NSUnderlyingErrorKey] = error;
  78. }
  79. *outError = [NSError errorWithDomain:@"org.videolan.vlc-ios.plexParser"
  80. code:response.statusCode
  81. userInfo:userInfo];
  82. }
  83. }
  84. [_containerInfo removeAllObjects];
  85. [_dicoInfo removeAllObjects];
  86. } else {
  87. if (outError) {
  88. NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:
  89. @{
  90. NSLocalizedDescriptionKey : NSLocalizedString(@"UNAUTHORIZED", nil),
  91. NSLocalizedFailureReasonErrorKey : NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil),
  92. }];
  93. if (error) {
  94. userInfo[NSUnderlyingErrorKey] = error;
  95. }
  96. *outError = [NSError errorWithDomain:@"org.videolan.vlc-ios.plexParser"
  97. code:response.statusCode
  98. userInfo:userInfo];
  99. }
  100. }
  101. } else
  102. APLog(@"PlexParser url Errors : %ld", (long)[response statusCode]);
  103. }
  104. if (error) {
  105. APLog(@"PlexParser url Error: %@", error);
  106. }
  107. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
  108. [xmlparser setDelegate:self];
  109. if (![xmlparser parse])
  110. APLog(@"PlexParser url Errors : %@", url);
  111. [_containerInfo setValue:auth forKey:@"authentification"];
  112. return [NSArray arrayWithArray:_containerInfo];
  113. }
  114. - (NSArray *)PlexExtractDeviceInfo:(NSData *)data
  115. {
  116. _containerInfo = [[NSMutableArray alloc] init];
  117. _dicoInfo = [[NSMutableDictionary alloc] init];
  118. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
  119. [xmlparser setDelegate:self];
  120. if (![xmlparser parse])
  121. APLog(@"PlexParser data Errors : %@", data);
  122. return [NSArray arrayWithArray:_containerInfo];
  123. }
  124. #pragma mark - Parser
  125. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  126. {
  127. if ([elementName isEqualToString:@"MediaContainer"]) {
  128. if ([attributeDict objectForKey:@"friendlyName"])
  129. [_dicoInfo setObject:[attributeDict objectForKey:@"friendlyName"] forKey:@"libTitle"];
  130. else if ([attributeDict objectForKey:@"title1"])
  131. [_dicoInfo setObject:[attributeDict objectForKey:@"title1"] forKey:@"libTitle"];
  132. if ([attributeDict objectForKey:@"title2"])
  133. [_dicoInfo setObject:[attributeDict objectForKey:@"title2"] forKey:@"libTitle"];
  134. if ([attributeDict objectForKey:@"grandparentTitle"])
  135. [_dicoInfo setObject:[attributeDict objectForKey:@"grandparentTitle"] forKey:@"grandparentTitle"];
  136. } else if ([elementName isEqualToString:@"Directory"]) {
  137. [_dicoInfo setObject:@"directory" forKey:@"container"];
  138. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  139. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  140. } else if ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"]) {
  141. [_dicoInfo setObject:@"item" forKey:@"container"];
  142. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  143. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  144. [_dicoInfo setObject:[attributeDict objectForKey:@"ratingKey"] forKey:@"ratingKey"];
  145. if ([attributeDict objectForKey:@"summary"])
  146. [_dicoInfo setObject:[attributeDict objectForKey:@"summary"] forKey:@"summary"];
  147. if ([attributeDict objectForKey:@"viewCount"])
  148. [_dicoInfo setObject:@"watched" forKey:@"state"];
  149. else
  150. [_dicoInfo setObject:@"unwatched" forKey:@"state"];
  151. } else if ([elementName isEqualToString:@"Media"]) {
  152. if ([attributeDict objectForKey:@"audioCodec"])
  153. [_dicoInfo setObject:[attributeDict objectForKey:@"audioCodec"] forKey:@"audioCodec"];
  154. if ([attributeDict objectForKey:@"videoCodec"])
  155. [_dicoInfo setObject:[attributeDict objectForKey:@"videoCodec"] forKey:@"videoCodec"];
  156. } else if ([elementName isEqualToString:@"Part"]) {
  157. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  158. if([attributeDict objectForKey:@"file"])
  159. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  160. NSString *duration = [[VLCTime timeWithNumber:[attributeDict objectForKey:@"duration"]] stringValue];
  161. [_dicoInfo setObject:duration forKey:@"duration"];
  162. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  163. if (sizeFile)
  164. [_dicoInfo setObject:sizeFile forKey:@"size"];
  165. } else if ([elementName isEqualToString:@"Stream"]) {
  166. if ([attributeDict objectForKey:@"key"])
  167. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  168. if ([attributeDict objectForKey:@"codec"])
  169. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  170. if ([attributeDict objectForKey:@"language"])
  171. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  172. if ([attributeDict objectForKey:@"languageCode"])
  173. [_dicoInfo setObject:[attributeDict objectForKey:@"languageCode"] forKey:@"languageCode"];
  174. } else if ([elementName isEqualToString:@"Device"] && [[attributeDict objectForKey:@"name"] isEqualToString:kPlexVLCDeviceName]) {
  175. if ([attributeDict objectForKey:@"name"])
  176. [_dicoInfo setObject:[attributeDict objectForKey:@"name"] forKey:@"name"];
  177. if ([attributeDict objectForKey:@"product"])
  178. [_dicoInfo setObject:[attributeDict objectForKey:@"product"] forKey:@"product"];
  179. if ([attributeDict objectForKey:@"productVersion"])
  180. [_dicoInfo setObject:[attributeDict objectForKey:@"productVersion"] forKey:@"productVersion"];
  181. if ([attributeDict objectForKey:@"platformVersion"])
  182. [_dicoInfo setObject:[attributeDict objectForKey:@"platformVersion"] forKey:@"platformVersion"];
  183. if ([attributeDict objectForKey:@"token"])
  184. [_dicoInfo setObject:[attributeDict objectForKey:@"token"] forKey:@"token"];
  185. if ([attributeDict objectForKey:@"clientIdentifier"])
  186. [_dicoInfo setObject:[attributeDict objectForKey:@"clientIdentifier"] forKey:@"clientIdentifier"];
  187. if ([attributeDict objectForKey:@"id"])
  188. [_dicoInfo setObject:[attributeDict objectForKey:@"id"] forKey:@"id"];
  189. }
  190. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"] || [elementName isEqualToString:@"Track"]))
  191. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  192. }
  193. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  194. {
  195. if (([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"] || [elementName isEqualToString:@"Device"]) && [_dicoInfo count] > 0) {
  196. [_containerInfo addObject:_dicoInfo];
  197. _dicoInfo = [[NSMutableDictionary alloc] init];
  198. }
  199. }
  200. @end